Detailed explanation of Nginx proxy_redirect usage

Detailed explanation of Nginx proxy_redirect usage

Today, I encountered a little problem when I was doing nginx reverse proxy for apache. It turned out that the port used by the backend apache was 8080. After passing through the reverse proxy, I used wireshark to capture the packet and found that the value of the location header field was http://192.168.1.154:8080/wuman/. If this is returned to the client, it is definitely not allowed. It looks awkward and also exposes the specific information of apache.

Therefore, nginx's proxy_redirect is used here to specify the modification of the location header field and refresh header field value in the response header returned by the proxy server.

The following is a small excerpt from nginx's configuration document

server {
 listen 80;
 server_name www.boke.com;
 location / {
  proxy_pass http://192.168.1.154:8080;
  proxy_redirect off;
 }
 }

At this point we use curl to view the results

[root@localhost nginx]# curl -I http://www.boke.com/wuman
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 24 Dec 2015 12:02:00 GMT
Content-Type: text/html; charset=iso-8859-1
Connection: keep-alive
Location: http://192.168.1.154:8080/wuman/

Here, location is the response header information with the actual address and port of the backend server. This is not allowed in the actual line, so here we plan to modify the location field in the response header of the proxy server through proxy_redirect and return it to the client.

server {
 listen 80;
 server_name www.boke.com;
 location / {
  proxy_pass http://192.168.1.154:8080;
  proxy_redirect http://192.168.1.154:8080/wuman/ http://www.boke.com/wuman/;
 }

server {
 listen 80;
 server_name www.boke.com;
 location / {
  proxy_pass http://192.168.1.154:8080;
  proxy_redirect ~^http://192.168.1.154:8080(.*) http://www.boke.com$1;
 }

Then curl checks the returned results

[root@localhost nginx]# curl -I http://www.boke.com/wuman
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 24 Dec 2015 12:08:34 GMT
Content-Type: text/html; charset=iso-8859-1
Connection: keep-alive
Location: http://www.boke.com/wuman/

At this point, checking location has become the result we want. At this time, we redirected to our new page through replacement 301

Source:

proxy_redirect

Syntax: proxy_redirect [ default|off|redirect replacement ]

Default value: proxy_redirect default

Use fields: http, server, location

If you need to modify the "Location" and "Refresh" fields in the response header sent from the proxied server, you can use this command to set them.

Assume that the Location field returned by the proxy server is: http://localhost:8000/two/some/uri/

This instruction:

proxy_redirect http://localhost:8000/two/ http://frontend/one/;

Rewrite the Location field to http://frontend/one/some/uri/.

You don't have to write a server name in the Replace field:

proxy_redirect http://localhost:8000/two/ /;

This will use the server's base name and port, even if it's from a port other than 80.

If the "default" parameter is used, it will be determined based on the settings of the location and proxy_pass parameters.

For example, the following two configurations are equivalent:

location / one / {
 proxy_pass http: //upstream:port/two/; 
 proxy_redirect default;
}
location / one / {
 proxy_pass http: //upstream:port/two/; 
 proxy_redirect http: //upstream:port/two/ /one/;
}

There are several variables that can be used in directives:

proxy_redirect http://localhost:8000/ http://$host:$server_port/;

This instruction can sometimes be repeated:

proxy_redirect default;
proxy_redirect http://localhost:8000//; proxy_redirect ; /;

The parameter off will disable all proxy_redirect directives in this field:

proxy_redirect off;
proxy_redirect default;
proxy_redirect http://localhost:8000/ /; proxy_redirect ; /;

Use this directive to add the hostname to relative redirects issued by the proxied server:

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Summary of three methods for implementing grayscale release in Nginx
  • Detailed explanation of Asp.Net Core publishing and deployment (MacOS + Linux + Nginx)
  • A brief analysis of nginScript, the JavaScript capability just released by nginx
  • Detailed explanation of Nginx's control over access volume
  • Setting up a proxy server using nginx
  • Docker container deployment attempt - multi-container communication (node+mongoDB+nginx)
  • Example of how to install nginx to a specified directory
  • How to configure Nginx to support ipv6 under Linux system
  • Detailed explanation of the front-end and back-end deployment tutorial based on Vue and Nginx
  • How to use nginx to simulate canary release

<<:  Vue project realizes paging effect

>>:  jQuery treeview tree structure application

Recommend

Installing Windows Server 2008 operating system on a virtual machine

This article introduces the installation of Windo...

Detailed summary of MySQL and connection-related timeouts

MySQL and connection related timeouts Preface: To...

Detailed explanation of Bind mounts for Docker data storage

Before reading this article, I hope you have a pr...

Detailed introduction to CSS font, text, and list properties

1. Font properties color, specifies the color of ...

How to run commands on a remote Linux system via SSH

Sometimes we may need to run some commands on a r...

SQL implementation LeetCode (185. Top three highest salaries in the department)

[LeetCode] 185. Department Top Three Salaries The...

Use CSS to set the width of INPUT in TD

Recently, when I was using C# to make a Web progra...

Vue.js style layout Flutter business development common skills

Correspondence between flutter and css in shadow ...

Installation steps of docker-ce on Raspberry Pi 4b ubuntu19 server

The Raspberry Pi model is 4b, 1G RAM. The system ...

Solve the Linux Tensorflow2.0 installation problem

conda update conda pip install tf-nightly-gpu-2.0...

Implementation of CSS child element selection parent element

Usually a CSS selector selects from top to bottom...

How to verify whether MySQL is installed successfully

After MySQL is installed, you can verify whether ...

How to get the intersection/difference/union of two sets in mysql

Common scenarios of MySQL: getting the intersecti...

Detailed tutorial on deploying Hadoop cluster using Docker

Recently, I want to build a hadoop test cluster i...