nginx配置proxy_pass URL末尾加与不加/(斜线)的区别

说明

  • curl在终端进行访问,可以避免浏览器缓存影响测试结果
  • 测试结果在本地通过一个虚拟主机8087的acces_log获取
  • 每一条curl下面为对应的测试结果

测试

1

location /tobaidu {
    proxy_pass http://127.0.0.1:8087;
}

curl http://127.0.0.1/tobaidu

http://127.0.0.1:8087/tobaidu

curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/tobaidu/

curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087/tobaidu/xxxx

2

location /tobaidu {
    proxy_pass http://127.0.0.1:8087/define;
}

curl http://127.0.0.1/tobaidu

http://127.0.0.1:8087/define

curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/define/

curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087/define/xxxx

3

location /tobaidu/ {
    proxy_pass http://127.0.0.1:8087;
}

curl http://127.0.0.1/tobaidu

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

curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/tobaidu/

curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087/tobaidu/xxxx

4

location /tobaidu/ {
    proxy_pass http://127.0.0.1:8087/define;
}

curl http://127.0.0.1/tobaidu

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

curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/define

curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087/definexxxx

5

location /tobaidu {
    proxy_pass http://127.0.0.1:8087/;
}

curl http://127.0.0.1/tobaidu

http://127.0.0.1:8087/

curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087//

curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087//xxxx

6

location /tobaidu {
    proxy_pass http://127.0.0.1:8087/define/;
}

curl http://127.0.0.1/tobaidu

http://127.0.0.1:8087/define/

curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/define//

curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087/define//xxxx

7

location /tobaidu/ {
    proxy_pass http://127.0.0.1:8087/;
}

curl http://127.0.0.1/tobaidu

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

curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/

curl http://127.0.0.1/tobaidu/xxxx

http://127.0.0.1:8087/xxxx

8

location /tobaidu/ {
    proxy_pass http://127.0.0.1:8087/define/;
}

curl http://127.0.0.1/tobaidu

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

curl http://127.0.0.1/tobaidu/

http://127.0.0.1:8087/define/

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部分

  2. 测试7为常用的反向代理

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值