nginx简单配置四种携带/时的拼接关系

一、代理静态文件

1、 当 location 尾部有 /,且代理地址尾部也有 / 时:(常用)

location /test11/ {
	root      /usr/local/nginx/html/;
}
则访问 http://ip/test11/aaa,实际访问的是/usr/local/nginx/html/aaa

2、 当 location 尾部有 /,代理地址尾部没有 / 时:(几乎不用)

location /test10/ {
	root      /usr/local/nginx/html;
}
则访问 http://ip/test10/aaa,实际访问的是/usr/local/nginx/html/aaa

3、 当 location 尾部没有 /,代理地址尾部有 / 时:(和4一致)

location /test01 {
	root      /usr/local/nginx/html/;
}
则访问 http://ip/test01/aaa,实际访问的是/usr/local/nginx/html/test01/aaa

4、 当 location 尾部没有 /,代理地址尾部没有 / 时:(常用)

location /test00 {
	root      /usr/local/nginx/html;
}
则访问 http://ip/test00/aaa,实际访问的是/usr/local/nginx/html/test00/aaa
则访问 http://ip/test00xx/aaa,实际访问的是/usr/local/nginx/html/test00xx/aaa

二、代理地址(尾部跟一个与location相同的路径)

下图是四种不同的情况下的映射关系截图

1、 当 location 尾部有 /,且代理地址尾部也有 / 时:(常用)

location /testproxy11/ {
	proxy_pass      http://127.0.0.1:10000/testproxy11/;
}
则访问 http://ip/testproxy11/aaa,实际访问的是http://127.0.0.1:10000/testproxy11/aaa

2、 当 location 尾部有 /,代理地址尾部没有 / 时:(几乎不用)

location /testproxy10/ {
	proxy_pass      http://127.0.0.1:10000/testproxy10;
}
则访问 http://ip/testproxy10/aaa,实际访问的是http://127.0.0.1:10000/testproxy10aaa

3、 当 location 尾部没有 /,代理地址尾部有 / 时:

location /testproxy01 {
	proxy_pass      http://127.0.0.1:10000//testproxy01/;
}
则访问 http://ip/testproxy01/aaa,实际访问的是http://127.0.0.1:10000/testproxy01//aaa(代理后出现的是双//)
则访问 http://ip/testproxy01aaa,实际访问的是http://127.0.0.1:10000/testproxy01/aaa

4、 当 location 尾部没有 /,代理地址尾部没有 / 时:(常用)

location /testproxy00 {
	proxy_pass      http://127.0.0.1:10000/testproxy00;
}
则访问 http://ip/testproxy00/aaa,实际访问的是http://127.0.0.1:10000/testproxy00/aaa
  访问 http://ip/testproxy00aaa,实际访问的是http://127.0.0.1:10000/testproxy00aaa

三、下面是关于代理地址后缀与location不同时的映射(常用)

这是四种不同的配置

location /testproxync11/ {
  proxy_pass http://127.0.0.1:10000/testproxy/;
}
location /testproxync10/ {
  proxy_pass http://127.0.0.1:10000/testproxy;
}
location /testproxync00 {
  proxy_pass http://127.0.0.1:10000/testproxy;
}
location /testproxync01 {
  proxy_pass http://127.0.0.1:10000/testproxy/;
}

上面四种配置分别按照不同方式访问后会是下面的结果

request: "GET /testproxync11/aaa HTTP/1.1", upstream: "http://127.0.0.1:10000/testproxy/aaa"
request: "GET /testproxync10/aaa HTTP/1.1", upstream: "http://127.0.0.1:10000/testproxyaaa"
request: "GET /testproxync00/aaa HTTP/1.1", upstream: "http://127.0.0.1:10000/testproxy/aaa"
request: "GET /testproxync00aaa HTTP/1.1",  upstream: "http://127.0.0.1:10000/testproxyaaa"
request: "GET /testproxync01aaa HTTP/1.1",  upstream: "http://127.0.0.1:10000/testproxy/aaa"
request: "GET /testproxync01/aaa HTTP/1.1", upstream: "http://127.0.0.1:10000/testproxy//aaa"

四、下面是关于代理地址无自定义后缀时的映射(常用)

location /testproxynp11/ {
  proxy_pass http://127.0.0.1:10000/;
}
location /testproxynp10/ {
  proxy_pass http://127.0.0.1:10000;
}
location /testproxynp00 {
  proxy_pass http://127.0.0.1:10000;
}
location /testproxynp01 {
  proxy_pass http://127.0.0.1:10000/;
}

上面四种配置分别按照不同方式访问后会是下面的结果

request: "GET /testproxynp10/aaa HTTP/1.1", upstream: "http://127.0.0.1:10000/testproxynp10/aaa"
request: "GET /testproxynp11/aaa HTTP/1.1", upstream: "http://127.0.0.1:10000/aaa"
request: "GET /testproxynp01/aaa HTTP/1.1", upstream: "http://127.0.0.1:10000//aaa"
request: "GET /testproxynp01aaa HTTP/1.1",  upstream: "http://127.0.0.1:10000/aaa"
request: "GET /testproxynp00aaa HTTP/1.1",  upstream: "http://127.0.0.1:10000/testproxynp00aaa"
request: "GET /testproxynp00/aaa HTTP/1.1", upstream: "http://127.0.0.1:10000/testproxynp00/aaa"

总结

声明:

后缀:/最后的斜杠

11:location有后缀且proxy_pass也有后缀

00:location无后缀且proxy_pass也无后缀

根据以上描述、论证以及平时的工作经历,可以发现,对于10和01两种类型平时慎用,这两种不好把握,也不符合常规,平时若想要代理路径,请选择使用11和00的方式
使用11时,会自动截取掉location上的路径,讲后面的路径映射到代理地址中;
使用00时,会自动将location路径中的所有路径平移到代理地址中,比如平时想要通过/aaa代理到 /aaab、 /aaac、/aaad这种,就可以直接使用/aaa一个location映射即可
  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值