Nginx 位置优先级

标签 nginx

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

最佳答案

来自 HTTP core module docs :

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

    如果仍然令人困惑,here's a longer explanation .

    关于Nginx 位置优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61597048/

    相关文章:

    reactjs - 更改 nginx 默认文件夹

    ruby-on-rails - 如何使用 NGINX 反向代理将蜻蜓图像保存到远程服务器?

    elasticsearch - Logstash 中的节拍输入正在丢失字段

    java - Jetty 应该位于哪个目录中?

    windows - Nginx 大量 'hanging'/Windows 上的待处理请求

    node.js - Nodejitsu 设计决策

    nginx - 具有相同域的 80 端口上的多个 Docker 容器

    php - 使用 Ubuntu 在 Nginx 上托管应用程序时出现路由问题

    php - 当我使用 php setrawcookie 函数时出现服务器错误

    testing - 使用 Nginx 将 GET 参数插入静态文件