zoukankan      html  css  js  c++  java
  • Nginx location指令匹配顺序规则

    location匹配命令

    1. “= ”,字面精确匹配, 如果匹配,则跳出匹配过程。(不再进行正则匹配)

    2. “^~ ”,最大前缀匹配,如果匹配,则跳出匹配过程。(不再进行正则匹配)

    3. 不带任何前缀:最大前缀匹配,举例如下:

         location /  代表以"/"开头的搜索匹配, 再没有正则表达式匹配的情况下才进行这个匹配(优先级最低)

    4. “~ ”,大小写相关的正则匹配

    5. “~* ” , 大小写无关的正则匹配

    6. “@”,  Named location 不是普通的location匹配,而是用于location内部重定向的变量。

    其中: 1、2、3 三种情况属于 location using literal string, 即使用普通字符串的location匹配;

                4、5       二种情况属于 location using regular expresstion,即使用正则表达式的location匹配;

    location 匹配的优先级(与location在配置文件中的顺序无关)
    = 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。
    普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
    ^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。
    最后匹配理带有"~"和"~*"的指令,如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。

    location  = / {
      # 只匹配"/".
      [ configuration A ] 
    }
    location  / {
      # 匹配任何请求,因为所有请求都是以"/"开始
      # 但是更长字符匹配或者正则表达式匹配会优先匹配
      [ configuration B ] 
    }
    location ^~ /images/ {
      # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
      [ configuration C ] 
    }
    location ~* .(gif|jpg|jpeg)$ {
      # 匹配以 gif, jpg, or jpeg结尾的请求. 
      # 但是所有 /images/ 目录的请求将由 [Configuration C]处理.   
      [ configuration D ] 
    }

    请求URI例子:

    • / -> 符合configuration A
    • /documents/document.html -> 符合configuration B
    • /images/1.gif -> 符合configuration C
    • /documents/1.jpg ->符合 configuration D
  • 相关阅读:
    [RxJS] Combination operators: concat, startWith
    [RxJS] Filtering operators: skipWhile and skipUntil
    [RxJS] Filtering operators: takeUntil, takeWhile
    [RxJS] Filtering operators: takeLast, last
    强连通tarjan模版
    HDU 4576 Robot (概率DP)
    Inside GDALAllRegister之二: 自动加载驱动
    [置顶] Java中字符串为什么不以结尾
    rcp(插件开发) The 'Eclipse-LazyStart' header is deprecated, use 'Bundle-ActivationPolicy'
    如何在模板中引用参数类中的一个特定member
  • 原文地址:https://www.cnblogs.com/hlongch/p/6513458.html
Copyright © 2011-2022 走看看