Nginx - Rewrites help.

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

     location /coupons/ {
          alias /home/jjobanputra/code/coupon/upload/;
          index index.php index.html index.htm;
          if (-e $request_filename) {
            expires 30d;
            break;
          }
          rewrite /coupons/(.+)$ /coupons/index.php?q=$1;
        }

        location = /coupons {
          try_files $uri $uri/ @coupons;
        }

        location @coupons {
          include         fastcgi_params;
          fastcgi_pass 127.0.0.1:53217;
          fastcgi_split_path_info ^(/coupons)(/.*)$;
          fastcgi_param SCRIPT_FILENAME /home/jjobanputra/code/coupon/upload/index.php;
          fastcgi_param PATH_INFO $fastcgi_path_info;
        }

        location ~ /coupons/?.*\.php$ {
          if ($fastcgi_script_name ~ /coupons/?(.*)$) {
            set $valid_fastcgi_script_name /$1;
          }
          fastcgi_pass 127.0.0.1:53217;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME /home/jjobanputra/code/coupon/upload/$valid_fastcgi_script_name;
          include /opt/nginx/conf/fastcgi.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 ;)

1 Reply

Yeah. That config is convoluted and flawed. So much so, that I'm not quite sure what it is you're trying to do.

Could you describe what types of files you're serving from the /coupon? How many .php scripts? Are you looking to list directories? Are there actually any index.html files or should everything that isn't an existing static file be passed to index.php?

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct