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

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

问题描述

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

解决方案

来自 HTTP 核心模块文档:

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

文档中的示例:

location =/{# 仅匹配查询/.[配置A]}地点/{# 匹配任何查询,因为所有查询都以/开头,但是是常规查询# 表达式和任何更长的常规块将# 先匹配.[配置B]}位置/文件/{# 匹配任何以/documents/开头的查询并继续搜索,# 所以正则表达式将被检查.这将仅在以下情况下匹配# 正则表达式没有找到匹配项.[配置C]}位置 ^~/图像/{# 匹配任何以/images/开头的查询并停止搜索,# 所以不会检查正则表达式.[配置D]}位置 ~* .(gif|jpg|jpeg)$ {# 匹配任何以 gif、jpg 或 jpeg 结尾的请求.然而,所有# 对/images/目录的请求将由# 配置 D.[配置E]}

如果仍然令人困惑,这里有更长的解释.>

What order do location directives fire in?

解决方案

From the HTTP core module docs:

  1. Directives with the "=" prefix that match the query exactly. If found, searching stops.
  2. All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops.
  3. Regular expressions, in the order they are defined in the configuration file.
  4. If #3 yielded a match, that result is used. Otherwise, the match from #2 is used.

Example from the documentation:

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天全站免登陆