文章目录

  • Nginx Location 优先级
  • 测试环境
  • 总结
  • 1.`=`号前缀(精确匹配)
  • 2.`^~`号前缀
  • 3.`~`, `~*`号前缀
  • 3. 无前缀


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

总结

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

nginx 泛域名优先级 nginx 配置优先级_nginx 泛域名优先级

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'";
    }
}

结果

nginx 泛域名优先级 nginx 配置优先级_nginx 泛域名优先级_02

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'";
    }
}

结果

nginx 泛域名优先级 nginx 配置优先级_优先级_03

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 '";
    }
}

结果

nginx 泛域名优先级 nginx 配置优先级_优先级_04

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

nginx 泛域名优先级 nginx 配置优先级_正则表达式_05

# 测试访问地址 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

nginx 泛域名优先级 nginx 配置优先级_正则表达式_06

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 '";
    }
}

结果

nginx 泛域名优先级 nginx 配置优先级_优先级_07

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 '";
    }
}

结果

nginx 泛域名优先级 nginx 配置优先级_正则表达式_08