Nginx location指令匹配顺序规则
2014-03-08 23:29
218 查看
location匹配命令
1. “= ”,字面精确匹配, 如果匹配,则跳出匹配过程。(不再进行正则匹配)
2. “^~ ”,最大前缀匹配,如果匹配,则跳出匹配过程。(不再进行正则匹配)
3. 不带任何前缀:最大前缀匹配,举例如下:
location / 代表以"/"开头的搜索匹配, 再没有正则表达式匹配的情况下才进行这个匹配(优先级最低)
4. “~ ”,大小写相关的正则匹配
5. “~* ” , 大小写无关的正则匹配
6. “@”, Named location 不是普通的location匹配,而是用于location内部重定向的变量。
其中: 1、2、3 三种情况属于 location using literal string, 即使用普通字符串的location匹配;
4、5 二种情况属于 location using regular expresstion,即使用正则表达式的location匹配;
location 匹配的优先级(与location在配置文件中的顺序无关)
= 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。
普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。
最后匹配理带有"~"和"~*"的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。
请求URI例子:
/ -> 符合configuration A
/documents/document.html -> 符合configuration B
/images/1.gif -> 符合configuration C
/documents/1.jpg ->符合 configuration D
1. “= ”,字面精确匹配, 如果匹配,则跳出匹配过程。(不再进行正则匹配)
2. “^~ ”,最大前缀匹配,如果匹配,则跳出匹配过程。(不再进行正则匹配)
3. 不带任何前缀:最大前缀匹配,举例如下:
location / 代表以"/"开头的搜索匹配, 再没有正则表达式匹配的情况下才进行这个匹配(优先级最低)
4. “~ ”,大小写相关的正则匹配
5. “~* ” , 大小写无关的正则匹配
6. “@”, Named location 不是普通的location匹配,而是用于location内部重定向的变量。
其中: 1、2、3 三种情况属于 location using literal string, 即使用普通字符串的location匹配;
4、5 二种情况属于 location using regular expresstion,即使用正则表达式的location匹配;
location 匹配的优先级(与location在配置文件中的顺序无关)
= 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。
普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。
最后匹配理带有"~"和"~*"的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。
location = / { # 只匹配"/". [ configuration A ] } location / { # 匹配任何请求,因为所有请求都是以"/"开始 # 但是更长字符匹配或者正则表达式匹配会优先匹配 [ configuration B ] } location ^~ /images/ { # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location [ configuration C ] } location ~* \.(gif|jpg|jpeg)$ { # 匹配以 gif, jpg, or jpeg结尾的请求. # 但是所有 /images/ 目录的请求将由 [Configuration C]处理. [ configuration D ] }
请求URI例子:
/ -> 符合configuration A
/documents/document.html -> 符合configuration B
/images/1.gif -> 符合configuration C
/documents/1.jpg ->符合 configuration D
相关文章推荐
- Nginx location指令匹配顺序规则
- Nginx location指令匹配顺序规则
- nginx location 指令匹配顺序
- nginx location 匹配规则,及先后顺序
- nginx location 指令匹配顺序
- nginx location 匹配顺序(转)
- nginx location匹配规则
- nginx location 匹配规则
- Nginx 的 location 指令的匹配规则
- nginx location匹配规则
- Nginx服务器的location指令匹配规则详解
- 详解Nginx location 匹配规则
- NGINX location 匹配规则
- Nginx location 匹配规则
- nginx location模块--匹配规则
- nginx location 匹配规则
- nginx location 匹配规则
- nginx location 匹配顺序
- 规则匹配之 字符串类的顺序对比
- nginx location的正则匹配规则