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为常用的反向代理
?