你的位置:国外/美国服务器租用 技术文档 LinuxVPS使用教程 正文
美国服务器出租
  1. 1美国独立服务器10G独享带宽不限流量,欧洲1G带宽独享,不限流量
  2. 2美国100M独享,洛杉矶 32GB内存 英特尔至强CPU,特价:1699元/月
  3. 3美国加州机房100M独享E3-1270,32G内存/96G内存,送KVM,1399元/月
  4. 4美国云服务器,8G内存,服务器CPU,4核心,特价699元/月,16G内存1299元
  5. 5美国G口抗攻击服务器,G口1000M独享带宽抗DDOS攻击服务器(至强Xeon E3 1230)
  6. 6国外G口带宽独享服务器,美国G口独享,欧洲G口独享服务器租用
  7. 7美国圣安娜KT服务器,加州KT服务器租用,KT独立服务器出租(特价799元/月)
  8. 8加州洛杉矶机房,中国访问速度最快的美国机房之一,999元/月,4G内存20M独享
  9. 9美国1G独享带宽,欧洲1G独享带宽租用(视频等大流量网站解决方案)
  10. 10凤凰城机房Phoenix服务器租用:7个机房4核I3,8G内存,30M独享带宽,首月999元
美国VPS主机
  1. 1美国SSD VPS租用,美国西海岸加州洛杉矶SSD VPS服务器,Linux/Windows
  2. 2内华达州VPS,拉斯维加斯VPS,拉斯维加斯服务器,内华达州服务器租用
  3. 3美国东海岸VPS,纽约服务器,曼哈顿云服务器,纽约VPS租用
  4. 4外贸VPS服务器,仿牌空间,仿牌主机,抗投诉VPS(外贸英文商城VPS)SSL证书安装服务
  5. 5美国Psychz电信直连VPS,中国访问速度最快的美国VPS,Psychz机房VPS
  6. 6Camforg专用VPS,美国Camforg多视频聊天软件VPS,Camforg服务器租用
  7. 7美国加州VPS,洛杉矶WebNX机房VPS,加州WN机房Windows VPS
  8. 8美国西雅图VPS,西雅图机房VPS,支持试用的VPS,VPS试用10元/天
  9. 9合租美国服务器,国外服务器合租,高端VPS服务器,完胜低配独立服务器的VPS
  10. 10抗攻击Windows VPS,不怕DDOS攻击的VPS,有攻击不关机,无攻击后2小时内恢复
Nginx的location设置教材和优先级
  • Nginx的location设置教材和优先级如下:Nginx的Location可以有以下几个匹配:
    1. =   严格匹配这个查询。如果找到,停止搜索。 
    2. ^~ 匹配路径的前缀,如果找到,停止搜索。
    3. ~   为区分大小写的正则匹配   
    4. ~* 为不区分大小写匹配

    受到攻击discuz的缓存案例:

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


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


            location ^~ /uc_server/avatar.php {
                   proxy_pass http://www.ctohome.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 norm