Nginx proxy_redirect: Change response-header Location and Refresh in the response of the server

In Nginx, you can use the proxy_redirect directive to change the Location and Refresh headers in the response from a server.

Here is an example configuration:

server {
listen 80;
server_name example.com;

location / {
proxy_pass http://backend_server;
proxy_redirect default;
proxy_redirect https://backend_server https://example.com;
}
}

In this example, the proxy_redirect default; directive sets the default behavior for proxy_redirect to replace the scheme and host in the Location and Refresh headers with the values specified by proxy_pass.

The second proxy_redirect directive https://backend_server https://example.com; specifically replaces https://backend_server with https://example.com. This is useful when the backend server returns URLs that are not correct or need to be changed for some reason.

Leave a Comment