Nginx的location设置教材和优先级
  • Nginx的location设置教材和优先级如下:Nginx的Location可以有以下几个匹配:
    1. =   严格匹配这个查询。如果找到,停止搜索。 
    2. ^~ 匹配路径的前缀,如果找到,停止搜索。
    3. ~   为区分大小写的正则匹配   
    4. ~* 为不区分大小写匹配

    受到攻击discuz的缓存案例:

            location ^~ /index.php {
                   proxy_pass http://www.654z.com:9192/index.php;
                    include vhosts/conf.proxy_cache;
            }


            location = / {
                   proxy_pass http://www.654z.com:9192/;
                    include vhosts/conf.proxy_cache;
            }


            location ^~ /uc_server/avatar.php {
                   proxy_pass http://www.654z.com:9192/uc_server/avatar.php;
                    include vhosts/conf.proxy_cache;
            }
     


    例子:

    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 ^~ /images/ {
    # matches any query beginning with /images/ and halts searching,
    # so regular expressions will not be checked.
    # 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
    [ configuration C ]
    }
    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 C.
    # 匹配任何已 gif、jpg 或 jpeg 结尾的请求。然而所有 /images/ 目录的请求将使用 Configuration C。
    [ configuration D ]
    }

     

    如果要定义多个location,则可以有2种方式:

    1. 使用/ :
      location / { client_max_body_size 200m; proxy_connect_timeout 30; proxy_set_header Host $http_host; proxy_set_header x-forwarded-for $remote_addr; proxy_pass http://127.0.0.1:8008; } location /tmp/{ root /; internal; }
      采用这种方式,/tmp可以放在/的下面,因为“/是匹配任何查询,但是正则表达式规则和长的块规则将被优先和查询匹配”
    2. 使用~ /* :
      location ~ /tmp/ { root /tmp; internal; } location ~ /* { client_max_body_size 20m; proxy_connect_timeout 30; fastcgi_pass fpass; include fastcgi_params; }
      采用这种方式,/tmp则必须放在~ /*这个前面,因为~是正则匹配的,正则匹配是有顺序的,只要匹配上就不会再往下匹配了。除非在conf中有定义=或者^~,也就是说=和^~的优先级最高,如果匹配上,就不会再去匹配其它的规则了。

    总之,引用Nginx的官方文档的匹配规则:

     

    引用

     

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

    注意:正则表达式的匹配是有顺序的,按顺序匹配。其它的匹配理论上讲是只有优先级,而没有顺序的。

     

     

     ningx官方新版翻译

    location
     
    syntax: location [=|~|~*|^~|@] /uri/ { ... }
    语法: location [=|~|~*|^~|@] /uri/ { ... }
     
    default: no
    默认: 否
     
    context: server
    上下文: server
     
    This directive allows different configurations depending on the URI. It can be configured using both literal strings and regular expressions. To use regular expressions, you must use a prefix:
    这个变量允许按照根据URI使用不同的配置.配置可以使用普通的字符串或者是正则表达式.使用正则表达式,必须使用一个前缀:
     
       1. "~" for case sensitive matching
       2. "~*" for case insensitive matching 
       1. "~" 用于区分大小写(大小写敏感)的匹配
       2. "~*" 用于不区分大小写的匹配
     
    To determine which location directive matches a particular query, the literal strings are checked first. Literal strings match the beginning portion of the query - the most specific match will be used. Afterwards, regular expressions are checked in the order defined in the configuration file. The first regular expression to match the query will stop the search. If no regular expression matches are found, the result from the literal string search is used.
    在决定哪个location变量来匹配一个特定的查询时,普通字符串会先检查.普通字符串查找查询的开头做匹配 -- 将会使用最明确的那个匹配(我的理解是:使用匹配得最完整的那个字符串的配置).然后正则表达式按照配置文件里面的顺序来匹配.第一个匹配查询的正则表达式会停止剩下的查找.如果没有匹配的正则表达式,就会使用普通字符串的查找结果.
     
    For caseless operation systems, like Mac OS X and Cygwin, liternal string matching will be done in case insensitive way (0.7.7). However, comparision is limited to single-byte locale's only.
    对于小部分系统,如Mac OS X 和Cygwin,普通字符串会以不区分大小写的情况来做匹配(0.7.7).但是,这种区别仅限于单字节 locale的 情况.
     
    Regular expression may contain captures (0.7.40), which can be used in other directives.
    正则表达式可以包含capture(0.7.40),capture可以用在其他的指令中.
     
    It is possible to disable regular expression checks after liternal string matching by using "^~" prefix. If most specific match literal location have this prefix - regular expressions aren't checked.
     "^~" 这个前缀的作用:在常规的字符串匹配检查之后,不做正则表达式的检查---即如果最明确的那个字符串匹配的location配置中有此前缀,那么不会做正则表达式的检查.
     
    By using "=" prefix on may define exact match between URI and location. On match search stops immediately as further search has no sense. E.g. if the request "/" occurs frequently, using "location = /" will speed up processing of this request a bit as search will stop after first comparison.
    使用  "=" 前缀可以做URI和location的精确匹配.匹配之后查询就会停止.例如,如果 "/" 这个请求常出现,使用 "location = /" 将会提高一点处理这个请求的速度.
     
    On exact match with literal location without "=" or "^~" prefixes search is also immediately terminated.
    (如何理解翻译?)
     
    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.
       1. 有 "=" 前缀的指令对"查询"做精确的匹配,如果找到了,查找停止. 
       2. All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops.
       2. 指令为常规字符串.如果匹配中使用了 "^~" ,查找停止.
       3. Regular expressions, in the order they are defined in the configuration file.
       3. 指令为正则表达式.按照配置文件里面的顺序查找.
       4. If #3 yielded a match, that result is used. Otherwise, the match from #2 is used. 
       4. 如果#3找到了,那么就用3的.否则,就用#2的.
     
    It is important to know that nginx does the comparison against decoded URIs. For example, if you wish to match "/images/%20/test", then you must use "/images/ /test" to determine the location.
    提醒:nginx会对编码过的URI做比较.例如,如果你想要匹配"/images/%20/test" , 你必须使用 "/images/ /test" 来定义这个location
     
    Example:
    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 ^~ /images/ { 
        # matches any query beginning with /images/ and halts searching, 
        # so regular expressions will not be checked. 
        [ configuration C ]    

    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 C.        
        [ configuration D ]    
     
    Example requests:
            * / -> configuration A 
            * /documents/document.html -> configuration B 
            * /images/1.gif -> configuration C 
            * /documents/1.jpg -> configuration D
     
    Note that you could define these 4 configurations in any order and the results would remain the same. While nested locations are allowed by the configuration file parser, their use is discouraged and may produce unexpected results.
    注意:上面的4段在文件中顺序可以是任意的,出来的效果都一样.虽然嵌套的location 指令是允许的,但不建议!会引起非预期的结果.
     
    The prefix "@" specifies a named location. Such locations are not used during normal processing of requests, they are intended only to process internally redirected requests (see error_page, try_files). 
     "@" 前缀指定的是一个命名的location.这样的location在一般的请求处理中是不适用的, 仅仅设计来处理内部的重定向请求(查看error_page, try_files).

  •  

  • 点这里复制本页地址发送给您QQ/MSN上的好友
  • 300*300广告