Need help with nginx rewrites (for sub-domains)

Edho P Arief edhoprima at gmail.com
Fri Apr 29 07:20:11 MSD 2011


On Fri, Apr 29, 2011 at 6:11 AM, Jimish Jobanputra <lists at ruby-forum.com> wrote:
> 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
>
> https://gist.github.com/946479
>
> 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 ;)
>

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



More information about the nginx mailing list