nginx location 匹配的规则
nginx 的配置文件中, server里面的location 的配置项的理解:
server {
listen 24010;
client_max_body_size 30M;
location =/ { #范围 / 根目录的时候,这个普通的结构会被最后一步的结果覆盖。
index aa;
root /data/root;
try_files /m.html =502;
}
location /bb/{
index a.html;
root /data/root;
}
location =/bb{
root /data/root;
try_files /index.html =404;
}
location ~* \.html {
root /data/M4-Product/www/gitvidagrid/app/webroot/ysr;
}
}
#访问 /bb/a.html的时候,最后一个location优先级比倒数第二个优先级更高。
1. =xx 这个是精确匹配,当匹配到时, 会中断后续的匹配。
=/ 这个只匹配根目录, http请求中,ip后面不管加不加 / , 发出的http请求头, 都是带有 / 的。
2. / 这个可以匹配所有的uri,
3. /xx/ 这个是普通的前缀匹配, (当多个前缀匹配满足时, 匹配的是更长的那个。 当既满足某个普通匹配,又满足正则匹配时,会匹配正则的)
4. ~ 和~* 这两个是正则匹配, (当多个正则匹配都满足时,匹配更长的这个正则匹配。)
5. ^~ 这个是使用前缀匹配,满足时,会中断匹配,不会匹配其它的正则匹配。
---------------------------------------------------------
参考:https://www.cnblogs.com/lidabo/p/4169396.html
To summarize, the order in which directives are checked is as follows:
- Directives with the “=” prefix that match the query exactly. If found, searching stops.
- All remaining directives with conventional strings. If this match used the “^~” prefix, searching stops.
- Regular expressions, in the order they are defined in the configuration file.
- If #3 yielded a match, that result is used. Otherwise, the match from #2 is used
----------------------------------------------------------------------------------------------------------------------
参考:https://www.cnblogs.com/shitoufengkuang/p/4919520.html
location表达式类型
~ 表示执行一个正则匹配,区分大小写
~* 表示执行一个正则匹配,不区分大小写
^~ 表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他location。
= 进行普通字符精确匹配。也就是完全匹配。
@ 它定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
location优先级说明
在nginx的location和配置中location的顺序没有太大关系。正location表达式的类型有关。相同类型的表达式,字符串长的会优先匹配。
以下是按优先级排列说明:
等号类型(=)的优先级最高。一旦匹配成功,则不再查找其他匹配项。
^~类型表达式。一旦匹配成功,则不再查找其他匹配项。
正则表达式类型(~ ~*)的优先级次之。如果有多个location的正则能匹配的话,则使用正则表达式最长的那个。
常规字符串匹配类型。按前缀匹配。
location优先级示例
配置项如下:
location = / {
# 仅仅匹配请求 /
[ configuration A ]
}
location / {
# 匹配所有以 / 开头的请求。
# 但是如果有更长的同类型的表达式,则选择更长的表达式。
# 如果有正则表达式可以匹配,则优先匹配正则表达式。
[ configuration B ]
}
location /documents/ {
# 匹配所有以 /documents/ 开头的请求。
# 但是如果有更长的同类型的表达式,则选择更长的表达式。
# 如果有正则表达式可以匹配,则优先匹配正则表达式。
[ configuration C ]
}
location ^~ /images/ {
# 匹配所有以 /images/ 开头的表达式,如果匹配成功,则停止匹配查找。
# 所以,即便有符合的正则表达式location,也不会被使用
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配所有以 gif jpg jpeg结尾的请求。
# 但是 以 /images/开头的请求,将使用 Configuration D
[ configuration E ]
}
请求匹配示例
/ -> configuration A
/index.html -> configuration B
/documents/document.html -> configuration C
/images/1.gif -> configuration D
/documents/1.jpg -> configuration E
注意,以上的匹配和在配置文件中定义的顺序无关。
nginx location 匹配的规则的更多相关文章
- nginx location匹配顺序及CI框架的nginx配置
Nginx location匹配顺序如下: 用前缀字符串定义的location规则对URI进行匹配测试. =号定义了精确的前缀字符串匹配,如果发现精确匹配则使用当前规则.否则继续下一步匹配. 匹配其它 ...
- nginx location匹配规则
谢谢作者的分享精神,原文地址:http://www.nginx.cn/115.html location匹配命令 ~ #波浪线表示执行一个正则匹配,区分大小写~* #表示执行一个正则匹 ...
- 转:nginx location匹配规则
location匹配命令 ~ #波浪线表示执行一个正则匹配,区分大小写~* #表示执行一个正则匹配,不区分大小写^~ #^~表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配 ...
- Nginx location 匹配规则详解
语法规则 location [=|~|~*|^~] /uri/ { … } 模式 含义 location = /uri = 表示精确匹配,只有完全匹配上才能生效 location ^~ /uri ^~ ...
- [转载+整理]Nginx Location匹配规则
目录 规则语法 location 分类 匹配顺序: 扩展 location / {}和 location =/ {}的区别 测试 规则语法 语法 匹配规则 空 普通匹配(遵循最大前缀匹配规则, 优先度 ...
- [整理] Nginx Location 匹配规则
目录 规则语法 location 分类 匹配顺序: 扩展 location / {}和 location =/ {}的区别 如何快速测试 规则语法 语法 匹配规则 空 普通匹配(遵循最大前缀匹配规则, ...
- 【转】nginx location匹配规则
转载请保留:http://www.nginx.cn/115.html location匹配命令 ~ #波浪线表示执行一个正则匹配,区分大小写~* #表示执行一个正则匹配,不区分大小写^ ...
- nginx location匹配及rewrite规则
location匹配规则 1. 实例 server{ location = \ { [配置A] } location / { [配置B] } location = /images/ { [配置C] } ...
- Nginx location 匹配顺序整理
Nginx location模块整理 具体的Nginx安装就不在这里描述了,这里只是为了对location的描述 Nginx环境 a. 查看当前系统cat /etc/redhat-release [r ...
随机推荐
- Slickflow.NET 开源工作流引擎基础介绍(十) -- 邮件轮询异步发送模块集成
前言:在任务数据生成时,为了让办理任务的用户及时获取到待办任务的主题和内容,需要发送通知类的消息,而电子邮件和手机端的短信通知则是比较普通的消息发送.本文是针对电子邮件异步发送模块的实现来做实例说明. ...
- LPC-LINK 2
LPC-Link 2 is an extensible, stand-alone debug adapter that can be configured to support various dev ...
- STM32 Controller area network (bxCAN) Identifier filtering
Identifier filtering In the CAN protocol the identifier of a message is not associated with the addr ...
- [Deepin 15] sudo source /etc/profile 提示找不到 source 命令(切换到 root 用户:sudo su)
在 Deepin/Ubuntu 系统 中,因为修改了下 配置文件,然后执行 source 命令重新加载配置文件,结果: sudo source /etc/profile 提示找不到 source 命令 ...
- stanford CS DB 课程 数据库系统实现
http://infolab.stanford.edu/db_pages/classes.html CS145: Introduction to Databases CS245: Databa ...
- GEF最简单的入门-helloword(1)
最近做插件项目.主要负责GEF这块. 好吧.资料真少的可以.特别是入门.都是一大堆一大堆的.网上最火的八进制的文章但对于我这种菜鸟级别看了还是一头雾水.各种资料折腾了半天.终于折腾出一个真正的入门例子 ...
- 基于设备树的controller学习(1)
作者 彭东林pengdonglin137@163.com 平台 TQ2440Linux-4.10.17 概述 在设备树中我们经常见到诸如"#clock-cells"."# ...
- Lucene 3.0 输出相似度
http://www.cnblogs.com/ibook360/archive/2011/10/19/2217638.html Lucene3.0之结果排序(原理篇) 传统上,人们将信息检索系统返回结 ...
- Drectx 3D窗口后台截图
//GDI与DX截屏API操作 LPDIRECTDRAW lpDD = NULL; LPDIRECTDRAWSURFACE lpDDSPrime = NULL; LPDIRECTDRAWSURFACE ...
- ios nil、NULL和NSNull 的使用
nil用来给对象赋值(Objective-C中的任何对象都属于id类型),NULL则给任何指针赋值,NULL和nil不能互换,nil用于类指针赋值(在Objective-C中类是一个对象,是类的met ...