【发布时间】:2011-07-11 10:28:51
【问题描述】:
位置指令按什么顺序触发?
【问题讨论】:
标签: nginx
位置指令按什么顺序触发?
【问题讨论】:
标签: nginx
文档中的示例:
location = / {
# matches the query / only.
[ configuration A ]
}
location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
[ configuration B ]
}
location /documents/ {
# matches any query beginning with /documents/ and continues searching,
# so regular expressions will be checked. This will be matched only if
# regular expressions don't find a match.
[ configuration C ]
}
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
# Configuration D.
[ configuration E ]
}
如果仍然感到困惑,here's a longer explanation。
【讨论】:
/ 和 /documents/ 规则都匹配请求 /documents/index.html,但后一个规则优先,因为它是最长的规则。
按此顺序触发。
= (完全正确)
location = /path
^~ (前锋)
location ^~ /path
~ (正则表达式区分大小写)
location ~ /path/
~* (正则表达式不区分大小写)
location ~* .(jpg|png|bmp)
/
location /path
【讨论】:
location = /path/,其他应该包括开始和结束修饰符(^ 和$)
现在有一个方便的在线工具来测试位置优先级:
location priority testing online
【讨论】:
按以下顺序评估位置:
location = /path/file.ext {} 完全匹配location ^~ /path/ {} 优先前缀匹配 -> 最长优先location ~ /Paths?/ {}(区分大小写的正则表达式)和 location ~* /paths?/ {}(不区分大小写的正则表达式)-> 第一个匹配项location /path/ {} 前缀匹配 -> 最长优先优先级前缀匹配(数字 2)与公共前缀匹配(数字 4)完全相同,但优先于任何正则表达式。
对于这两种前缀匹配类型,最长的匹配获胜。
区分大小写和不区分大小写具有相同的优先级。评估在第一个匹配规则处停止。
Documentation 表示在任何正则表达式之前评估所有前缀规则,但如果一个正则表达式匹配,则不使用标准前缀规则。这有点令人困惑,并且不会改变上面报告的优先顺序。
【讨论】: