zoukankan      html  css  js  c++  java
  • nigx配置location规则

    以下是收集的,对于不是很熟的朋友,配置转发很有帮助

    1、location匹配命令和优先级:

    优先级:

    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.
    1. =前缀的指令严格匹配这个查询。如果找到,停止搜索。
    2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。
    3. 正则表达式,在配置文件中定义的顺序。
    4. 如果第3条规则产生匹配的话,结果被使用。否则,使用第2条规则的结果。

    匹配命令:

    "="前缀指令匹配,如果匹配成功,则停止其他匹配

    普通字符串指令匹配,后续还会匹配正则表达,^~    也表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项

    ~ 正则表达式指令匹配,区分大小写

    ~* 正则表达式指令匹配,不区分大小写

    /  通用匹配,任何未匹配到其它location的请求都会匹配到

    2、例子

    1、测试"^~"和"~",nginx配置如下。浏览器输入http://localhost/helloworld/test,返回601。如将#1注释,#2打开,浏览器输入http://localhost/helloworld/test,返回603。注:#1和#2不能同时打开,如同时打开,启动nginx会报nginx: [emerg] duplicate location "/helloworld"...,因为这两个都是同一个字符串。

    location ^~ /helloworld {      #1
        return 601;
    }
            
    #location /helloworld {        #2
    #    return 602;
    #}
    
    location ~ /helloworld {
        return 603;
    }   

    2、测试普通字符串的长短(普通字符串的匹配与顺序无关,与长短有关)。浏览器输入http://localhost/helloworld/test/a.html,返回601。浏览器输入http://localhost/helloworld/a.html,返回602。

    location /helloworld/test/ {        #1
        return 601;
    }
            
    location /helloworld/ {                #2
        return 602;
    }

    3、测试正则表达式的顺序(正则匹配与顺序相关)。浏览器输入http://localhost/helloworld/test/a.html,返回602;将#2和#3调换顺序,浏览器输入http://localhost/helloworld/test/a.html,返回603

    location /helloworld/test/ {        #1
        return 601;
    }
    
    location ~ /helloworld {            #2
        return 602;
    }
            
    location ~ /helloworld/test {        #3
        return 603;
    }

    4、总结

    location = / {  
       #规则A  
    }  
    location = /login {  
       #规则B  
    }  
    location ^~ /static/ {  
       #规则C  
    }  
    location ~ .(gif|jpg|png|js|css)$ {  
       #规则D  
    }  
    location ~* .png$ {  
       #规则E  
    }  
    location !~ .xhtml$ {  
       #规则F  
    }  
    location !~* .xhtml$ {  
       #规则G  
    }  
    location / {  
       #规则H  
    }  
    访问根目录/, 比如http://localhost/ 将匹配规则A
    访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
    访问 http://localhost/static/a.html 将匹配规则C
    访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到 规则C
    访问 http://localhost/a.PNG 则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。
    访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
    访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。

     

  • 相关阅读:
    [1041] XX easy problem
    [1027] 火焰纹章
    省赛选拔赛解题报告
    二维DP hdu 1421 搬寝室问题
    最长斐波那契子序列选取(离散化 + 二分 + DP)
    两个有序链表合成一个有序链表
    CodeForce 608B Hamming Distance Sum
    CodeForce 607A&&608C Chain Reaction
    (DP)最大价值事件序列选取
    面向对象>>>抽象类>>>鸭子类型>>>封装
  • 原文地址:https://www.cnblogs.com/zwdx/p/9365953.html
Copyright © 2011-2022 走看看