nginx代理的规则

发布时间:2024年01月19日

nginx配置proxy_pass URL末尾加与不加/(斜线)的区别_nginx proxy加/和不加-CSDN博客

说明
curl在终端进行访问,可以避免浏览器缓存影响测试结果
测试结果在本地通过一个虚拟主机8087的acces_log获取
每一条curl下面为对应的测试结果
测试
1
location /tobaidu {
? ? proxy_pass http://127.0.0.1:8087;
}
1
2
3
curl http://127.0.0.1/tobaidu

http://127.0.0.1:8087/tobaidu
1
curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/tobaidu/
1
curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087/tobaidu/xxxx
1
2
location /tobaidu {
? ? proxy_pass http://127.0.0.1:8087/define;
}
1
2
3
curl http://127.0.0.1/tobaidu

http://127.0.0.1:8087/define
1
curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/define/
1
curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087/define/xxxx
1
3
location /tobaidu/ {
? ? proxy_pass http://127.0.0.1:8087;
}
1
2
3
curl http://127.0.0.1/tobaidu

重定向到http://127.0.0.1/tobaidu/
1
curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/tobaidu/
1
curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087/tobaidu/xxxx
1
4
location /tobaidu/ {
? ? proxy_pass http://127.0.0.1:8087/define;
}
1
2
3
curl http://127.0.0.1/tobaidu

重定向到http://127.0.0.1/tobaidu/
1
curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/define
1
curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087/definexxxx
1
5
location /tobaidu {
? ? proxy_pass http://127.0.0.1:8087/;
}
1
2
3
curl http://127.0.0.1/tobaidu

http://127.0.0.1:8087/
1
curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087//
1
curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087//xxxx
1
6
location /tobaidu {
? ? proxy_pass http://127.0.0.1:8087/define/;
}
1
2
3
curl http://127.0.0.1/tobaidu

http://127.0.0.1:8087/define/
1
curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/define//
1
curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087/define//xxxx
1
7
location /tobaidu/ {
? ? proxy_pass http://127.0.0.1:8087/;
}

1
2
3
curl http://127.0.0.1/tobaidu

重定向到http://127.0.0.1/tobaidu/
1
curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/
1
curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087/xxxx
1
8
location /tobaidu/ {
? ? proxy_pass http://127.0.0.1:8087/define/;
}
1
2
3
curl http://127.0.0.1/tobaidu

重定向到http://127.0.0.1/tobaidu/
1
curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/define/
1
curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087/define/xxxx
1
结论
URL符合 protocol://ip:port 同时结尾不加/,则nginx会代理匹配路径部分,否则不代理匹配路径,同时自动添加不匹配路径”部分”,比如/tobaidu/xxxx的/xxxx部分

测试7为常用的反向代理
?

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