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


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

我正在尝试通过fastcgi将运行symfony框架的php网站迁移到nginx和php。

使用http://wiki.nginx.org/中的Symfony howto可以很好地工作,但是我在使用自定义重写规则时遇到了麻烦。

我的目标是将形式为/aaaa的url重写为/view/shortcut/aaaa。然后该请求应该由php和symfony处理。

旧的apache重写规则:

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

Nginx rules i have try:

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

它们都被发送到fastcgi,但request_uri似乎仍然是/aaaa,因为我得到这个错误:

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

我也试过使用try_files没有任何运气。请建议。

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

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

$uri的值可能会在请求处理过程中发生变化,例如在进行内部重定向或使用索引文件时。因此,如果您想在REQUEST_URI值传递给PHP之前修复它,只需将$request_uri更改为fastcgi_params中的$uri?$args

这是Nginx首席开发者Igor Sysoev在官方邮件列表中推荐的方法。