nginx 匹配路径_nginx location 匹配目录

nginx每个location都是一个匹配目录,nginx的策略是:访问请求来时,会对访问地址进行解析,从上到下逐个匹配,匹配上就执行对应location大括号中的策略,并根据策略对请求作出相应。

依访问地址:http://www.wandouduoduo.com/wddd/index.html为例,nginx配置如下:

location /wddd/ {

proxy_connect_timeout 18000; ##修改成半个小时

proxy_send_timeout 18000;

proxy_read_timeout 18000;

proxy_pass http://127.0.0.1:8080;

}

那访问时就会匹配这个location,从而把请求代理转发到本机的8080Tomcat服务中,Tomcat相应后,信息原路返回。总结:location如果没有“/”时,请求就可以模糊匹配以字符串开头的所有字符串,而有“/”时,只能精确匹配字符本身。

下面举个例子说明:

配置location /wandou 可以匹配/wandoudouduo请求,也可以匹配/wandou*/duoduo等等,只要以wandou开头的目录都可以匹配到。

而location /wandou/必须精确匹配/wandou/这个目录的请求,不能匹配/wandouduoduo/或/wandou*/duoduo等请求。

proxy_pass有无“/”的四种区别探究

访问地址都是以:http://www.wandouduoduo.com/wddd/index.html 为例。请求都匹配目录/wddd/

第一种:加"/"

location /wddd/ {

proxy_pass http://127.0.0.1:8080/;

}

第二种: 不加"/"

location /wddd/ {

proxy_pass http://127.0.0.1:8080;

}

location /wddd/ {

proxy_pass http://127.0.0.1:8080/sun/;

}

location /wddd/ {

proxy_pass http://127.0.0.1:8080/sun;

}

总结

location目录后加"/",只能匹配目录,不加“/”不仅可以匹配目录还对目录进行模糊匹配。而proxy_pass无论加不加“/”,代理跳转地址都直接拼接。

server {

listen 80;

server_name localhost; # http://localhost/wddd01/xxx -> http://localhost:8080/wddd01/xxx

location /wddd01/ {

proxy_pass http://localhost:8080;

}

# http://localhost/wddd02/xxx -> http://localhost:8080/xxx

location /wddd02/ {

proxy_pass http://localhost:8080/;

}

# http://localhost/wddd03/xxx -> http://localhost:8080/wddd03*/xxx

location /wddd03 {

proxy_pass http://localhost:8080;

}

# http://localhost/wddd04/xxx -> http://localhost:8080//xxx,请注意这里的双斜线,好好分析一下。

location /wddd04 {

proxy_pass http://localhost:8080/;

}

# http://localhost/wddd05/xxx -> http://localhost:8080/hahaxxx,请注意这里的haha和xxx之间没有斜杠,分析一下原因。

location /wddd05/ {

proxy_pass http://localhost:8080/haha;

}

# http://localhost/api6/xxx -> http://localhost:8080/haha/xxx

location /wddd06/ {

proxy_pass http://localhost:8080/haha/;

}

# http://localhost/wddd07/xxx -> http://localhost:8080/haha/xxx

location /wddd07 {

proxy_pass http://localhost:8080/haha;

}

# http://localhost/wddd08/xxx -> http://localhost:8080/haha//xxx,请注意这里的双斜杠。

location /wddd08 {

proxy_pass http://localhost:8080/haha/;

}

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值