Nginx localtion规则匹配详细介绍并举例

  • Nginx 的 location 模块用于定义请求的处理规则,根据请求的 URI 匹配不同的规则,并指定相应的配置块。
  • Nginx 的 HTTP 配置主要包括三个区块,结构如下:
http {                      # 这个是协议级别
  include mime.types;
  default_type application/octet-stream;
  keepalive_timeout 65;
  gzip on;
    server {             # 这个是服务器级别
      listen 80;
      server_name localhost;
        location / {  # 这个是请求级别
          root html;
          index index.html index.htm;
        }
      }
}

1. Nginx location 区段介绍

  • location 是在 server 块中配置,根据不同的 URI 使用不同的配置,来处理不同的请求。
  • location 是有顺序的,会根据不同请求配置的优先级来匹配的location 处理。
  • 基本语法如下:匹配URL类型,有6种参数可选,当然也可以不带参数。
    location [ =| ^~ | ~ | ~* | uri | / ]
  • 命名location,用@标识,类似于定于goto语句块。
    location @name { … }

2. location 前缀含义

符号含义
=表示精确匹配,优先级也是最高的,大小写敏感,如果匹配成功就停止向下匹配并立即处理请求
^~表示uri以某个常规字符串开头,理解为匹配url路径即可,表示包含正则表达式,并且匹配以指定的正则表达式开头,对uri的最左边部分做匹配检查,不区分字符大小写
~表示区分大小写的正则匹配,表示包含正则表达式,并且区分大小写
~*表示不区分大小写的正则匹配,表示包含正则表达式,并且不区分大写
!~表示区分大小写不匹配的正则
!~*表示不区分大小写不匹配的正则
/通用匹配,任何请求都会匹配到
@内部服务跳转

查找顺序和优先级
= 大于 ^~ 大于 ~ | ~* | !~ | !~* 大于 /
多个location配置的情况下匹配顺序为:首先匹配 =,其次匹配^~, 其次是按正则匹配,最后是交给 /通用匹配。
当有匹配成功时候,停止匹配,按当前匹配规则处理请求。

3. location 配置示例

3.1. location 没有修饰符匹配

没有修饰符,表示:必须以指定模式开始
不加任何规则时,默认是大小写敏感,前缀匹配,相当于加了~^~

[root@localhost conf.d]# cat default.conf
server {
    listen       80;
    server_name  localhost;
    location /abc {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}[root@localhost conf.d]# cd /usr/share/nginx/html/
[root@localhost html]# ls
50x.html  index.html  
[root@localhost html]# mkdir abc
[root@localhost html]# cp index.html  abc/
[root@localhost html]# systemctl restart nginx
​
//那么,访问时,必须加上abc,例如:http://192.168.221.130/abc
//访问前必须要保证访问的nginx根目录下面有abc目录

3.2. location = 匹配

=表示:必须与指定的模式精确匹配

[root@localhost conf.d]# vim default.conf
server {
    listen       80;
    server_name  localhost;
    access_log  /var/log/nginx/http_access.log  main;
​
    location / {
        root /usr/share/nginx/html;
        index a.html index.htm;
    }
    location = / {
        root /usr/share/nginx/html;
        index b.html index.htm;
    }
}

浏览器访问测试:
http://192.168.221.130 对应的是=/
http://192.168.221.130/a.html 对应的是/
直接访问IP,访问的是b.html
访问a.html需要加上后缀

3.3. location ~ 匹配

~ 表示:指定的正则表达式【区分大小写】

[root@localhost conf.d]# cat default.conf
server {
    listen       80;
    server_name  localhost;
    location ~ /abc {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }# location = / {
   #     root   /usr/share/nginx/html;
   #     index  b.html index.htm;
   # }
}

测试访问:http://192.168.221.130/abc
不正确的访问:http://192.168.221.130/ABC

修改配置文件,再次访问观察

//将配置文件修改为:
server {
    listen       80;
    server_name  localhost;
    location ~ /abc {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
​
    location ~ /ABC {
        root   /usr/share/nginx/html;
        index  b.html index.htm;
    }
}//创建目录和文件:
[root@localhost conf.d]# mkdir /usr/share/nginx/html/ABC
[root@localhost conf.d]# cp /usr/share/nginx/html/b.html  /usr/share/nginx/html/ABC
​
[root@localhost conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost conf.d]# systemctl restart nginx

访问:http://192.168.221.130/ABC/
结论~ 区分大小写。而且目录需要根据大小写定义。

3.4. location ^~ 和 ~*匹配

~*,执行正则匹配,忽略大小写;^~,表示普通字符串匹配到后【不再】进行正则匹配(采用该规则后,该符号后面的字符是最佳匹配,不再进行后续的查找)
^~ 和 ~* 匹配案例

server {
    listen       80;
    server_name  localhost;
​
    location ^~ /static {
        root    /usr/share/nginx/html; 
        index index.html;
    }
​
    location ~* .jpg$ {
        root    /usr/share/nginx/html;  #上传图片到发布目录中
    }}
//static目录资源
[root@localhost html]# pwd
/usr/share/nginx/html
[root@localhost html]# mkdir static
[root@localhost html]# cd static
[root@localhost static]# cp ../index.html  .///.jpg资源访问
[root@localhost html]# ls
50x.html  abc  ABC  a.html  b.html  index.html  jingtai  static  test.jpg   
//将test.jpg上传到/usr/share/nginx/html中

​浏览器访问:
http://192.168.221.130/static/
http://192.168.221.130/test.jpg

4. location 匹配总结

location = / {    # 只匹配 / 的查询
  [ configuration A ]
}
location / {    # 匹配任何以 / 开始的查询,但是正则表达式与一些较长的字符串将被首先匹配。
  [ configuration B ]
}
location ^~ /images/ {    # 匹配任何以 /images/ 开始的查询并且停止搜索,不检查正则表达式。
  [ configuration C ]
}
location ~* \.(gif|jpg|jpeg)$ {    # 匹配任何以gif, jpg, or jpeg结尾的文件
  [ configuration D ]
} 

#各请求的处理如下例:
    / → configuration A
    /documents/document.html → configuration B
    /images/1.gif → configuration C
    /documents/1.jpg → configuration D

5. location 匹配顺序

= 大于 ^~ 大于 ~ | ~* 大于 最长前缀匹配 大于 /
location =      精准匹配;= 匹配优先级最高。一旦匹配成功,则不再查找其他匹配项。
location ^~     带参前缀匹配;^~类型表达式。一旦匹配成功,则不再查找其他匹配项。
location ~      正则匹配(区分大小写)
location ~*     正则匹配(不区分大小写)
location /a     普通前缀匹配,优先级低于带参数前缀匹配。
location /      任何没有匹配成功的,都会匹配这里处理
  • 23
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
nginx中,location指令用于匹配请求的URI,以便决定如何处理这个请求。location指令可以接受一个字符串参数,也可以接受一个正则表达式作为参数。 以下是nginx location的路径匹配规则: 1. 以“=”开头的location指令表示严格匹配,只有当请求的URI与location指令的参数完全一致时,才会匹配成功。 2. 如果location指令的参数是一个目录名称,例如“/user/”,那么匹配规则如下: - 如果请求的URI是“/user/”,那么匹配成功。 - 如果请求的URI是“/user”(没有斜杠结尾),那么nginx会自动将其转换为“/user/”,然后再进行匹配。 - 如果请求的URI是“/user/login”,那么匹配成功,因为“/user/login”包含“/user/”这个目录名称。 3. 如果location指令的参数是一个正则表达式,例如“~^/user/(.*)$”,那么匹配规则如下: - 如果请求的URI与正则表达式匹配成功,那么这个location指令就匹配成功。 - 正则表达式中可以使用捕获组,例如“~^/user/(\d+)/(\w+)$”表示匹配形如“/user/123/abc”的URI,并将“123”和“abc”作为变量传递给后端处理程序。 4. 如果location指令的参数是“/”,那么这个location指令会匹配所有请求。 5. 如果存在多个location指令,nginx会按照定义的顺序依次进行匹配,直到找到第一个匹配成功的location指令为止。 需要注意的是,nginx的location匹配规则是从上到下依次匹配的,一旦匹配成功就不再继续匹配。因此,如果存在多个location指令,需要注意定义的顺序,避免出现匹配错误的情况。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

TA548464

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

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

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

打赏作者

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

抵扣说明:

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

余额充值