我有一个工作网站(http://www.example.com
)(使用 PHP-FPM),我想将所有传入流量重定向到另一个网站(ttps://www.other-example.com/foo.html
)上的 URL,但一个特定路径()除外/api
。
这是我尝试过的 Nginx 配置(将 PHP 处理的东西放在专用location
的 for/api
和return 301
on 中location /
):
upstream php {
server unix:/var/run/php/php7.4-fpm.sock;
}
server {
listen 80;
listen [::]:80;
server_name www.example.com;
root /var/www/www.example.com;
index index.php;
location ~ ^/api(?:/(.*))?$ {
#return 418;
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
#include snippets/fastcgi-php.conf;
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;
include fastcgi_params;
fastcgi_pass php;
}
}
location / {
return 301 https://www.other-example.com/foo.html;
}
}
但是我所有的请求(http://www.example.com
和http://www.example.com/foo
)http://www.example.com/api
都会得到一个HTTP/1.1 301 Moved Permanently
with Location: https://www.other-example.com/foo.html
。
我知道这些location ~ ^/api(?:/(.*))?$
作品,因为如果我取消注释,return 418;
我会收到 418http://www.example.com/api
个请求响应,但其他请求会收到 301 个响应。