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

你可能感兴趣的:(nginx)