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

Nginx localtion规则匹配

  • 1. Nginx location 区段介绍
  • 2. location 前缀含义
  • 3. location 配置示例
    • 3.1. location 没有修饰符匹配
    • 3.2. location = 匹配
    • 3.3. location ~ 匹配
    • 3.4. location ^~ 和 ~*匹配
  • 4. location 匹配总结
  • 5. location 匹配顺序

  • 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 /      任何没有匹配成功的,都会匹配这里处理

你可能感兴趣的:(Nginx,nginx,运维,服务器,linux,centos)