Nginx – 用重写的URL填充REQUEST_URI

我有一个Nginxconfiguration,用于一个新的PHP应用程序,它具有与另一个传统PHP应用程序相同的function,但具有不同的URL。

我想保留旧应用程序的path,用/pagereplace/foopath前缀,并用/page/otherBarreplace特殊path/foo/bar

  # legacy support location ~ ^/foo/bar { rewrite /foo/bar /page/otherBar$1 last; } # How to rewrite all other pages starting with "/foo" ? # END legacy support location / { # try to serve file directly, fallback to front controller try_files $uri /index.php$is_args$args; } location ~ ^/index\.php(/|$) { proxy_read_timeout 300; include fastcgi_params; fastcgi_split_path_info ^(.+\.php)(/.*)$; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www/$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; } 

这种方法不起作用,因为$request_uri被传递给fastcgi_params包含文件中的REQUEST_URI ,仍然包含/foo/bar

我已经尝试将REQUEST_URI设置为$fastcgi_path_info但是对于所有未被重写的URL都是失败的,因为它是空的。 $uri也不起作用,因为它只包含/index.php?

是否有包含重写path的第三个位置configuration的variables?

$request_uri具有原始URI的值, $uri具有最终URI的值。 您可以使用set指令从location /块内保存$uri的快照,稍后使用它来生成REQUEST_URI参数。

喜欢这个:

 location / { set $save_uri $uri; try_files $uri /index.php$is_args$args; } location ~ ^/index\.php(/|$) { include fastcgi_params; fastcgi_param REQUEST_URI $save_uri; ... } 

请参阅此文档了解更多