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

2024-01-02 05:59:17

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

文章来源:https://blog.csdn.net/ShenDaiSun/article/details/135330618
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。