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

How do I replace upstream “Location: http://server1.sxi.io:8080/app/” http/301 response-header with “Location: http://sxi.io/app/” in the response of the proxied server using nginx?

Nginx provides proxy_redirect directive which can be used in http, server, or location context. The syntax is:

proxy_redirect redirect replacement

In this example, the proxied server (upstream Apache or Lighttpd) returned line Location: http://server1.sxi.io:8080/app/. The following directive in nginx.conf:

proxy_redirect http://server1.sxi.io:8080/app/ http://sxi.io/app/;

will rewrite this line in the form Location: http://sxi.io/app/. Save and close the file. Reload the nginx server
# service nginx reload
OR
# /usr/local/nginx/sbin/nginx -s reload
You can verify this with the curl command:
$ curl -I http://server1.sxi.io:8080/app/
OR
$ curl http://server1.sxi.io/app/

In the replace line, it is possible to use variables as follows:

proxy_redirect   http://server1.sxi.io:8070/    http://$host:$server_port/;

The parameter off forbids all proxy_redirect directives:

proxy_redirect off;
References:

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.