nginx location区段匹配规则
官方文档
语法
功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能
语法:location [ 修饰符 ] pattern {…}
常用修饰符说明:
修饰符 | 功能 |
---|---|
= | 精确匹配 |
~ | 正则表达式模式匹配,区分大小写 |
~* | 正则表达式模式匹配,不区分大小写 |
^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配, 那么就停止搜索其他模式了,不支持正则表达式 |
@ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |
没有修饰符表示必须以指定模式开始,如:
server {
server_name www.idfsoft.com;
location /abc {
......
}
}
那么如下内容就可正确匹配:
- http://www.idfsoft.com/abc
- http://www.idfsoft.com/abc?p1=11&p2=22
- http://www.idfsoft.com/abc/
=:表示必须与指定的模式精确匹配,如:
server {
server_name www.idfsoft.com;
location = /abc {
......
}
}
那么如下内容就可正确匹配:
- http://www.idfsoft.com/abc
- http://www.idfsoft.com/abc?p1=11&p2=22
如下内容则无法匹配:
- http://www.idfsoft.com/abc/
- http://www.idfsoft.com/abc/abcde
~:表示指定的正则表达式要区分大小写,如:
server {
server_name www.idfsoft.com;
location ~ ^/abc$ {
......
}
}
那么如下内容就可正确匹配:
- http://www.idfsoft.com/abc
- http://www.idfsoft.com/abc?p1=11&p2=22
如下内容则无法匹配:
- http://www.idfsoft.com/abc/
- http://www.idfsoft.com/ABC
- http://www.idfsoft.com/abcde
~*:表示指定的正则表达式不区分大小写,如:
server {
server_name www.idfsoft.com;
location ~* ^/abc$ {
......
}
}
那么如下内容就可正确匹配:
- http://www.idfsoft.com/abc
- http://www.idfsoft.com/abc?p1=11&p2=22
- http://www.idfsoft.com/ABC
如下内容则无法匹配:
- http://www.idfsoft.com/abc/
- http://www.idfsoft.com/abcde
~:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,则停止搜索其他模式
查找顺序和优先级:由高到底依次为
- 带有=的精确匹配优先
- 正则表达式按照他们在配置文件中定义的顺序
- 带有^~修饰符的,开头匹配
- 带有或*修饰符的,如果正则表达式与URI匹配
- 没有修饰符的精确匹配
匹配实例
安装echo模块
//下载模块
[root@localhost ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
//解压
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.16.1.tar.gz
nginx-1.14.2.tar.gz v0.61.tar.gz
[root@localhost ~]# tar xf v0.61.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.16.1.tar.gz
echo-nginx-module-0.61 v0.61.tar.gz
nginx-1.14.2.tar.gz
//编译
[root@localhost ~]# cd /usr/local
[root@localhost local]# cd nginx-1.16.1/
[root@localhost nginx-1.16.1]# ./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.16.1]# make
[root@localhost nginx-1.16.1]# cd objs/
[root@localhost objs]# ls
addon nginx ngx_auto_headers.h src
autoconf.err nginx.8 ngx_modules.c
Makefile ngx_auto_config.h ngx_modules.o
//备份正在在使用的nginx
[root@localhost objs]# 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
无修饰符
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
//任何未匹配到其它location的请求都会匹配到
location /abc {
echo "无修饰符";
}
}
}
[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl http://192.168.30.130/abc
无修饰符
[root@localhost ~]# curl http://192.168.30.130/ABc
无修饰符
[root@localhost ~]# curl http://192.168.30.130/ABc/
无修饰符
= 对普通字符精确匹配
location = /uri = 开头表示精确匹配,只有完全匹配上才能生效。
location /abc {
echo "无修饰符";
}
location = /abc {
echo "精确匹配";
}
}
[root@localhost ~]# curl http://192.168.30.130/abc
精确匹配
[root@localhost ~]# curl http://192.168.30.130/abc/
无修饰符
[root@localhost ~]# curl http://192.168.30.130/abc/abcc
无修饰符
[root@localhost ~]# curl http://192.168.30.130/abc?a=1\$b=2
精确匹配
^~ 匹配字符串开头
location ^~ /abc {
echo "匹配开头";
}
}
[root@localhost ~]# curl http://192.168.30.130/abcdee
匹配开头
[root@localhost ~]# curl http://192.168.30.130/abc
匹配开头
[root@localhost ~]# curl http://192.168.30.130/abc/
匹配开头
~ 区分大小写的匹配
location ~ /abc/ {
echo "区分大小写的匹配";
}
}
[root@localhost ~]# curl http://192.168.30.130/ABC/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
[root@localhost ~]# [root@localhost ~]# curl http://192.168.30.130/abc/
区分大小写的匹配
[root@localhost ~]# curl http://192.168.30.130/abc/ABC
区分大小写的匹配
~* 不区分大小写的匹配
location ~* ^/abc$ {
echo "不区分大小写的匹配";
}
[root@localhost ~]# curl http://192.168.30.130/abc
不区分大小写的匹配
[root@localhost ~]# curl http://192.168.30.130/ABC
不区分大小写的匹配
[root@localhost ~]# curl http://192.168.30.130/abcdee
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>