Eu tenho um site em funcionamento ( http://www.example.com
) que (usa PHP-FPM) para o qual desejo redirecionar todo o tráfego de entrada para uma URL em outro site ( ttps://www.other-example.com/foo.html
) exceto um caminho específico ( /api
).
Aqui está a configuração do Nginx que tentei (coloque o material de manipulação do PHP em um dedicado location
for /api
e um 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;
}
}
Mas todos os meus pedidos ( http://www.example.com
, http://www.example.com/foo
e http://www.example.com/api
) recebem um HTTP/1.1 301 Moved Permanently
com Location: https://www.other-example.com/foo.html
.
Conheço o location ~ ^/api(?:/(.*))?$
trabalho porque se eu descomentar return 418;
recebo 418 respostas para http://www.example.com/api
solicitações, mas 301 para as outras solicitações.