nginx location各种修饰符的匹配优先级

这作为一个备份,方便查询,毕竟nginx的强大,必然有其复杂性!

Location modifier
Nginx allows you to define location blocks by specifying a pattern that will be
matched against the requested document URI.
server {
server_name website.com;
location /admin/ {
# The configuration you place here only applies to
# http://website.com/admin/
}
}

location的匹配修饰符(modifier)有如下几种:

location [=|~|~*|^~|@] pattern { ... }

=   完全相等的匹配

~   区分大小写的带正则的匹配

~* 不区分大小写的带正则的匹配

^~ 类似无修饰符的匹配,URI一旦匹配到相关的请求(必须从URI头部开始)就停止再去匹配其他

@  命名修饰符,只能用于内部请求匹配

The = modifier
The requested document URI must match the specified pattern exactly. The pattern
here is limited to a simple literal string; you cannot use a regular expression:
server {
server_name website.com;
location = /abcd {
[…]
}
}
The configuration in the location block:
• Applies to http://website.com/abcd (exact match)
• Applies to http://website.com/ABCD (it is case-sensitive if your operating
system uses a case-sensitive filesystem)
• Applies to http://website.com/abcd?param1&param2 (regardless of query
string arguments)
• Does not apply to http://website.com/abcd/ (trailing slash)
• Does not apply to http://website.com/abcde (extra characters after the
specified pattern)
The ~ modifier
The requested URI must be a case-sensitive match to the specified regular expression:
server {
server_name website.com;
location ~ ^/abcd$ {
[…]
}
}
The ^/abcd$ regular expression used in this example specifies that the pattern
must begin (^) with /, be followed by abc, and finish ($) with d. Consequently,
the configuration in the location block:
• Applies to http://website.com/abcd (exact match)
• Does not apply to http://website.com/ABCD (case-sensitive)
• Applies to http://website.com/abcd?param1&param2 (regardless of query
string arguments)
• Does not apply to http://website.com/abcd/ (trailing slash) due to the
specified regular expression
• Does not apply to http://website.com/abcde (extra characters) due to the
specified regular expression
The ~* modifier
The requested URI must be a case-insensitive match to the specified regular expression:
server {
server_name website.com;
location ~* ^/abcd$ {
[…]
}
}
The regular expression used in the example is similar to the previous one.
Consequently, the configuration in the location block:
• Applies to http://website.com/abcd (exact match)
• Applies to http://website.com/ABCD (case-insensitive)
• Applies to http://website.com/abcd?param1&param2 (regardless of query
string arguments)
• Does not apply to http://website.com/abcd/ (trailing slash) due to the
specified regular expression
• Does not apply to http://website.com/abcde (extra characters) due to the
specified regular expression
The ^~ modifier
Similar to the no-symbol behavior, the location URI must begin with the specified
pattern. The difference is that if the pattern is matched, Nginx stops searching for
other patterns (read the section below about search order and priority).
The @ modifier
Defines a named location block. These blocks cannot be accessed by the client,
but only by internal requests generated by other directives, such as try_files or
error_page.
Search order and priority
Since it‘s possible to define multiple location blocks with different patterns, you
need to understand that when Nginx receives a request, it searches for the location
block that best matches the requested URI:
server {
server_name website.com;
location /files/ {
# applies to any request starting with "/files/"
# for example /files/doc.txt, /files/, /files/temp/
}
location = /files/ {
# applies to the exact request to "/files/"
# and as such does not apply to /files/doc.txt
# but only /files/
}
}
When a client visits http://website.com/files/doc.txt, the first location block
applies. However, when they visit http://website.com/files/, the second block
applies (even though the first one matches) because it has priority over the first one
(it is an exact match).
The order you established in the configuration file (placing the /files/ block before
the = /files/ block) is irrelevant. Nginx will search for matching patterns in a
specific order:
1. location blocks with the = modifier: If the specified string exactly matches
the requested URI, Nginx retains the location block.
2. location blocks with no modifier: If the specified string exactly matches the
requested URI, Nginx retains the location block.
3. location blocks with the ^~ modifier: If the specified string matches the
beginning of the requested URI, Nginx retains the location block.
4. location blocks with ~ or ~* modifier: If the regular expression matches the
requested URI, Nginx retains the location block.
5. location blocks with no modifier: If the specified string matches the
beginning of the requested URI, Nginx retains the location block.
In that extent, the ^~ modifier begins to make sense, and we can envision cases
where it becomes useful.

Case 1:
server {
server_name website.com;
location /doc {
[…] # requests beginning with "/doc"
}
location ~* ^/document$ {
[…] # requests exactly matching "/document"
}
}

You might wonder: when a client requests http://website.com/document, which
of these two location blocks applies? Indeed, both blocks match this request. Again,
the answer does not lie in the order in which the blocks appear in the configuration
files. In this case, the second location block will apply as the ~* modifier has
priority over the other.

Case 2:
server {
server_name website.com;
location /document {
[…] # requests beginning with "/document"
}
location ~* ^/document$ {
[…] # requests exactly matching "/document"
}
}
The question remains the same—what happens when a client sends a request
to download http://website.com/document? There is a trick here. The string
specified in the first block now exactly matches the requested URI. As a result, Nginx
prefers it over the regular expression.

Case 3:
server {
server_name website.com;
location ^~ /doc {
[…] # requests beginning with "/doc"
}
location ~* ^/document$ {
[…] # requests exactly matching "/document"
}
}
This last case makes use of the ^~ modifier. Which block applies when a client visits
http://website.com/document? The answer is the first block. The reason being
that ^~ has priority over ~*. As a result, any request with a URI beginning with /
doc will be affected to the first block, even if the request URI matches the regular
expression defined in the second block.

个人的观点,老外发明的东西或者提出的idea,最好看E文原版的资料,得到第一手的资料信息,中文翻译后,很多都比较难以考证或者不全。

时间: 2024-09-29 22:25:48

nginx location各种修饰符的匹配优先级的相关文章

06 nginx Location详解之精准匹配

一:Location详解之精准匹配 location 语法 location 有”定位”的意思, 根据Uri来进行不同的定位. 在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上. 比如, 碰到.php, 如何调用PHP解释器?  --这时就需要location location 的语法 location [=|~|~*|^~] patt { } 中括号可以不写任何参数,此时称为一般匹配 也可以写参数 因此,大类型可以分为3种 location = p

Nginx Location和Rewrite深入剖析

Nginx Location和Rewrite深入剖析 Nginx Location Nginx由内核和模块组成,其中内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端的请求映射到一个location block,而location是Nginx配置中的一个指令,用于访问的URL匹配,而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作. location功能是由ngx_http_index_module模块提供的. location常放在serve

nginx之location匹配优先级和安全问题

最近一直在做location的配置,遇到优先级别问题(如有配置不当,会存在安全隐患),以下是个人的一些学习体会 一.location 匹配符 1.等于匹配符:      ##"=" 其特点可概括为两点:   ##1.精确匹配,2.不支持正则表达式 语法示例: 1 location = /static/img/file.jpg { 2 3 ... 4 5 } Code-1 2.空  匹配符: 空匹配符的特点是:   ##1.匹配以指定模式开始的URI,2.不支持正则 语法示例: 1 lo

nginx location优先级详解

nginx中location有几种: 1.前缀,可以有=或^~修饰,比如 location  /       /开头的 location /img/     /img/开头的 location = /a.htm      刚好/a.htm location ^~ /d    匹配后不再检查正则表达式location.注意这个意思不是非正则表达式!除了~开头其他都是非正则表达式,也就是前缀匹配 2.正则表达式,固定~或~*(不区分大小写)开头,比如: location ~  \.html$ loc

nginx location 规则优先级

一 nginx  location 匹配命令 ~   #表示执行一个正则匹配,区分大小写 ~*  #表示执行一个正则匹配,不区分大小写 ^~  #表示普通字符匹配,如果该选项匹配,只匹配该选项, 不匹配别的选项,一般用来匹配目录 =   #进行普通字符精确匹配 二 nginx location 匹配优先级(与location在配置文件中的顺序无关) 1=  精确匹配.如果发现精确匹配,nginx停止搜索其他匹配模式. 2    普通字符匹配,正则表达式规则和长的块规则将被优先和和查询匹配,也就是

nginx——location 优先级

一. location 的匹配符1.等于匹配符:=等于匹配符就是等号,特点可以概括为两点:精确匹配不支持正则表达式2.空匹配符空匹配符的特点是:匹配以指定模式开始的 URI不支持正则表达式3.正则匹配符:~正则匹配符是可以使用正则表达式的匹配符.不过这里要强调的是, 一般来说~是指:区分大小写的正则匹配而~*表示:         不区分大小写的正则匹配但是对于一些对大小写不敏感的操作系统,这两者没有区别. 二.优先级的实例 location ~ ^/poechant$ { return 400

Nginx Location匹配顺序

理论部分 文字释义匹配规则如下: 略述: 1.nginx服务器首先在server块的多个location块中搜索是否有标准的uri和请求字符串匹配.如果有多个标准uri可以匹配,就匹配其中匹配度最高的一个location. 2.然后,nginx在使用location块中,正则uri和请求字符串,进行匹配.如果正则匹配成功,则结束匹配,并使用这个location处理请求:如果正则匹配失败,则使用标准uri中,匹配度最高的location. 详细: 1.如果有精确匹配,会先进行精确匹配,匹配成功,立

nginx location 优先级

location 顺序/优先级:     location = > location 完整路径 > location ^~ 路径 > location ~,~* 正则顺序 > location 部分起始路径 > / location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 # 但是正则和最长字符串会优先匹配 [ config

nginx location指令详解

来源:https://www.cnblogs.com/xiaoliangup/p/9175932.html Nginx的HTTP配置主要包括三个区块,结构如下: http { //这个是协议级别 include mime.types; default_type application/octet-stream; keepalive_timeout 65; gzip on; server { //这个是服务器级别 listen 80; server_name localhost; location