当前位置:   article > 正文

nginx location匹配规则_nginx 判断路径

nginx 判断路径

语法规则

nginx官方文档说明

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

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

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

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

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

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

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

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

官方示例:

  1. location = / {
  2. [ configuration A ]
  3. }
  4. location / {
  5. [ configuration B ]
  6. }
  7. location /documents/ {
  8. [ configuration C ]
  9. }
  10. location ^~ /images/ {
  11. [ configuration D ]
  12. }
  13. location ~* \.(gif|jpg|jpeg)$ {
  14. [ configuration E ]
  15. }

请求“/”匹配配置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模块用于调试

  1. #下载模块
  2. [root@localhost ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
  3. #解压
  4. [root@localhost ~]# tar xf echo-nginx-module-0.61
  5. #查看原来的参数,预编译时加上--add-module=/root/echo-nginx-module-0.61 ,添加echo模块
  6. [root@localhost ~]# nginx -V
  7. nginx version: nginx/1.18.0
  8. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
  9. built with OpenSSL 1.0.2k-fips 26 Jan 2017
  10. TLS SNI support enabled
  11. 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
  12. [root@localhost ~]# cd nginx-1.18.0
  13. [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
  14. [root@localhost nginx-1.18.0]# make
  15. [root@localhost nginx-1.18.0]# cd objs/
  16. [root@localhost objs]# ls
  17. addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o
  18. autoconf.err nginx ngx_auto_config.h ngx_modules.c src
  19. #备份正在在使用的nginx
  20. [root@localhost ~]# cp /usr/local/nginx/sbin/nginx /opt/
  21. #新编译的nginx替换原来的
  22. [root@localhost objs]# nginx -s stop;cp nginx /usr/local/nginx/sbin/;nginx
  23. cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y

1. 无修饰符

  1. #任何未匹配到其它location的请求都会匹配到
  2. server {
  3. listen 80;
  4. server_name www.test.com;
  5. location /abc {
  6. echo "无修饰符(/)";
  7. }
  8. ......
  9. }
  10. [root@localhost ~]# curl www.test.com/abc
  11. 无修饰符(/)
  12. [root@localhost ~]# curl www.test.com/abc/
  13. 无修饰符(/)
  14. [root@localhost ~]# curl www.test.com/abc/ABc
  15. 无修饰符(/)
  16. [root@localhost ~]# curl www.test.com/abc/abc
  17. 无修饰符(/)
  18. [root@localhost ~]# curl www.test.com/abc/ABc/
  19. 无修饰符(/)

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

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

  1. server {
  2. listen 80;
  3. server_name www.test.com;
  4. location /abc {
  5. echo "无修饰符(/)";
  6. }
  7. location = /abc {
  8. echo "精确匹配(=)";
  9. }
  10. }
  11. [root@localhost ~]# curl www.test.com/abc
  12. 精确匹配(=)
  13. [root@localhost ~]# curl www.test.com/abc/
  14. 无修饰符(/)
  15. [root@localhost ~]# curl www.test.com/abc/ABC
  16. 无修饰符(/)
  17. [root@localhost ~]# curl www.test.com/abc?a=1\$b=2
  18. 精确匹配(=)

3. ^~ 匹配字符串开头

  1. server {
  2. listen 80;
  3. server_name www.test.com;
  4. location ^~ /static {
  5. echo "匹配开头(^~)";
  6. }
  7. }
  8. [root@localhost ~]# curl www.test.com/static
  9. 匹配开头(^~)
  10. [root@localhost ~]# curl www.test.com/staticcc
  11. 匹配开头(^~)
  12. [root@localhost ~]# curl www.test.com/static/
  13. 匹配开头(^~)

4. ~ 区分大小写的匹配

  1. server {
  2. listen 80;
  3. server_name www.test.com;
  4. location ~ /images/ {
  5. echo "区分大小写的匹配(~)";
  6. }
  7. }
  8. [root@localhost ~]# curl www.test.com/IMAGES/
  9. <html>
  10. <head><title>404 Not Found</title></head>
  11. <body>
  12. <center><h1>404 Not Found</h1></center>
  13. <hr><center>nginx/1.18.0</center>
  14. </body>
  15. </html>
  16. [root@localhost ~]# curl www.test.com/images/
  17. 区分大小写的匹配(~)
  18. [root@localhost ~]# curl www.test.com/images/1.jpg
  19. 区分大小写的匹配(~)

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

  1. server {
  2. listen 80;
  3. server_name www.test.com;
  4. location ~* \.(gif|jpg|jpeg)$ {
  5. echo "不区分大小写的匹配(~*)";
  6. }
  7. }
  8. [root@localhost ~]# curl www.test.com/a.jpg
  9. 不区分大小写的匹配(~*)
  10. [root@localhost ~]# curl www.test.com/A.jpg
  11. 不区分大小写的匹配(~*)
  12. [root@localhost ~]# curl www.test.com/a.jpg/
  13. <html>
  14. <head><title>404 Not Found</title></head>
  15. <body>
  16. <center><h1>404 Not Found</h1></center>
  17. <hr><center>nginx/1.18.0</center>
  18. </body>
  19. </html>

访问控制

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

  1. allow 192.168.1.1/32 172.16.0.0/16;
  2. 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 中国大陆许可协议进行许可。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/254457
推荐阅读
相关标签
  

闽ICP备14008679号