我正在尝试通过 fastcgi 将运行 symfony 框架的 php 网站迁移到 nginx 和 php。
使用 http://wiki.nginx.org/ 中的 Symfony howto 一切正常但我遇到了自定义重写规则的问题。
我的目标是将形式为 /aaaa 的 url 重写为 /view/shorthand/aaaa。然后请求应该由 php 和 symfony 处理。
旧的 apache 重写规则:
RewriteRule ^([0-9a-f]+)$ index.php/view/shorthand/$1 [L]
我试过的Nginx规则:
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 documentation ,您会注意到 $request_uri
和 $uri
变量之间的区别。
$request_uri
= 完整的原始请求 URI(带参数)$uri
= 请求中的当前 URI,规范化
$uri
的值可能会在请求处理期间发生变化,例如在进行内部重定向或使用索引文件时。所以,如果你想在传递给 PHP 之前修复 REQUEST_URI
值,只需在 中将
。 $request_uri
更改为 $uri?$args
fastcgi_params
这就是 Nginx 首席开发人员 Igor Sysoev 的方式 recommends in the official mailing list .
关于php - 用 Nginx 和 PHP 重写 fastcgi 仍然发送旧的 request_uri 到后端(php 和 symfony),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8339183/