Nginx反向代理配置文件参数详解请参考 Nginx(十三) 配置文件详解 - 反向代理(超详细)
测试1:proxy_redirect http://127.0.0.1:8080/three/ http://www.read*******l.cn:8688/four/;
http {
server {
listen 8688;
server_name www.read*******l.cn;
location /one/ {
proxy_pass http://127.0.0.1:8080/two/;
proxy_redirect http://127.0.0.1:8080/three/ http://www.read*******l.cn:8688/four/;
}
}
server {
listen 8080;
server_name www.read*******l.cn;
location /two/ {
return 301 http://127.0.0.1:8080/three/world;
}
}
}
客户端发送请求:http://www.read*******l.cn:8688/one/hello
客户端最终请求:http://www.read*******l.cn:8688/four/world
测试2:proxy_redirect http://127.0.0.1:8080/three/ /four/;
http {
server {
listen 8688;
server_name www.read*******l.cn;
location /one/ {
proxy_pass http://127.0.0.1:8080/two/;
proxy_redirect http://127.0.0.1:8080/three/ /four/;
}
}
server {
listen 8080;
server_name www.read*******l.cn;
location /two/ {
return 301 http://127.0.0.1:8080/three/world;
}
}
}
客户端发送请求:http://www.read*******l.cn:8688/one/hello
客户端最终请求:http://www.read*******l.cn:8688/four/world
proxy_redirect http://127.0.0.1:8080/three/ /four/;
相当于
proxy_redirect http://127.0.0.1:8080/three/ http://www.read*******l.cn:8688/four/;
测试3:proxy_redirect http://127.0.0.1:8688/three/ /;
http {
server {
listen 8688;
server_name www.read*******l.cn;
location /one/ {
proxy_pass http://127.0.0.1:8080/two/;
proxy_redirect http://127.0.0.1:8080/three/ /;
}
}
server {
listen 8080;
server_name www.read*******l.cn;
location /two/ {
return 301 http://127.0.0.1:8080/three/world;
}
}
}
客户端发送请求:http://www.read*******l.cn:8688/one/hello
客户端最终请求:http://www.read*******l.cn:8688/world
测试4:proxy_redirect default;
http {
server {
listen 8688;
server_name www.read*******l.cn;
location /one/ {
proxy_pass http://127.0.0.1:8080/two/;
proxy_redirect default;
}
}
server {
listen 8080;
server_name www.read*******l.cn;
location /two/ {
return 301 http://127.0.0.1:8080/three/world;
}
}
}
客户端发送请求:http://www.read*******l.cn:8688/one/hello
客户端最终请求:http://127.0.0.1:8080/three/world
测试5:proxy_redirect default;
http {
server {
listen 8688;
server_name www.read*******l.cn;
location /one/ {
proxy_pass http://127.0.0.1:8080/two/;
proxy_redirect default;
}
}
server {
listen 8080;
server_name www.read*******l.cn;
location /two/ {
return 301 http://127.0.0.1:8080/two/world;
}
}
}
客户端发送请求:http://www.read*******l.cn:8688/one/hello
客户端最终请求:http://www.read*******l.cn:8688/one/world
根据测试4和测试5的结果可得出以下结论:
当proxy_redirect配置为default时,如果proxy_pass 的URL与响应头中Location字段的URL 部分内容完全匹配,则用server_name、listen port和当前location的URI组合替换掉Location中与proxy_pass相匹配的部分。
proxy_pass相当于是 proxy_redirect redirect replacement 中的 redirect 参数,
server_name + listen port + 当前location的URI 组合起来相当于 proxy_redirect redirect replacement 中的 replacement 参数。
测试6:proxy_redirect off;
http {
server {
listen 8688;
server_name www.read*******l.cn;
location /one/ {
proxy_pass http://127.0.0.1:8080/two/;
proxy_redirect off;
}
}
server {
listen 8080;
server_name www.read*******l.cn;
location /two/ {
return 301 http://127.0.0.1:8080/three/world;
}
}
}
客户端发送请求:http://www.read*******l.cn:8688/one/hello
客户端最终请求:http://127.0.0.1:8080/three/world
测试7:proxy_redirect off;
http {
server {
listen 8688;
server_name www.read*******l.cn;
location /one/ {
proxy_pass http://127.0.0.1:8080/two/;
proxy_redirect off;
}
}
server {
listen 8080;
server_name www.read*******l.cn;
location /two/ {
return 301 http://127.0.0.1:8080/two/world; # 与测试5的区别之处
}
}
}
客户端发送请求:http://www.read*******l.cn:8688/one/hello
客户端最终请求:http://127.0.0.1:8080/two/world