Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use proxy_redirect in Nginx

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to use proxy_redirect in Nginx? Many novices are not very clear about this. In order to help you solve this problem, the following small series will explain it in detail for everyone. Those who have this need can come to learn. I hope you can gain something.

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

So we're going to curl up and we're going to see

[root@localhost nginx]# curl -I http://www.boke.com/wumanHTTP/1.1 301 Moved PermanentlyServer: nginxDate: Thu, 24 Dec 2015 12:02:00 GMTContent-Type: text/html; charset=iso-8859-1Connection: keep-aliveLocation: 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 on the actual line. Therefore, we intend 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; }

Curl View returns results

[root@localhost nginx]# curl -I http://www.boke.com/wumanHTTP/1.1 301 Moved PermanentlyServer: nginxDate: Thu, 24 Dec 2015 12:08:34 GMTContent-Type: text/html; charset=iso-8859-1Connection: keep-aliveLocation: http://www.boke.com/wuman/

Now the location has become the result we want. Redirect to our new page via replacement 301

Source:

proxy_redirect

Syntax: proxy_redirect [ default| off| redirect replacement ]

Default: 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 proxy server, you can set it with this command.

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

This command:

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

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

The server name may not be written in the substitute field:

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

This uses the base name and port of the server, even if it comes from a port other than port 80.

If the "default" parameter is used, it is determined by 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/;}

Some variables can be used in instructions:

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

This command can sometimes be repeated:

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

The off parameter disables all proxy_redirect directives in this field:

proxy_redirect off;proxy_redirect default;proxy_redirect http://localhost:8000//; proxy_redirect ; /; Is it helpful to read all of the above? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report