chi*_*org 3 rewrite nginx php-fpm
我有一个 Nginx 配置,用于新的 PHP 应用程序,该应用程序具有与另一个旧 PHP 应用程序相同的功能,但 URL 不同。
我想保留旧的应用程序的路径,取代了/foo与路径前缀/page和更换特殊的路径/foo/bar有/page/otherBar:
# 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;
}
Run Code Online (Sandbox Code Playgroud)
这种方法不起作用,因为在包含文件中$request_uri传递给REQUEST_URI的fastcgi_params仍然包含/foo/bar.
我试过设置REQUEST_URI为,$fastcgi_path_info但是对于所有未重写的 URL 都失败了,因为它当时是空的。$uri也不起作用,因为它只包含/index.php?
包含重写路径的第三个位置配置是否有任何变量?
$request_uri具有原始 URI$uri的值和最终 URI 的值。您可以使用该set指令$uri从location /块内部保存快照,并在以后使用它来生成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;
...
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅此文档。