Nginx 中location、proxy_pass是否斜杠不同结果、区别
博主: haitianisgood
原博客网址:
https://blog.csdn.net/haitianisgood/article/details/112942178
Nginx中location、proxy_pass中带不带斜杠,反代的URL可能是不同的
下面通过例子来说明一下:
条件
服务器 10.0.0.1 (安装nginx)
服务器 10.0.0.2 (反代的后端服务器)
http请求逻辑
client -> 10.0.0.1(nginx) -> 10.0.0.2(后端服务器)
请求URL:
http://10.0.0.1/test/api
1. proxy_pass不带反斜杠,location是否带反斜杠区别
示例1:
location /test/ {
proxy_pass http://10.0.0.2;
}
服务器10.0.0.2 收到的请求: /test/api
示例2:
location /test {
proxy_pass http://10.0.0.2
}
服务器10.0.0.2 收到的请求: /test/api
2. proxy_pass带反斜杠,location是否带反斜杠区别
示例3:
location /test/ {
proxy_pass http://10.0.0.2/;
}
服务器10.0.0.2收到的请求: /api
示例4:
location /test {
proxy_pass http://10.0.0.2/;
}
服务器10.0.0.2收到的请求: //api
3. proxy_pass带其他URI part并带反斜杠,location是否带反斜杠区别
示例5:
location /test/ {
proxy_pass http://10.0.0.2/ng/;
}
服务器10.0.0.2收到的请求: /ng/api
示例6:
location /test {
proxy_pass http://10.0.0.2/ng/;
}
服务器10.0.0.2收到的请求: /ng//api
4. proxy_pass带其他URI part不带反斜杠,location是否带反斜杠区别
示例7:
location /test/ {
proxy_pass http://10.0.0.2/ng;
}
服务器10.0.0.2收到的请求: /ngapi
示例8:
location /test {
proxy_pass http://10.0.0.2/ng;
}
服务器10.0.0.2收到的请求: /ng/api
总结
- proxy_pass不带斜杠,location带不带斜杠效果一样
- proxy_pass带斜杠,location是否带斜杠效果不一样
- proxy_pass带其他URI part,location是否带斜杠效果不一样
- proxy_pass带其他URI part并带斜杠,location是否带斜杠效果不一样
个人理解:proxy_pass带斜杠和带其他URI part,location部分会被proxy_pass后斜杠或其他URI part替换修改。 HTTP的URL被替换内容就是location内容,最终形成新的反代后的URL传给后面服务器