Nginx(十五) proxy_pass和proxy_redirect指令的组合测试

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

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值