# Different FastCGI backend for /subdir: regex not matching

**URL:** <https://www.ruby-forum.com/t/different-fastcgi-backend-for-subdir-regex-not-matching/210575>\
**Category:** NGINX\
**Created:** [August 11, 2011, 10:23am UTC](https://www.ruby-forum.com/t/different-fastcgi-backend-for-subdir-regex-not-matching/210575 "2011-08-11T10:23:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [August 11, 2011, 10:23am UTC](https://www.ruby-forum.com/t/different-fastcgi-backend-for-subdir-regex-not-matching/210575/1 "2011-08-11T10:23:43Z")

</div>

I have a virtual host, and for a certain subdirectory I want to use  
another fastcgi backend.

What I have now goes something like:  
(…)  
location ~ /subdir/ {  
alias /var/www/otheraccount/public\_html/;

```
            # PHP FastCGI
            location ^~ /subdir\/(.+)\.php {
                    return 504;
                    include /usr/pkg/etc/nginx/fastcgi_params;

                    fastcgi_pass 127.0.0.1:9000;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME

```

/var/www/othervhost/public\_html$fastcgi\_script\_name;

```
                    try_files $uri =502;
            }
   }

    # PHP FastCGI
    location ~ \.php {
            include /usr/pkg/etc/nginx/fastcgi_params;

            fastcgi_pass 127.0.0.1:9001;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME

```

/var/www/blah/public\_html$fastcgi\_script\_name;

```
            try_files $uri =403;
    }

```

}

Ignore the try\_files and bogus return, they’re in place to try to trace  
what’s happening where.

This is nginx 1.0.4, if it matters.

Basically, the problem is that the last try\_files is triggered (I get a  
403 requesting /subdir/index.php, and if I change that last try\_files to  
404 then I get a 404); Therefore I conclude the regex trying to catch  
php files under /subdir is not effective.

What would the proper regex be ?  
I want it to catch /subdir/_.php, /subdir/subsub/_.php,  
/subdir/s/u/b.php, etc

On a related note, trying to access www.vhost.local/subdir (no trailing  
/) results in nginx trying to serve subdir from the original root (ie  
the alias isn’t picked up); It may be a trivial question but I’ve run  
into this in the past. What is the correct way to catch both /subdir and  
/subdir/ requests?

I would very much appreciate a reply on this subject, as I’ve been  
struggling with this for hours. Regexes are not my strong point.

Thanks.

Posted at Nginx Forum:

> **[Different FastCGI backend for /subdir: regex not matching](https://forum.nginx.org/read.php?2,213682,213682#msg-213682)**
>
> I have a virtual host, and for a certain subdirectory I want to use another fastcgi backend. What I have now goes something like: (...) location ~ /subdir/ { alias /var/www/otheraccount/public\_html/; # PHP FastC

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 11, 2011, 12:46pm UTC](https://www.ruby-forum.com/t/different-fastcgi-backend-for-subdir-regex-not-matching/210575/2 "2011-08-11T12:46:51Z")

</div>

Hello!

On Thu, Aug 11, 2011 at 04:22:52AM -0400, hugo wrote:

> I have a virtual host, and for a certain subdirectory I want to use  
> another fastcgi backend.
> 
> What I have now goes something like:  
> (…)  
> location ~ /subdir/ {

Do you actually want to match prefix here, as opposed to any uri  
containing “/subdir/” in it? If yes, use

```
     location /subdir/ {

```

instead. To prevent all other regexp locations from matching  
under /subdir/ (even if there are no regexp locations in subdir  
matched) you may also want to use “^~” (no regexp check on match),  
i.e.

```
     location ^~ /subdir/ {

```

> ```
> alias /var/www/otheraccount/public_html/;
> 
> # PHP FastCGI
> location ^~ /subdir\/(.+)\.php {
> 
> ```

“^~” means “no regexp check on match”, you have to use “~” (regexp  
location) here instead. And unless you really need captures here  
for some reason not present in posted config, it’s enough to just  
use .php pattern here. You may also need “$” anchor to make sure  
you are matching extension but not some intermediate part of  
name.

I.e. use

```
              location ~ \.php$ {

```

here instead.

> ```
> }
> 
> # PHP FastCGI
> location ~ \.php {
> 
> ```

The same as above: you probably need “$” anchor here.

```
      location ~ \.php$ {

```

> Ignore the try\_files and bogus return, they’re in place to try to trace  
> what’s happening where.
> 
> This is nginx 1.0.4, if it matters.
> 
> Basically, the problem is that the last try\_files is triggered (I get a  
> 403 requesting /subdir/index.php, and if I change that last try\_files to  
> 404 then I get a 404); Therefore I conclude the regex trying to catch  
> php files under /subdir is not effective.

See above. The problem is that you don’t have regexp but static  
string, due to “^~” used.

> What would the proper regex be ?  
> I want it to catch /subdir/_.php, /subdir/subsub/_.php,  
> /subdir/s/u/b.php, etc

Recommended pattern is:

```
location / {
    ...

    location ~ \.php$ {
        ...
    }
}

location /subdir/ {
    ...

    location ~ \.php$ {
        ...
    }
}

```

Note all top-level locations are just normal static strings with  
prefix match (most specific match wins). Regexp locations are  
properly isolated in them, and don’t interfere with processing of  
requests in other locations.

> On a related note, trying to access www.vhost.local/subdir (no trailing  
> /) results in nginx trying to serve subdir from the original root (ie  
> the alias isn’t picked up); It may be a trivial question but I’ve run

This is because “/subdir” isn’t matched by “location /subdir/”  
(neither by “location ~ /subdir/”), and hence it’s processed in  
some other matching location (usually “location /”, or implicit  
location if you don’t have one defined).

> into this in the past. What is the correct way to catch both /subdir and  
> /subdir/ requests?

I would recommend using something like

```
location = /subdir {
    root ...
    # or just immediate redirect to /subdir/, e.g
    # return 301 /subdir/;
}

location /subdir/ {
    root ...
}

```

Note that if you use

```
location /subdir/ {
    proxy_pass ...
}

```

nginx will automagically recognize that requests to “/subdir”  
should be redirected to “/subdir/”, and you don’t need extra  
“location = /subdir”. This applies only to locations fully  
handled by backend modules though.

> I would very much appreciate a reply on this subject, as I’ve been  
> struggling with this for hours. Regexes are not my strong point.

Don’t use regexp unless you really need to, use normal locations.  
(This applies even if one have no problems with regexps.)

Maxim D.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 11, 2011, 3:03pm UTC](https://www.ruby-forum.com/t/different-fastcgi-backend-for-subdir-regex-not-matching/210575/3 "2011-08-11T15:03:51Z")

</div>

## hugo Wrote:

> $fastcgi\_script\_filename includes /subdir, and the  
> path ends up as  
> /var/www/othervhost/public\_html/subdir/target.php;  
> however target.php lives in the root folder,  
> below.
> 
> Short of using ../, what is the recommended  
> solution?

Actually ../ will lead nowhere, please disregard that.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,213682,213696#msg-213696](http://forum.nginx.org/read.php?2,213682,213696#msg-213696)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 11, 2011, 3:01pm UTC](https://www.ruby-forum.com/t/different-fastcgi-backend-for-subdir-regex-not-matching/210575/4 "2011-08-11T15:01:22Z")

</div>

Hello,

Thanks for the reply. I’m getting somewhere now. Clearly a review of the  
documentation regarding location is in order for me.

I’m stumped by something else now;

The fastcgi server returns a 404 for /subdir/target.php, because  
$fastcgi\_script\_filename includes /subdir, and the path ends up as  
/var/www/othervhost/public\_html/subdir/target.php; however target.php  
lives in the root folder, below.

Short of using …/, what is the recommended solution?

Posted at Nginx Forum:

> **[Re: Different FastCGI backend for /subdir: regex not matching](https://forum.nginx.org/read.php?2,213682,213695#msg-213695)**
>
> I have a virtual host, and for a certain subdirectory I want to use another fastcgi backend. What I have now goes something like: (...) location ~ /subdir/ { alias /var/www/otheraccount/public\_html/; # PHP FastC

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 12, 2011, 9:34am UTC](https://www.ruby-forum.com/t/different-fastcgi-backend-for-subdir-regex-not-matching/210575/5 "2011-08-12T09:34:30Z")

</div>

I worked around this by adding a symlink [/var/www/othervhost/subir →   
public\_html]. Ideally I’d like to solve it at the webserver level, but  
this will do too.

The location blocks provided before are working. Thank you.

Regards,

Hugo

Posted at Nginx Forum:

> **[Re: Different FastCGI backend for /subdir: regex not matching](https://forum.nginx.org/read.php?2,213682,213721#msg-213721)**
>
> I have a virtual host, and for a certain subdirectory I want to use another fastcgi backend. What I have now goes something like: (...) location ~ /subdir/ { alias /var/www/otheraccount/public\_html/; # PHP FastC
