nginx 之 proxy_redirect详解【转】

proxy_redirect

语法:proxy_redirect [ default|off|redirect replacement ] 
默认值:proxy_redirect default 
使用字段:http, server, location 
如果需要修改从被代理服务器传来的应答头中的"Location"和"Refresh"字段,可以用这个指令设置。
假设被代理服务器返回Location字段为: http://localhost:8000/two/some/uri/
这个指令:
proxy_redirect http://localhost:8000/two/ http://frontend/one/;
将Location字段重写为http://frontend/one/some/uri/。
在代替的字段中可以不写服务器名:
proxy_redirect http://localhost:8000/two/ /;
这样就使用服务器的基本名称和端口,即使它来自非80端口。
如果使用“default”参数,将根据location和proxy_pass参数的设置来决定。
例如下列两个配置等效:
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/;
}
在指令中可以使用一些变量:
proxy_redirect http://localhost:8000/ http://$host:$server_port/;
这个指令有时可以重复:
proxy_redirect default;
proxy_redirect http://localhost:8000/ /;
proxy_redirect http://www.example.com/ /;
参数off将在这个字段中禁止所有的proxy_redirect指令:
proxy_redirect off;
proxy_redirect default;
proxy_redirect http://localhost:8000/ /;
proxy_redirect http://www.example.com/ /;
利用这个指令可以为被代理服务器发出的相对重定向增加主机名:
proxy_redirect / /;
 
将被代理服务器发出的重定向http协议的location改为https协议:proxy_redirect ~^http://([^:]+)(:\d+)?(.*)$ https://$1$2$3;
 
 
nginx的proxy_redirect用法总结:

访问测试地址:hostA/app/a.jsp

测试代理地址:proxy_pass hostB:8080/test_app/;

用法对比:

proxy_redirect hostB:8080/ /app/

跳转到:hostA/app/test_app/b.jsp

proxy_redirect hostB:8080/ /

跳转到:hostA/test_app/b.jsp

proxy_redirect / /;

跳转到:hostB:8080/test_app/b.jsp

proxy_redirect default;

跳转到:hostA/app/b.jsp

proxy_redirect hostB:8080/test_app/ /app/;(与default等同)

跳转到:hostA/app/b.jsp

proxy_redirect hostB:8080/test_app/ hostA/app/;

跳转到:hostA/app/b.jsp

proxy_redirect(不加)

跳转到:hostA/app/b.jsp

proxy_redirect off;

跳转到:hostB:8080/test_app/b.jsp

proxy_redirect hostB:8080/test_app/ /;

跳转到:hostA/b.jsp

 

 

转自

nginx 之 proxy_redirect详解 - 苍青浪 - 博客园
https://www.cnblogs.com/cangqinglang/p/10265367.html

 

nginx的proxy_redirect如何使用解决重定向失败-百度经验
https://jingyan.baidu.com/article/49711c61aac2dcfa441b7c14.html

posted @ 2022-06-20 17:17  paul_hch  阅读(8565)  评论(0编辑  收藏  举报