zoukankan      html  css  js  c++  java
  • nginx location 匹配的规则

    nginx 的配置文件中, server里面的location 的配置项的理解:

     server {
         listen       24010;
     
       client_max_body_size  30M;
     
       location  =/ { #范围 / 根目录的时候,这个普通的结构会被最后一步的结果覆盖。
         index aa;
         root /data/root;
         try_files /m.html =502;
       }
     
       location /bb/{
         index a.html;
         root /data/root;
       }
       location =/bb{
         root /data/root;
         try_files /index.html =404;
       }
     
       location ~* .html {
         root /data/M4-Product/www/gitvidagrid/app/webroot/ysr;
       }
     }
     #访问 /bb/a.html的时候,最后一个location优先级比倒数第二个优先级更高。
    

      

    1. =xx 这个是精确匹配,当匹配到时, 会中断后续的匹配。

       =/  这个只匹配根目录, http请求中,ip后面不管加不加 / , 发出的http请求头, 都是带有 / 的。

    2. / 这个可以匹配所有的uri, 

    3. /xx/  这个是普通的前缀匹配, (当多个前缀匹配满足时, 匹配的是更长的那个。 当既满足某个普通匹配,又满足正则匹配时,会匹配正则的)

    4. ~ 和~* 这两个是正则匹配, (当多个正则匹配都满足时,匹配更长的这个正则匹配。)

    5. ^~ 这个是使用前缀匹配,满足时,会中断匹配,不会匹配其它的正则匹配。

    ---------------------------------------------------------

    参考:https://www.cnblogs.com/lidabo/p/4169396.html

    To summarize, the order in which directives are checked is as follows:

    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

    ----------------------------------------------------------------------------------------------------------------------

    参考:https://www.cnblogs.com/shitoufengkuang/p/4919520.html

    location表达式类型

    ~ 表示执行一个正则匹配,区分大小写

    ~* 表示执行一个正则匹配,不区分大小写

    ^~ 表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他location。

    = 进行普通字符精确匹配。也就是完全匹配。

    @ 它定义一个命名的 location,使用在内部定向时,例如 error_page, try_files

    location优先级说明

    在nginx的location和配置中location的顺序没有太大关系。正location表达式的类型有关。相同类型的表达式,字符串长的会优先匹配。

    以下是按优先级排列说明:

    等号类型(=)的优先级最高。一旦匹配成功,则不再查找其他匹配项。

    ^~类型表达式。一旦匹配成功,则不再查找其他匹配项。

    正则表达式类型(~ ~*)的优先级次之。如果有多个location的正则能匹配的话,则使用正则表达式最长的那个。

    常规字符串匹配类型。按前缀匹配。

    location优先级示例

    配置项如下:

    location = / {

        # 仅仅匹配请求 /

        [ configuration A ]

    }

    location / {

        # 匹配所有以 / 开头的请求。

        # 但是如果有更长的同类型的表达式,则选择更长的表达式。

        # 如果有正则表达式可以匹配,则优先匹配正则表达式。

        [ configuration B ]

    }

    location /documents/ {

        # 匹配所有以 /documents/ 开头的请求。

        # 但是如果有更长的同类型的表达式,则选择更长的表达式。

        # 如果有正则表达式可以匹配,则优先匹配正则表达式。

        [ configuration C ]

    }

    location ^~ /images/ {

        # 匹配所有以 /images/ 开头的表达式,如果匹配成功,则停止匹配查找。

        # 所以,即便有符合的正则表达式location,也不会被使用

        [ configuration D ]

    }

    location ~* .(gif|jpg|jpeg)$ {

        # 匹配所有以 gif jpg jpeg结尾的请求。

        # 但是 以 /images/开头的请求,将使用 Configuration D

        [ configuration E ]

    }

    请求匹配示例

    / -> configuration A

    /index.html -> configuration B

    /documents/document.html -> configuration C

    /images/1.gif -> configuration D

    /documents/1.jpg -> configuration E

    注意,以上的匹配和在配置文件中定义的顺序无关。

  • 相关阅读:
    HashMap,Hash优化与高效散列
    Dubbo Overview
    模板引擎 引自 《PHP核心技术与最佳实践》
    使用 phpStorm 开发
    使用 Zend_Studio 开发
    Symfony 2.0 认识Request, Response, Session, Cookie
    Symfony 建立一个Bundle
    Symfony 从路由认识它
    信鸽推送.net 服务端代码
    c#输出json,其中包含子json (可以含 无限级 子json)的方法思路
  • 原文地址:https://www.cnblogs.com/oxspirt/p/10423122.html
Copyright © 2011-2022 走看看