小编典典

Nginx 位置优先级

all

位置指令按什么顺序触发?


阅读 90

收藏
2022-05-06

共1个答案

小编典典

HTTP
核心模块文档

  1. 带有“=”前缀的指令与查询完全匹配。如果找到,则停止搜索。
  2. 所有剩余的带有常规字符串的指令。如果此匹配使用“^~”前缀,则搜索停止。
  3. 正则表达式,按照它们在配置文件中定义的顺序。
  4. 如果 #3 产生匹配,则使用该结果。否则,使用来自#2 的匹配。

文档中的示例:

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 ] 
}

如果它仍然令人困惑,这里有一个更长的解释

2022-05-06