Nginx位置优先级 [英] Nginx location priority

查看:61
本文介绍了Nginx位置优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What order do location directives fire in?

推荐答案

来自 HttpCoreModule文档:

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

如果仍然令人困惑,请此处有较长的解释.

If it's still confusing, here's a longer explanation.

这篇关于Nginx位置优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆