Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite with Nginx and PHP fastcgi still sends old request_uri to backend (php and symfony)

I am trying to migrate a php-website running the symfony framework to nginx and php over fastcgi.

It all works well usining the Symfony howto from http://wiki.nginx.org/ but I run into trouble with a custom rewrite rule.

My goal is to rewrite urls of of the form /aaaa to /view/shorthand/aaaa. The request should then be handeled by php and symfony.

Old apache rewrite rule:

RewriteRule ^([0-9a-f]+)$ index.php/view/shorthand/$1 [L]

Nginx rules i have tried:

rewrite ^/([0-9a-f]+)$ /view/shorthand/$1 break;
rewrite ^/([0-9a-f]+)$ /index.php/view/shorthand/$1 break;

They all get sent to fastcgi but the request_uri still seems to be /aaaa since I get this error:

FastCGI sent in stderr: "Action "aaaa/index" does not exist" while reading response header from upstream

I have also tried using try_files without any luck. Please advice.

like image 601
blkmr Avatar asked Dec 01 '11 09:12

blkmr


1 Answers

The problem is in default FastCGI variables set (fastcgi_params config file), where REQUEST_URI is equal to nginx's $request_uri. But if you carefully read Nginx documentation, you will notice the difference between $request_uri and $uri variables.

  • $request_uri = full original request URI (with arguments)
  • $uri = current URI in request, normalized

The value of $uri may change during request processing, e.g. when doing internal redirects, or when using index files. So, if you want to fix REQUEST_URI value before it passed to PHP, just change $request_uri to $uri?$args in fastcgi_params.

It's the way Nginx lead developer Igor Sysoev recommends in the official mailing list.

like image 153
Dopamine Avatar answered Oct 14 '22 03:10

Dopamine