用Nginx和PHP重写fastcgi仍然将旧的request_uri发送到后端(php和symfony)

blk*_*kmr 5 php symfony1 fastcgi nginx url-rewriting

我试图将运行symfony框架的php网站迁移到nginx和php over fastcgi.

http://wiki.nginx.org/开始使用Symfony howto一切正常,但我遇到了自定义重写规则的问题.

我的目标是将/ aaaa形式的URL重写为/ view/shorthand/aaaa.然后应该由php和symfony对请求进行处理.

旧的apache重写规则:

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

我尝试过的Nginx规则:

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

他们都被发送到fastcgi但是request_uri似乎仍然是/ aaaa,因为我收到此错误:

FastCGI sent in stderr: "Action "aaaa/index" does not exist" while reading response header from upstream
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用try_files而没有任何运气.请指教.

小智 12

问题在于默认的FastCGI变量集(fastcgi_params配置文件),其中REQUEST_URI等于nginx的$request_uri.但是如果你仔细阅读Nginx文档,你会发现$request_uri$uri变量之间的区别.

  • $request_uri =完整的原始请求URI(带参数)
  • $uri =请求中的当前URI,规范化

$uri在请求处理期间,值可能会发生变化,例如在执行内部重定向或使用索引文件时.所以,如果你想修正REQUEST_URI值之前,它传递给PHP,只是改变$request_uri$uri?$argsfastcgi_params.

这是Nginx首席开发人员Igor Sysoev 在官方邮件列表中推荐的方式.