Nginx的location的规则及斜线的重要性

1、location规则

符号含义
== 开头表示进行普通字符精确匹配。也就是完全匹配
~表示执行一个正则匹配,区分大小写
~*表示执行一个正则匹配,不区分大小写
^~^~开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)
!~区分大小写不匹配
!~*不区分大小写不匹配
@它定义一个命名的 location,使用在内部定向时
/用户所使用的代理(一般为浏览器)

1.1、常用表达式示例

location = / {
          #规则A
      }
      location = /login {
          #规则B
      }
      location ^~ /static/ {
          #规则C
      }
      location ~ \.(gif|jpg|png|js|css)$ {
          #规则D
      }
      location ~* \.png$ {
          #规则E
      }
      location !~ \.xhtml$ {
          #规则F
      }
      location !~* \.xhtml$ {
          #规则G
      }
      location / {
          #规则H
      }

2、表达式优先级

nginx的location顺序没有太大关系,与表达式的类型有关。基本的规则是:相同类型的表达式,字符串长的会优先匹配。

  1. 检查请求uri是否与某个=规则匹配,如果有,直接应用规则,终止后续匹配。
  2. nginx首先检查所有路径匹配规则配置项,包括"^~"规则和没有前导符号的规则,选择并记住和当前请求uri匹配度最长的配置项。但这个时候,并不会启用相关的配置,而仅仅是记住。
  3. 判断上一步中选择下来的路径规则是否包含 ^~ ,如果包含,则使用该条规则,终止后续匹配。
  4. 按配置顺序进行正则表达式检查,匹配到第一条合适的正则表达式时,使用该条规则,终止后续匹配。
  5. 使用步骤三选择出来的路径匹配规则。

具体流程如下图:

在这里插入图片描述
示例:

location = / {
    [ configuration A ]
}

location / {
    [ configuration B ]
}

location /documents/ {
    [ configuration C ]
}

location ^~ /images/ {
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}

The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.

3、location中斜线的位置的重要性

不同的斜线的搭配访问结果是不同的,具体示例如下:

3.1、代理模式

nginx 服务器及端口 127.0.0.1:80
后端服务:127.0.0.1:8080
测试url:http://127.0.0.1:80/dongli/api/abc

A.配置
location /dongli/ {
   proxy_pass http://127.0.0.1:8080/;
}

实际访问:http://127.0.0.1:8080/api/abc

B.配置
location /dongli {
   proxy_pass http://127.0.0.1:8080/;
}

实际访问:http://127.0.0.1:8080//api/abc

C.配置
location /dongli/ {
   proxy_pass http://127.0.0.1:8080;
}

实际访问:http://127.0.0.1:8080/dongli/api/abc

D.配置
location /dongli {
   proxy_pass http://127.0.0.1:8080;
}

实际访问:http://127.0.0.1:8080/dongli/api/abc

E.配置
location /dongli/ {
   proxy_pass http://127.0.0.1:8080/server/;
}

实际访问:http://127.0.0.1:8080/server/api/abc

F.配置
location /dongli {
   proxy_pass http://127.0.0.1:8080/server/;
}

实际访问:http://127.0.0.1:8080/server//api/abc

G.配置
location /dongli/ {
   proxy_pass http://127.0.0.1:8080/server;
}

实际访问:http://127.0.0.1:8080/serverapi/abc

H.配置
location /dongli {
   proxy_pass http://127.0.0.1:8080/server;
}

实际访问:http://127.0.0.1:8080/server/api/abc

3.2 本地资源模式

nginx 服务器及端口 127.0.0.1:80
后端服务:127.0.0.1:8080
真实的资源路径:

E:/project/hello
E:/project/hello/index.html
E:/project/hello/img/123.png

测试url:

http://127.0.0.1/hello/index.html
http://127.0.0.1/hello/img/123.png
A.配置
location /hello/{
	root   E:/project/;
	index  index.html;
}

实际请求资源路径

E:/project/hello/index.html
E:/project/hello/img/123.png
B.配置
location /hello/{
	root   E:/project;
	index  index.html;
}

实际请求资源路径

E:/project/hello/index.html
E:/project/hello/img/123.png
C.配置
location /hello{
	root   E:/project/;
	index  index.html;
}

实际请求资源路径

E:/project/hello/index.html
E:/project/hello/img/123.png
D.配置
location /hello{
	root   E:/project;
	index  index.html;
}

实际请求资源路径

E:/project/hello/index.html
E:/project/hello/img/123.png
E.配置
location /hello/{
	alias   E:/project/;
}

实际请求资源路径

E:/project/hello/index.html 404
E:/project/hello/img/123.png 正常
F.配置
location /hello{
	alias   E:/project/;
}

实际请求资源路径

E:/project/hello/index.html 404
E:/project/hello/img/123.png 正常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丿乐灬学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值