zoukankan      html  css  js  c++  java
  • [nginx]location语法

    location语法

    location语法格式

    location [=|~|~*|^~] uri {
      ....
    }
    
    location    [=|~|~*|^~]    uri               {....}
    指令        匹配标识       匹配的网站地址    匹配URI后要执行的配置段
    

    nginx location匹配规则

    location匹配命令
    
    ~      # 波浪线表示执行一个正则匹配,区分大小写
    ~*     # 表示执行一个正则匹配,不区分大小写
    ^~     # ^~表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
    =      # 进行普通字符精确匹配
    @      # "@" 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
    
    
    location 匹配的优先级(与location在配置文件中的顺序无关)
    = 精确匹配会第一个被处理。如果发现精确匹配,nginx停止搜索其他匹配。
    普通字符匹配 正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
    ^~ 则只匹配该规则,nginx停止搜索其他匹配,否则nginx会继续处理其他location指令。
    最后匹配理带有""和"*"的指令 如果找到相应的匹配,则nginx停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。

    ~ 与~* 的区别

    location字段 说明
    ~ 匹配内容区分大小写
    ~* 匹配内容不区分的小写
    !~ 取反
    ^~ 但多个匹配同时存在,优先匹配 ^~匹配的内容;不做正则表达式的检查 (优先处理)

    location官方示例

    location = / {
        [ configuration A ]
    }
     
    location / {
        [ configuration B ]
    }
     
    location /documents/ {
        [ configuration C ]
    }
     
    location ^~ /images/ {
        [ configuration D ]
    }
     
    location ~* .(gif|jpg|jpeg)$ {
        [ configuration E ]
    }
    
    • 说明
    "/"                         请求将匹配配置A,
    "/index.html"               请求将匹配配置B,
    "/documents/document.html"  请求将匹配配置C,
    "/images/1.gif"             请求将匹配配置D,
    "/documents/1.jpg"          请求将匹配配置E.
    

    按匹配顺序: nginx location匹配规则

            location  = / {
                # 只匹配"/".
                [ configuration A ]
            }
            location ^~ /images/ {
                # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
                [ configuration C ]
            }
            location ~* .(gif|jpg|jpeg)$ {
                # 匹配以 gif, jpg, or jpeg结尾的请求.
                # 但是所有 /images/ 目录的请求将由 [Configuration C]处理.
                [ configuration D ]
            }
            location  / {
                # 匹配任何请求,因为所有请求都是以"/"开始
                # 但是更长字符匹配或者正则表达式匹配会优先匹配
                [ configuration B ]
            }
    
    • 不同uri及特殊字符组合匹配的顺序说明

    测试location的访问

    nginx服务企业应用
    修改返回值

        server {
            listen       80;
            server_name  www.maotai.com maotai.com;
            root   html;
    
            location = / {
                return 401;
            }
            location ^~ /images/ {
                return 402;
            }
            location ~* .(gif|jpg|jpeg)$ {
                return 403;
            }
            location /documents/ {
                return 404;
            }
            location / {
                return 502;
            }
        }
    
    [root@n1 nginx]# curl -I -w "%{http_code}
    " -o /dev/null -s  www.maotai.com
    401
    [root@n1 nginx]# curl -I -w "%{http_code}
    " -o /dev/null -s  www.maotai.com/images/1.jpg
    402
    [root@n1 nginx]# curl -I -w "%{http_code}
    " -o /dev/null -s  www.maotai.com/documents/ss.jpg
    403
    [root@n1 nginx]# curl -I -w "%{http_code}
    " -o /dev/null -s  www.maotai.com/documents/
    404
    [root@n1 nginx]# curl -I -w "%{http_code}
    " -o /dev/null -s  www.maotai.com/documents
    502
    
    
    
  • 相关阅读:
    Java 第十一届 蓝桥杯 省模拟赛 洁净数
    Java 第十一届 蓝桥杯 省模拟赛 第十层的二叉树
    Java 第十一届 蓝桥杯 省模拟赛 第十层的二叉树
    Java 第十一届 蓝桥杯 省模拟赛 第十层的二叉树
    Java 第十一届 蓝桥杯 省模拟赛 70044与113148的最大公约数
    Java 第十一届 蓝桥杯 省模拟赛 70044与113148的最大公约数
    20. Valid Parentheses
    290. Word Pattern
    205. Isomorphic Strings
    71. Simplify Path
  • 原文地址:https://www.cnblogs.com/iiiiher/p/8545244.html
Copyright © 2011-2022 走看看