# Issue with multiple location blocks to handle two webapps

**URL:** <https://www.ruby-forum.com/t/issue-with-multiple-location-blocks-to-handle-two-webapps/222417>\
**Category:** NGINX\
**Created:** [August 28, 2012, 6:40am UTC](https://www.ruby-forum.com/t/issue-with-multiple-location-blocks-to-handle-two-webapps/222417 "2012-08-28T06:40:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [August 28, 2012, 6:40am UTC](https://www.ruby-forum.com/t/issue-with-multiple-location-blocks-to-handle-two-webapps/222417/1 "2012-08-28T06:40:30Z")

</div>

Hi All,

My webapp is a combination of two apps running on one server.

the community app (smaller one) is located under community directory and  
the all urls starting with community should be handled by it  
e.g.  
[example.com/community/questionsexample,com/community/2/an-actual-qestion](http://example.com/community/questionsexample,com/community/2/an-actual-qestion)  
etc etc

The main app takes care of rest of URLs such as [example.com](http://example.com) (which  
should  
redirect to [example.com/city](http://example.com/city)) [example.com/city/sampledir](http://example.com/city/sampledir) etc etc

## the location blocks I have used are

#handle community section separately  
location ^~ /_community_/ {  
try\_files $uri $uri/ /index.php?qa-rewrite=$uri&$args;  
location ~ .php$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
include fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME  
$document\_root$fastcgi\_script\_name;  
fastcgi\_param PATH\_INFO $fastcgi\_script\_name;  
access\_log off;  
}  
}

```
location =/ {
    #convert domain to domain/city
    rewrite ^(.*)$ $scheme://example.com/city permanent;
}

```

#these locations always use /city as prefix  
location ~^/(biz|biz/|profiles|profiles/) {  
rewrite ^(.\*)$ $scheme://example.com/city$1 permanent;  
}

```
#main app should handle everything except community
location* /* {
    #remove trailing slash
    rewrite ^/(.*)/$ /$1 permanent;
    #NB this will remove the index.php from the url
    try_files $uri $uri /index.php?$args;
}

location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME 

```

## $document\_root$fastcgi\_script\_name; fastcgi\_param PATH\_INFO $fastcgi\_script\_name; access\_log off; }

With this code the main app works fine but the community app ONLY works  
for  
[example.com/community/](http://example.com/community/) and [example.com/community/?a=foo/bar](http://example.com/community/?a=foo/bar) etc etc but  
\*NOT

- for [example.com/community/foo/bar](http://example.com/community/foo/bar)

I think [example.com/community/foo/bar](http://example.com/community/foo/bar) doesn’t get handled by the  
community  
location block and goes to the main location which gives 404 error.

I tried various combination for location but nothing is fixing it  
perfectly. I will appreciate any pointers / help.

Thanks,  
Nikhil

---

<div class="post-metadata">

**Author:** ![Nikhil\_Bhardwaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecccb3/32.png) [@Nikhil\_Bhardwaj](https://www.ruby-forum.com/u/Nikhil_Bhardwaj)\
**Post date:** [August 28, 2012, 10:26am UTC](https://www.ruby-forum.com/t/issue-with-multiple-location-blocks-to-handle-two-webapps/222417/2 "2012-08-28T10:26:05Z")

</div>

On Mon, Aug 27, 2012 at 09:39:55PM -0700, Nikhil Bhardwaj wrote:

Hi there,

> ```
> location ^~ /*community*/ {
> try_files $uri $uri/ /index.php?qa-rewrite=$uri&$args;
> 
> ```

If you ask for /community/foo/bar, this will end up using  
/index.php?qa-rewrite=/community/foo/bar&, which will be handled in the  
top level “location ~ .php” block.

Perhaps you want

try\_files $uri $uri/ /community/index.php?qa-rewrite=$uri&$args;

here? (Not tested by me.)

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Nikhil\_Bhardwaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecccb3/32.png) [@Nikhil\_Bhardwaj](https://www.ruby-forum.com/u/Nikhil_Bhardwaj)\
**Post date:** [August 28, 2012, 11:44pm UTC](https://www.ruby-forum.com/t/issue-with-multiple-location-blocks-to-handle-two-webapps/222417/3 "2012-08-28T23:44:06Z")

</div>

Thanks Francis,

> here? (Not tested by me.)  
> f  
> This did the trick. $uri had /community as the first part but you are  
> right using  
> try\_files $uri $uri/ /community/index.php?qa-rewrite=$uri&$args;  
> forced it to use the correct php location and then in the application  
> removing /community from the $uri (which translates to  
> $\_GET[‘qa-rewrite’]) worked perfectly for me.

Thanks again.  
Nikhil
