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

How do I replace upstream “Location: http://server1.cyberciti.biz:8080/app/” http/301 response-header with “Location: http://www.cyberciti.biz/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:

ADVERTISEMENTS

proxy_redirect redirect replacement

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

proxy_redirect http://server1.cyberciti.biz:8080/app/ http://www.cyberciti.biz/app/;

will rewrite this line in the form Location: http://www.cyberciti.biz/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.cyberciti.biz:8080/app/
OR
$ curl http://server1.cyberciti.biz/app/

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

proxy_redirect   http://server1.cyberciti.biz:8070/    http://$host:$server_port/;

The parameter off forbids all proxy_redirect directives:

proxy_redirect off;
References:
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
1 comment… add one
  • ceooph Mar 12, 2013 @ 11:32

    Hi,
    Thanks for ALL your articles, about nginx and [lu]nix ;-)
    I’ve a question about nginx. And I hope you can answer it ;-)
    We do reverse proxy to access from internet some documentation filtering by ip address. These documentations are hosted on commercials portals.
    For 1 base we are often redirect between 2 or 3 (sometimes 6) servers.
    And we have access to more than 50 bases !
    Do you know if with nginx it’s possible to do proxy_pass (and proxy_redirect) to these server one 1 virtual name ?
    I try it with different location for each server but it’s doesn’t work.

    Thanks in advance for your help. You can contact me at ceooph@gmail.com
    And continue your blog ! When I search documentation about linux and I find an article on your site, I know that I’ll have to response ;-)

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.