栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > 服务器

Nginx的location的常见规则优先级问题

服务器 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力
目录
  • 一、Location / 匹配
  • 二、Location = / 匹配

locaiton有四种类型的匹配规则,分别为完全匹配(=)、前缀普通匹配(^~)、正则表达式匹配(~或者~*)、普通匹配

规则

  • 等号类型(=)的优先级最高。一旦匹配成功,则不再查找其他匹配项
  • 前缀普通匹配(^~)优先级次之。不支持正则表达式。使用前缀匹配,如果有多个location匹配的话,则使用表达式最长的那个
  • 正则表达式类型(~ ~*)的优先级次之。一旦匹配成功,则不再查找其他匹配项
  • 常规字符串匹配,如果有多个location匹配的话,则使用表达式最长的那个

说明

  • 先判断精准命中,如果命中,立即返回结果并结束解析过程
  • 若未结束,判断前缀普通命中,如果有多个命中,使用表达式“最长”的命中结果,结束解析过程
  • 若未结束,继续判断正则表达式的匹配,按正则表达式顺序为准,由上至下一旦匹配成功1个,立即返回结果,并结束解析过程
  • 若未结束,继续普通命中,普通命中和前缀普通命中相似,顺序无所谓,按照location表达式的长短来确定命中结果

Location,用来快速进行资源定位,定义不同方式来处理或解决url请求,一般有:/ , = /, ~, ~* ,^~
优先级是:(location = /)>(localtion^~)>(location ~| ~* )>(location /)
其中,~ 与 ~*,谁在上面先匹配谁.

一、Location / 匹配
# "/" 是直接到nginx发布目录/usr/local/nginx/html/里来查资源,比如location.html
location / {
 root   html;
 index  index.html index.htm;
    }

在发布目录里创建location.html文件,内容为:this is location.html。
执行172.16.0.9/location.html时,服务器发布目录里找这个location.html文件,并把结果this is loction.html返回出来,如下:

root@backupserver:/usr/local/nginx/html# ls
50x.html  index.html
root@backupserver:/usr/local/nginx/html# echo "this is  location.html" > ./location.html
root@backupserver:/usr/local/nginx/html# ls
50x.html  index.html  location.html
root@backupserver:/usr/local/nginx/html# /usr/local/nginx/sbin/nginx -s reload
root@backupserver:/usr/local/nginx/html# curl 172.16.0.9/location.html
this is  location.html
root@backupserver:/usr/local/nginx/html# 


在这里插入图片描述

二、Location = / 匹配

精准定位 一般用来匹配某个文件,优先级高于 /
比如:
在nginx配置文件中增加一个location = / ,定位到/data目录。如下:

server {
 listen80;
 server_name  localhost;
 #charset koi8-r;
 #access_log  logs/host.access.log  main;
 location / {
     autoindex on;
     root   html;
     index  index.html index.htm;
 }
 location = /location.html {
     root /data/;
     index index.html;
 }

重启Nginx服务,测试:
1、先不在/data/目录创建location.html。
可以看到,报404错误。这是因为,= / 优先级高于/ ,服务器去第二个location里寻找location.html这个文件,而不是去第一个location里找。由于第二个location指定的目录是/data,/data目录下没有location.html文件


在这里插入图片描述

在/data目录下创建location.html文件

root@backupserver:/usr/local/nginx/html# ls /data/
www
root@backupserver:/usr/local/nginx/html# echo "this is  other location.com" > /data/location.html
root@backupserver:/usr/local/nginx/html# ls
50x.html  index.html  location.html
root@backupserver:/usr/local/nginx/html# curl 172.16.0.9/location.html
this is  other location.com
root@backupserver:/usr/local/nginx/html#


在这里插入图片描述

上面可以看到,访问 服务器是,服务器首先去location = /里面找,即使它在另一个location下面。精准匹配优先级是最高的,它不论是在配置文件内容上面还是下面,服务器首先去找精准匹配的内容

除以精准匹配,还有~ ,~* ,^~
~是对大小写敏感,匹配时严格大小写
~* 是对大小写不敏感,匹配时不分大小写。
^~用来匹配以uri开头,匹配成功以后,会停止搜索后面的正则表达式匹配。
以上优先最高的是精准匹配。location = /,其次是:^,然后是 和~* 这两种看准在配置文件内容上面,就先匹配谁,优先级最低的是 /

以上规则在使用nginx时会被广泛使用,比如多台服务器做网站动静分离时:

location ~ .*.(html|htm|js|css|txt|png|jpg|jpeg|doc)$ {
root html;
}

到此这篇关于Nginx的location的常见规则优先级的文章就介绍到这了,更多相关Nginx location规则优先级内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/304794.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号