nginx location匹配规则

语法规则

nginx官方文档说明

location [=|~|~*|^~|!~|!~*] /pattern/{...}
默认值:no
使用字段:server,location

修饰符功能
=精确匹配
~正则表达式模式匹配,区分大小写, !~取反
~*正则表达式模式匹配,不区分大小写, !~*取反
^~前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式
@定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等

为某个请求URI(路径)建立配置。

路径匹配在URI规范化以后进行。所谓规范化,就是先将URI中形如“%XX”的编码字符进行解码, 再解析URI中的相对路径“.”和“..”部分, 另外还可能会压缩相邻的两个或多个斜线成为一个斜线。

可以使用前缀字符串或者正则表达式定义路径。使用正则表达式需要在路径开始添加“~*”前缀 (不区分大小写),或者“~”前缀(区分大小写)。为了根据请求URI查找路径,nginx先检查前缀字符串定义的路径 (前缀路径),在这些路径中找到能最精确匹配请求URI的路径。然后nginx按在配置文件中的出现顺序检查正则表达式路径, 匹配上某个路径后即停止匹配并使用该路径的配置,否则使用最大前缀匹配的路径的配置。

路径可以嵌套,但有例外。

如果最大前缀匹配的路径以“^~”开始,那么nginx不再检查正则表达式。

而且,使用“=”前缀可以定义URI和路径的精确匹配。如果发现匹配,则终止路径查找。 比如,如果请求“/”出现频繁,定义“location = /”可以提高这些请求的处理速度, 因为查找过程在第一次比较以后即结束。这样的路径明显不可能包含嵌套路径。

官方示例:

location = / {
    [ configuration A ]
}

location / {
    [ configuration B ]
}

location /documents/ {
    [ configuration C ]
}

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

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

请求“/”匹配配置A, 请求“/index.html”匹配配置B, 请求“/documents/document.html”匹配配置C, 请求“/images/1.gif”匹配配置D, 请求“/documents/1.jpg”匹配配置E。

前缀“@”定义了命名路径。这种路径不在一般的请求处理中使用, 而是用在请求重定向中。这些路径不能嵌套,也不能包含嵌套路径。

匹配优先级

1、= 精确匹配

​ 严格匹配,只有完全相等才匹配成功,然后停止匹配,不然继续匹配

2、^~ 字符串前缀匹配

​ 字符串前缀匹配,前缀字符串相同就匹配成功,然后停止匹配,不然继续匹配

​ 如果有包含关系,按最大匹配原则匹配,比如在前缀匹配:location /dir01 与 location /dir01/dir02,如有请求 http://localhost/dir01/dir02/file 将最终匹配到 location /dir01/dir02

3、正则匹配 ~ 和 ~*

​ ~ 和 ~* 实质上是同级的,优先级取决于配置文件书写的先后顺序,先写的优先级高

​ ~ 区分大小写的匹配

​ 进行区分大小写的正则匹配,匹配成功后仍续搜索,如果有多个匹配,取最长匹配

​ ~* 区分大小写的匹配

​ 进行不区分大小写的正则匹配,匹配成功后仍续搜索,如果有多个匹配,取最长匹配

4、 通用匹配 location / {......}

​ 所有请求都可以匹配到,匹配到一个普通格式后,搜索并未结束,而是暂存当前匹配的结果,并继续搜索正则匹配模式 ,匹配到正则就交给正则匹配处理,如果没有匹配到正则就以暂存的结果为匹配结果

优先级次序如下:

( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) 、( location ~* 正则 ) --> ( location 路径 )

匹配实例

安装echo模块用于调试

#下载模块
[root@localhost ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz

#解压
[root@localhost ~]# tar xf echo-nginx-module-0.61

#查看原来的参数,预编译时加上--add-module=/root/echo-nginx-module-0.61  ,添加echo模块
[root@localhost ~]# nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=/root/echo-nginx-module-0.61


[root@localhost ~]# cd nginx-1.18.0
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=/root/echo-nginx-module-0.61
[root@localhost nginx-1.18.0]# make
[root@localhost nginx-1.18.0]# cd objs/
[root@localhost objs]# ls
addon         Makefile  nginx.8            ngx_auto_headers.h  ngx_modules.o
autoconf.err  nginx     ngx_auto_config.h  ngx_modules.c       src

#备份正在在使用的nginx
[root@localhost ~]# cp /usr/local/nginx/sbin/nginx /opt/

#新编译的nginx替换原来的
[root@localhost objs]# nginx -s stop;cp nginx /usr/local/nginx/sbin/;nginx 
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y


1. 无修饰符

#任何未匹配到其它location的请求都会匹配到
    server {
        listen       80;
        server_name  www.test.com;

location /abc {
   echo "无修饰符(/)";
}
......
    }



[root@localhost ~]# curl www.test.com/abc
无修饰符(/)
[root@localhost ~]# curl www.test.com/abc/
无修饰符(/)
[root@localhost ~]# curl www.test.com/abc/ABc
无修饰符(/)
[root@localhost ~]# curl www.test.com/abc/abc
无修饰符(/)
[root@localhost ~]# curl www.test.com/abc/ABc/
无修饰符(/)


2. = 对普通字符精确匹配

location = /uri   = 开头表示精确匹配,只有完全匹配上才能生效。

    server {
        listen       80;
        server_name  www.test.com;

location /abc {
   echo "无修饰符(/)";
}
location = /abc {
   echo "精确匹配(=)";
}

    }


[root@localhost ~]# curl www.test.com/abc
精确匹配(=)
[root@localhost ~]# curl www.test.com/abc/
无修饰符(/)
[root@localhost ~]# curl www.test.com/abc/ABC
无修饰符(/)
[root@localhost ~]# curl www.test.com/abc?a=1\$b=2
精确匹配(=)


3. ^~ 匹配字符串开头

    server {
        listen       80;
        server_name  www.test.com;
location ^~ /static {
   echo "匹配开头(^~)";
}
    }

[root@localhost ~]# curl www.test.com/static
匹配开头(^~)
[root@localhost ~]# curl www.test.com/staticcc
匹配开头(^~)
[root@localhost ~]# curl www.test.com/static/
匹配开头(^~)

4. ~ 区分大小写的匹配

    server {
        listen       80;
        server_name  www.test.com;

location ~ /images/ {
   echo "区分大小写的匹配(~)";
}

    }
    
    
    
[root@localhost ~]# curl www.test.com/IMAGES/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>

[root@localhost ~]# curl www.test.com/images/
区分大小写的匹配(~)
[root@localhost ~]# curl www.test.com/images/1.jpg
区分大小写的匹配(~)

5. ~* 不区分大小写的匹配

    server {
        listen       80;
        server_name  www.test.com;

location ~* \.(gif|jpg|jpeg)$ {
   echo "不区分大小写的匹配(~*)";
}

    }



[root@localhost ~]# curl www.test.com/a.jpg
不区分大小写的匹配(~*)
[root@localhost ~]# curl www.test.com/A.jpg
不区分大小写的匹配(~*)
[root@localhost ~]# curl www.test.com/a.jpg/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>

访问控制

location段
allow:设定允许哪台或哪些主机访问,多个参数间用空格隔开
deny:设定禁止哪台或哪些主机访问,多个参数间用空格隔开
示例:

allow 192.168.1.1/32 172.16.0.0/16;
deny all;

用户认证

auth_basic "欢迎信息";
auth_basic_user_file "/path/to/user_auth_file"

user_auth_file内容格式为:

username:password

这里的密码为加密后的密码串,建议用htpasswd来创建此文件:

htpasswd -c -m /path/to/.user_auth_file USERNAME

要点总结:

location 的匹配顺序是“先匹配普通,再匹配正则”

普通匹配与配置书写顺序无关,因为按照匹配的长短来取匹配结果

正则匹配与顺序有关,因为是从上往下匹配。

正则匹配项匹配规则,受配置文件的前后顺序影响,但普通匹配模式不会

本文作者:EverEternity

本文链接:https://www.cnblogs.com/shipment/p/13463742.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值