zoukankan      html  css  js  c++  java
  • Nignx(四)location语法详解

    1. Location语法优先级排列

    匹配符 匹配规则 优先级
    =    精确匹配    1
    ^~    以某个字符串开头    2
    ~    区分大小写的正则匹配    3
    ~*    不区分大小写的正则匹配    4
    !~    区分大小写不匹配的正则    5
    !~*    不区分大小写不匹配的正则    6
    /    通用匹配,任何请求都会匹配到    7

    (location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)

    2. nginx.conf配置文件实例

    server {
        listen 80;
        server_name pythonav.cn;
    
        #优先级1,精确匹配,根路径
        location =/ {
            return 400;
        }
    
        #优先级2,以某个字符串开头,以av开头的,优先匹配这里,区分大小写
        location ^~ /av {
           root /data/av/;
        }
    
        #优先级3,区分大小写的正则匹配,匹配/media*****路径
        location ~ /media {
              alias /data/static/;
        }
    
        #优先级4 ,不区分大小写的正则匹配,所有的****.jpg|gif|png 都走这里
        location ~* .*.(jpg|gif|png|js|css)$ {
           root  /data/av/;
            }
    
        #优先7,通用匹配
        location / {
            return 403;
        }
    }

    3. nginx语法之root和alias区别实战

    注意:本地测试前提前配置SwitchHosts,映射tests.com为本地IP

    server {
        listen       80;
        server_name  tests.com;
        
        # http://tests.com/ccc.png => ‪D: estccc.png
        # http://http://tests.com/index3.html => ‪D: estindex3.html
        # http://tests.com/tupian/bb.png => D: est upianb.png
        location ~.(html|js|css|png|gif|icon|jpg|ttf|woff|woff2|properties|eot|svg|php|ico|map|swf|asp|php|aspx|jsp|do|action|shtml|htm|txt|apk|json)$ {
            root   D://test;
            index index.html;
        }
        
        # http://tests.com/tupian/bb.png => ‪D: est upianb.png
        location ^~ /tupian {
            root   D://test;
        }
        
        # http://tests.com/yemian/ => D: estyemianindex.html
        location /yemian {
            root   D://test;
            index index.html;
        }
        
        # http://tests.com/aliasym/index2.html => D: estindex2.html
        location ^~ /aliasym {
            alias   D://test;
        }
        
        # http://tests.com/ => ‪D: estindex2.html
        location / {
            root   D://test;
            index index2.html;
        }
        
        # http://tests.com/ => ‪D: estindex.html
        location =/ {
            rewrite / /index.html break;
            root   D://test;
        }
    }
  • 相关阅读:
    restore db fail
    MAC 设置环境变量path的常用方法
    python编码问题 decode与encode
    python 正则表达式 提取网页中标签的中文
    转:Linux实时将所有输出重定向到文件
    selenium 定位无标签的元素
    re.compile
    Python urllib.quote
    Shell #*/ 和 %/*
    [转载] python必碰到的问题---encode与decode,中文乱码
  • 原文地址:https://www.cnblogs.com/yifanSJ/p/12069020.html
Copyright © 2011-2022 走看看