I have a working website (http://www.example.com
) that (uses PHP-FPM) for which I want to redirect all incoming trafic to an URL on another website (ttps://www.other-example.com/foo.html
) except one specific path (/api
).
Here is the Nginx configuration I tried (put the PHP handling stuff in a dedicated location
for /api
and a 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;
}
}
But all my requests (http://www.example.com
, http://www.example.com/foo
and http://www.example.com/api
) gets a HTTP/1.1 301 Moved Permanently
with Location: https://www.other-example.com/foo.html
.
I know the location ~ ^/api(?:/(.*))?$
works because if I uncomment the return 418;
I get 418 responses for http://www.example.com/api
requests but 301 for the other requests.