# Need help with nginx rewrites (for sub-domains)

**URL:** <https://www.ruby-forum.com/t/need-help-with-nginx-rewrites-for-sub-domains/206361>\
**Category:** NGINX\
**Created:** [April 29, 2011, 1:11am UTC](https://www.ruby-forum.com/t/need-help-with-nginx-rewrites-for-sub-domains/206361 "2011-04-29T01:11:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [April 29, 2011, 1:11am UTC](https://www.ruby-forum.com/t/need-help-with-nginx-rewrites-for-sub-domains/206361/1 "2011-04-29T01:11:17Z")

</div>

Hello,

I have been scratching my head last 2 days trying to get this working:

I have a Rails application, say, [http://example.com](http://example.com) and now I want to  
run a PHP app on [http://example.com/coupons](http://example.com/coupons)

I set up the PHP app but I am having a hard time with nginx rewrites. I  
hope someone here can assist me:

Here’s how I set up things in nginx.conf

> <https://gist.github.com/jimishjoban/946479>

Now [http://example.com/coupons](http://example.com/coupons) loads up the home page, however if I try  
[http://example.com/coupons/](http://example.com/coupons/) (with the extra slash), it says “No input  
file found”

Same goes with [http://example.com/coupons/Admin](http://example.com/coupons/Admin) and so on… somehow the  
rewrites are screwed up i guess…

PS: The above code has been tweaked again and again, so if there is  
something silly, be nice and let me know 😉

---

<div class="post-metadata">

**Author:** ![jimishjoban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jimishjoban](https://www.ruby-forum.com/u/jimishjoban)\
**Post date:** [April 29, 2011, 5:20am UTC](https://www.ruby-forum.com/t/need-help-with-nginx-rewrites-for-sub-domains/206361/2 "2011-04-29T05:20:48Z")

</div>

On Fri, Apr 29, 2011 at 6:11 AM, Jimish Jobanputra  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here’s how I set up things in nginx.conf  
> PS: The above code has been tweaked again and again, so if there is  
> something silly, be nice and let me know 😉

you’re thinking too far.

First, create a symlink like this:  
/home/jjobanputra/code/coupon/coupons =\>  
/home/jjobanputra/code/coupon/upload

(eg.  
ln -s /home/jjobanputra/code/coupon/upload  
/home/jjobanputra/code/coupon/coupons  
)

Using alias is a shortcut for getting headache.

then the config:

#order is important.  
location ~ ^/coupons/.\*.php$ {  
root /home/jjobanputra/code/coupon;  
try\_files $uri =404;  
fastcgi\_pass 127.0.0.1:53217;  
fastcgi\_index index.php;  
#probably not needed - it should be covered in fastcgi.conf  
fastcgi\_param SCRIPT\_FILENAME $request\_filename;  
include /opt/nginx/conf/fastcgi.conf;  
}

location /coupons/ {  
root /home/jjobanputra/code/coupon;  
index index.php index.html index.htm;  
#this line below probably not going to work as expected  
#the alternative is to capture the url using location ~ ^/coupons/(.\*)

# and use it as parameter replacing uri in q=$uri

#another possible hack is using @location and rewrite.  
try\_files $uri $uri/ /coupons/index.php?q=$uri;  
expires 30d;  
}

#I find this mandatory for any kind of rewrites.  
#Especially if the root is different than the global root.  
#Basically a behaviour difference.  
location = /coupons {  
rewrite ^ /coupons/ permanent;  
}

---

<div class="post-metadata">

**Author:** ![jimishjoban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jimishjoban](https://www.ruby-forum.com/u/jimishjoban)\
**Post date:** [April 29, 2011, 12:01pm UTC](https://www.ruby-forum.com/t/need-help-with-nginx-rewrites-for-sub-domains/206361/3 "2011-04-29T12:01:00Z")

</div>

Thanks a lot!!! That works like a charm…

However, there is one more issue:

If I try to go to [http://example.com/coupons/about](http://example.com/coupons/about) then it takes me to  
index page itself… I tried couple of things, but no luck yet…

---

<div class="post-metadata">

**Author:** ![jimishjoban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jimishjoban](https://www.ruby-forum.com/u/jimishjoban)\
**Post date:** [April 29, 2011, 12:17pm UTC](https://www.ruby-forum.com/t/need-help-with-nginx-rewrites-for-sub-domains/206361/4 "2011-04-29T12:17:47Z")

</div>

On Fri, Apr 29, 2011 at 5:01 PM, Jimish Jobanputra  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks a lot!!! That works like a charm…
> 
> However, there is one more issue:
> 
> If I try to go to [http://example.com/coupons/about](http://example.com/coupons/about) then it takes me to  
> index page itself… I tried couple of things, but no luck yet…

I guess the `location /coupons/` should go like this instead (replace  
entire `location /coupons/ { }` block):

location ~ /coupons/(.\*) {  
root /home/jjobanputra/code/coupon;  
try\_files $uri $uri/ /coupons/index.php?q=$1;  
index index.php index.html index.htm;  
expires 30d;  
}

---

<div class="post-metadata">

**Author:** ![jimishjoban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jimishjoban](https://www.ruby-forum.com/u/jimishjoban)\
**Post date:** [April 29, 2011, 12:33pm UTC](https://www.ruby-forum.com/t/need-help-with-nginx-rewrites-for-sub-domains/206361/5 "2011-04-29T12:33:52Z")

</div>

On Fri, Apr 29, 2011 at 01:11:17AM +0200, Jimish Jobanputra wrote:

> Here’s how I set up things in nginx.conf  
> PS: The above code has been tweaked again and again, so if there is  
> something silly, be nice and let me know 😉

It’s really hard to understand using your configuration how  
/coupons URLs should be processed. Could describe it like this

/coupons  
script /home/jjobanputra/code/coupon/upload/index.php

/coupons/  
script /home/jjobanputra/code/coupon/upload/index.php

/coupons/Admin  
scipt /home/jjobanputra/code/coupon/upload/index.php  
path\_info /Admin

/coupons/image.gif  
file /home/jjobanputra/code/coupon/upload/image.gif

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![jimishjoban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jimishjoban](https://www.ruby-forum.com/u/jimishjoban)\
**Post date:** [April 29, 2011, 12:31pm UTC](https://www.ruby-forum.com/t/need-help-with-nginx-rewrites-for-sub-domains/206361/6 "2011-04-29T12:31:27Z")

</div>

I think, probably I have messed up with something… so my index.php  
looks like this:

```
    $qstring = explode ( "/" , $_GET["qstr"] ) ;

    if ( $qstring[0] != "" )
    {
            $entity = $data->select ( "SEF_URL" , "*" , array ( 

```

“URL” =\> $qstring[0] ) ) ;  
$entity = $entity[0] ;

```
            switch ( $entity["EntityType"] )
            {
                    case "StaticPage" :
                            $static_page = 1 ;
                            $entity_id = $entity["EntityID"] ;
                            include ( "st_page.php" ) ;
                            break;
                    case "Category" :
                            $category_id = $entity["EntityID"] ;
                            include ( "browse.php" ) ;
                            break;
                    case "Website" :
                            $entity_id = $entity["EntityID"] ;
                            include ( "detail.php" ) ;
                            break;
                    default :
                            include ( "sef_redirector.php" ) ;
                            break;
            }
    }
    else
    {
            include ( "sef_redirector.php" ) ;
    }

```

It does not expect ?q=uri format… sorry for that…

so now i tried something like:

location ~ /coupons/(.\*) {  
root /home/jjobanputra/code/coupon;  
try\_files $uri $uri/ /coupons/$1;  
index index.php index.html index.htm;  
expires 30d;  
}

but that gives me 500 internal server error…

---

<div class="post-metadata">

**Author:** ![jimishjoban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jimishjoban](https://www.ruby-forum.com/u/jimishjoban)\
**Post date:** [April 29, 2011, 12:35pm UTC](https://www.ruby-forum.com/t/need-help-with-nginx-rewrites-for-sub-domains/206361/7 "2011-04-29T12:35:50Z")

</div>

I think, i figured it out… there were some rules in .htaccess which i  
had to update too… loogs good now…

Thanks guys!!!
