Need help with nginx rewrites (for sub-domains)

Hello,

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

I have a Rails application, say, http://example.com and now I want to
run a PHP app on 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

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

Same goes with 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 :wink:

On Fri, Apr 29, 2011 at 6:11 AM, Jimish Jobanputra
[email protected] 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 :wink:

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;
}

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 then it takes me to
index page itself… I tried couple of things, but no luck yet…

On Fri, Apr 29, 2011 at 5:01 PM, Jimish Jobanputra
[email protected] 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 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;
}

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 :wink:

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.

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…

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

Thanks guys!!!