Nginx 位置优先级

编程入门 行业动态 更新时间:2024-10-12 10:25:00
本文介绍了Nginx 位置优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

解决方案

来自 HTTP 核心模块文档:

  • 带有="前缀的指令与查询完全匹配.如果找到,搜索将停止.
  • 所有剩余指令都带有常规字符串.如果此匹配使用了^~"前缀,则搜索停止.
  • 正则表达式,按照它们在配置文件中定义的顺序.
  • 如果 #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:

  • Directives with the "=" prefix that match the query exactly. If found, searching stops.
  • All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops.
  • Regular expressions, in the order they are defined in the configuration file.
  • 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 位置优先级

    本文发布于:2023-11-25 10:32:25,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1629404.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:优先级   位置   Nginx

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!