6

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.

blkmr
  • 81
  • 1
  • 7

1 Answers1

14

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.

Anne
  • 26,765
  • 9
  • 65
  • 71
Dopamine
  • 185
  • 1
  • 11