NginX "Location" 区块选择算法
NginX "Location" block selection algorithm
由于可以定义多个具有不同模式的位置块,当 Nginx 收到请求时,它会搜索与请求的 URI 最匹配的位置块。
我在配置文件中指定位置块的顺序无关紧要。 Nginx 将按特定顺序搜索匹配模式:
location 块带有 = 修饰符:如果指定的字符串完全匹配
请求的 URI,Nginx 保留位置块。
不带修饰符的位置块:如果指定的字符串与
请求的 URI,Nginx 保留位置块。
location 块带有 ^~ 修饰符:如果指定的字符串匹配
请求的 URI 的开头,Nginx 保留位置块。
带有 ~ 或 ~* 修饰符的位置块:如果正则表达式匹配
请求的 URI,Nginx 保留位置块。
不带修饰符的位置块:如果指定的字符串与
请求的 URI 的开头,Nginx 保留位置块。
因此,在以下情况下:
server
{
server_name website.com;
location /document
{
[…] # requests beginning with "/document"
}
location ~* ^/document$
{
[…] # requests exactly matching "/document"
}
}
有人告诉我,在这种情况下,第一个块中指定的字符串现在与请求的 URI 完全匹配。因此,Nginx 比正则表达式更喜欢它。
我的问题是为什么? NginX 应该 select 第二个块(优先级 3),因为它比第一个块(优先级 5)具有更好(更低)的优先级。
谁能解释一下为什么 NginX 仍然会选择第二个块?
提前致谢。
关于这个问题documentation:
To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.
根据文档,nginx 必须选择正则表达式位置。我通过在我的配置中添加以下块来测试它:
location /document
{
return 405;
}
location ~* ^/document$
{
return 406;
}
这是测试结果,正如预期的那样,使用了正则表达式位置:
root@terrty:~# curl -I https://terrty.net/document
HTTP/1.1 406 Not Acceptable
Server: nginx/1.7.10
由于可以定义多个具有不同模式的位置块,当 Nginx 收到请求时,它会搜索与请求的 URI 最匹配的位置块。
我在配置文件中指定位置块的顺序无关紧要。 Nginx 将按特定顺序搜索匹配模式:
location 块带有 = 修饰符:如果指定的字符串完全匹配 请求的 URI,Nginx 保留位置块。
不带修饰符的位置块:如果指定的字符串与 请求的 URI,Nginx 保留位置块。
location 块带有 ^~ 修饰符:如果指定的字符串匹配 请求的 URI 的开头,Nginx 保留位置块。
带有 ~ 或 ~* 修饰符的位置块:如果正则表达式匹配 请求的 URI,Nginx 保留位置块。
不带修饰符的位置块:如果指定的字符串与 请求的 URI 的开头,Nginx 保留位置块。
因此,在以下情况下:
server
{
server_name website.com;
location /document
{
[…] # requests beginning with "/document"
}
location ~* ^/document$
{
[…] # requests exactly matching "/document"
}
}
有人告诉我,在这种情况下,第一个块中指定的字符串现在与请求的 URI 完全匹配。因此,Nginx 比正则表达式更喜欢它。
我的问题是为什么? NginX 应该 select 第二个块(优先级 3),因为它比第一个块(优先级 5)具有更好(更低)的优先级。
谁能解释一下为什么 NginX 仍然会选择第二个块?
提前致谢。
关于这个问题documentation:
To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.
根据文档,nginx 必须选择正则表达式位置。我通过在我的配置中添加以下块来测试它:
location /document
{
return 405;
}
location ~* ^/document$
{
return 406;
}
这是测试结果,正如预期的那样,使用了正则表达式位置:
root@terrty:~# curl -I https://terrty.net/document
HTTP/1.1 406 Not Acceptable
Server: nginx/1.7.10