Nginx配置 - Location 优先级

Nginx Location 优先级

测试环境

  • Nginx 1.10.3 (nginx version: nginx/1.10.3 (Ubuntu))
  • Ubuntu 16.04.4 LTS x64 (ip:192.168.241.132)
  • Chrome (Win10)
  • Win10 Host

总结

以下为个人小结测试,如文中有错误,欢迎指出,谢谢!

在这里插入图片描述

1.所有规则分配到各自类,优先级如下,找到第一个符合的类别即停止

2.在符合的类别中搜索所有符合的规则,按照下述示例处理冲突

1.=号前缀(精确匹配)

优先级最高,首选,无论位置

精确匹配只有完全符合才会匹配

# 访问http://192.168.241.132/demo, http://192.168.241.132/demo?args=....都会匹配
# 访问http://192.168.241.132/demo123 不会被匹配
server {
        listen 80;
        location = /demo {
                default_type text/plain;
                return 403 "= /demo";
        }
 }

配置文件

# 测试访问地址 http://192.168.241.132/equal_prefix
server {
	listen 80;
    
    # 无符号前缀,并且一致,在前面
    location /equal_prefix {
    	default_type text/plain;
    	return 403 "Reuqest Rule '/equal_prefix'";
    }
    
    # 大小写敏感的正则表达式,能匹配,并且在前面
    location ~ /equal_.* {
    	default_type text/plain;
    	return 403 "Reuqest Rule ' ~ /equal_.*'";
    }
    # 大小写不敏感的正则表达式,能匹配,并且在前面
    location ~* /equal_.* {
    	default_type text/plain;
    	return 403 "Reuqest Rule ' ~* /equal_.*'";
    }

    # ^~ 前缀,能匹配,在前面
    location ^~ /equal_.*{
    	default_type text/plain;
    	return 403 "Reuqest Rule ' ^~ /equal_.*'";
    }
    
    # 假如还有这个,Nginx会报错,无法启动
    # nginx: [emerg] duplicate location "/equal_prefix"
    # 因此忽略 一致匹配的情况下 前后优先级的问题
    # location ^~ /equal_prefix {
    #	default_type text/plain;
    #	return 403 "Reuqest Rule '^~ /equal_prefix'";
    # }
    
    
    # = 精确匹配
    location = /equal_prefix {
    	default_type text/plain;
    	return 403 "Reuqest Rule '= /equal_prefix'";
    }
}

结果

在这里插入图片描述

2.^~号前缀

除 = 号前缀外,优先级第二,并且最长优先,无论位置

配置文件

# 测试访问地址 http://192.168.241.132/Caret-RegEx
server {
	listen 80;
    
    # 假如还有这个,Nginx会报错,无法启动
    # nginx: [emerg] duplicate location
    # 因此忽略 和 ^~ /Caret-RegEx  一致匹配的情况下 前后优先级的问题
    # 无符号前缀,并且一致,在前面
    # location /Caret-RegEx {
    #	default_type text/plain;
    #    return 403 "Reuqest Rule '/Caret-RegEx'";
    #}
    
    # 无前缀,部分匹配,在前面
    location /Caret- {
    	default_type text/plain;
        return 403 "Reuqest Rule ' /Caret-'";
    }
    
    # 大小写不敏感的正则表达式,并且在前面
    location ~* /Caret-RegEx {
    	default_type text/plain;
        return 403 "Reuqest Rule '~* /Caret-RegEx'";
    }
    
    # 大小写敏感的正则表达式,并且在前面
    location ~ /Caret-RegEx {
    	default_type text/plain;
        return 403 "Reuqest Rule '~ /Caret-RegEx'";
    }
    
    # ^~ 前缀的, 较短,在前面
    location ^~ /Caret {
    	default_type text/plain;
        return 403 "Reuqest Rule '^~ /Caret '";
    }
    
    # ^~ 前缀的,较长,在后面
    location ^~ /Caret-RegEx {
    	default_type text/plain;
        return 403 "Reuqest Rule '^~ /Caret-RegEx'";
    }
}

结果

在这里插入图片描述

3.~, ~*号前缀

配置文件
1.同样符合的正则表达式情况下,不区分长短,不区分精确字符个数,位置前面优先

# 测试访问地址 http://192.168.241.132/RegEx
server {
	listen 80;
    
    # 正则表达式, 较短,并且不完全匹配
    location ~ /Reg {
    	default_type text/plain;
        return 403 "Reuqest Rule ' ~ /Reg'";
    }
    
    # 正则表达式, 精确字符个数较少
    location ~ /Reg.* {
    	default_type text/plain;
        return 403 "Reuqest Rule ' ~ /Reg'";
    }
    
    # 正则表达式, 精确字符个数较多
    location ~ /RegEx {
    	default_type text/plain;
        return 403 "Reuqest Rule ' /RegEx '";
    }
}

结果

在这里插入图片描述

2. ~与~*优先级一致 ,位置前面优先

# 测试访问地址 http://192.168.241.132/RegEx
server {
	listen 80;
    
    location ~ /RegEx {
    	default_type text/plain;
        return 403 "Reuqest Rule ' ~ /RegEx '";
    }
    
    location ~* /RegEx {
    	default_type text/plain;
        return 403 "Reuqest Rule '  ~* /RegEx '";
    }
}

结果1

在这里插入图片描述

# 测试访问地址 http://192.168.241.132/RegEx
server {
	listen 80;
    
    location ~* /RegEx {
    	default_type text/plain;
        return 403 "Reuqest Rule '  ~* /RegEx '";
    }
    
    location ~ /RegEx {
    	default_type text/plain;
        return 403 "Reuqest Rule ' ~ /RegEx '";
    }
}

结果2

在这里插入图片描述

3.优先级比无前缀要高,即使位置在后面

# 测试访问地址 http://192.168.241.132/RegEx
server {
	listen 80;
    
    location /RegEx {
    	default_type text/plain;
        return 403 "Reuqest Rule ' /RegEx '";
    }
    
    location ~ /RegEx {
    	default_type text/plain;
        return 403 "Reuqest Rule ' ~ /RegEx '";
    }
}

结果

在这里插入图片描述

3. 无前缀

优先级最低, 同样无前缀情况下,精确度最长的优先

# 测试访问地址 http://192.168.241.132/non-prefix
server {
	listen 80;
    # 精确度低,但在前面
    location /non {
    	default_type text/plain;
        return 403 "Reuqest Rule ' /non '";
    }
    # 精确度高,但在后面
    location /non-prefix {
    	default_type text/plain;
        return 403 "Reuqest Rule ' /non-prefix '";
    }
}

结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值