I am trying to do a permanent redirect from a subpath location on a original server (https://my-server1.com/api/old/subpath
) to the root of an external host while keeping the subpath (https://my-server2.com/subpath
), the HTTP method (generally the request are POST requests) and the headers (the headers include authentication data).
Following several similar issues on SO, I have been trying the following:
1.
location ^/api/old(.*)$ {
rewrite $scheme://my-server2.com$1 permanent;
}
...but I get a 404 when doing the POST request
2.
location /api/old {
rewrite ^/api/old(.*)$ $scheme://my-server2.com$1 permanent;
5
...but I get a 405 when doing the POST request, I believe because the POST request is changed into a GET
3.
location ~ ^/api/old(.*)$ {
return 308 $scheme://my-server2.com$1;
}
...but I get a 403 when doing the POST request, I believe the method is ok, but the auth headers is not forwarded
4.
location ~ ^/api/old(.*)$ {
proxy_pass $scheme://my-server2.com$1;
}
...but I get a 502 (not idea why exactly).
Which one is the right method to do so, and what am I missing here?
I found how to have the method #4 working and why it produced a 502. It needed a DNS resolver to work. One can use OpenDNS (208.67.222.222) as the DNS resolver, for example.
The directive would become:
location ~ ^/api/old(.*)$ {
proxy_pass $scheme://my-server2.com$1;
resolver 208.67.222.222;
}
This is not ideal though, as this is 100% transparent, and I still wonder if there a way to inform the client of the permanent redirect at the same time.