【Nginx学习系列】location匹配规则

location 匹配规则

规则匹配
=严格匹配。如果请求匹配这个 location,那么将停止搜索并立即处理此请求
~区分大小写匹配(可用正则表达式)
~*不区分大小写匹配(可用正则表达式)
!~区分大小写不匹配
!~*不区分大小写不匹配
^~前缀匹配
@“@” 定义一个命名的location,使用在内部定向时
/通用匹配,任何请求都会匹配到

location 原文

A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching). To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.
If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.
Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.

location 的定义分为两种:

  • 前缀字符串(prefix string)
  • 正则表达式(regular expression),具体为前面带 ~* 和 ~ 修饰符的

而匹配 location 的顺序为:

  1. 检查使用前缀字符串的 locations,在使用前缀字符串的 locations 中选择最长匹配的,并将结果进行储存。
  2. 如果符合带有 = 修饰符的 URI,则立刻停止匹配
  3. 如果符合带有 ^~ 修饰符的 URI,则也立刻停止匹配。
  4. 如果不满足2和3,使用1中记住的最长匹配前缀字符串location,按照定义文件的顺序,检查正则表达式,匹配到就停止。
  5. 当正则表达式匹配不到的时候,使用之前储存的前缀字符串
    在这里插入图片描述

location的两种语法

约定: []表示里面的参数能省略, <>表示里面的参数不能省略.

第一种语法分为3个部分, 分别是: location关键字+@name别名(name是自己取的名字)+如何处理, 这个语法很简单, 就是做内部跳转, 这里不讨论了.

location @name { … }

第二种语法分为4个部分, 分别是: location关键字 + 匹配方式符号(可省略)+匹配规则+如何处理, 这个最复杂也是最常用, 我们只讨论这个.

location [ = | ~ | ~* | ^~ ] uri { … }

普通匹配和正则匹配

[ = | ~ | ~* | ^~ ]分为两种匹配模式, 分别是普通匹配和正则匹配.

普通匹配概述
  • = : 这代表精准匹配全路径, 命中它后直接返回, 不再进行后续匹配, 优先级最高.

  • ^~ : 这代表精准匹配开头, 命中开头后直接返回, 不再进行后续匹配, 优先级第二.

  • 无匹配方式符号 : 这代表通用性匹配, 命中后还会继续后续匹配, 最后选取路径最长的匹配, 并储存起来, 优先级第四.

正则匹配概述
  • ~: 这是区分大小写的正则匹配, 命中后则不进行后续匹配, 立即返回, 优先级第三.

  • *~: 不区分大小写的正则匹配, 命中后则不进行后续匹配, 立即返回, 优先级第三.

注:正则匹配中 ~ 和 * ~ 优先级一样, 它们按照从上到下的顺序进行匹配, 最先命中的立即返回, 后续的不会进行匹配, 所以精细的正则匹配规则往前放, 通用的正则匹配规则往后放.

location 匹配顺序

  • “=” 精准匹配,如果匹配成功,则停止其他匹配
  • 普通字符串指令匹配,优先级是从长到短(匹配字符越多,则选择该匹配结果)。匹配成功的location如果使用^~,则停止其他匹配(正则匹配)
  • 正则表达式指令匹配,按照配置文件里的顺序(从上到下),成功就停止其他匹配,如果正则匹配成功,使用该结果;否则使用普通字符串匹配结果

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

即:

(精确匹配)> (最长字符串匹配,但完全匹配) >(非正则匹配)>(正则匹配)>(最长字符串匹配,不完全匹配)>(location通配)

location 匹配案例

server {
	listen 9876;
	
	location = / {  
		return 200 "精确匹配/";
	}
	
	location ~* /ma.*ch {
		return 200 "正则匹配/ma.*ch";
	}
	
	location ~ /mat.*ch {
		return 200 "正则匹配/match.*";
	}
	
	
	location = /test {
		return 200 "精确匹配/test";
	}
	
	location ^~ /test/ {
		return 200 "前缀匹配/test";
	}
	
	location /test/hello/world {
		return 200 "完全匹配/test/hello/world";
	}
	location /test/hello {
		return 200 "部分匹配/test/hello";
	}
	
	location ~ /test/he*o {
		return 200 "正则匹配/test/he*o";
	}
	location /document {
        return 200 "/document";
    }
    location ~ /docu* {
        return 200 "~/docu";
     }
	location / {  
		return 200 "通配/";
	}

}
curl localhost:9876/test 
精确匹配/test
curl localhost:9876/test/hello/world
完全匹配/test/hello/world
curl localhost:9876/test/
前缀匹配/test
curl localhost:9876/test/hell
前缀匹配/test
curl localhost:9876/test/hello/aaa
部分匹配/test/hello
curl localhost:9876/test/heao
前缀匹配/test  
curl localhost:9876/match
正则匹配/ma.*ch
curl localhost:9876/test/hello/world/ss
完全匹配/test/hello/world
curl localhost:9876/document
~/docu

上面例子中最后一个需要好好理解

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: nginxlocation匹配规则是根据请求的URI(Uniform Resource Identifier)来匹配location指令中的模式,从而确定应该由哪个location块来处理该请求。nginxlocation匹配规则有以下几种: 1. 精确匹配:如果location指令中的模式与请求的URI完全一致,则匹配成功。 2. 前缀匹配:如果location指令中的模式是以“/”开头的字符串,则匹配请求URI中以该字符串开头的部分。 3. 正则匹配:如果location指令中的模式是一个正则表达式,则匹配请求URI与该正则表达式匹配结果。 4. 最长前缀匹配:如果有多个location指令的模式都能匹配请求URI,则选择最长的那个模式来处理请求。 5. 通用匹配:如果没有任何location指令能够匹配请求URI,则使用通用匹配来处理请求。 以上就是nginxlocation匹配规则,可以根据实际需求选择不同的匹配方式来处理请求。 ### 回答2: nginx是一个高性能的Web服务器、反向代理和负载均衡器,它广泛用于各种(动态)网站和Web应用程序的部署中。在nginx中,location是一个非常重要的配置指令,它可以帮助服务器根据请求的URL路径(URI)匹配指定的服务或文件。 location匹配规则如下: 1. 精确匹配(=):只有当请求URI与location指定的URI完全匹配时,才会执行该location指令之后的操作。 例如: location = /hello { return 200 "Hello, nginx!\n"; } 当访问/hello时,会直接返回"Hello, nginx!"字符串,不会再进行其他的匹配和处理。 2. 前缀匹配(^~):如果请求URI以location指定的前缀开头,那么该location指令之后的操作将被执行。 例如: location ^~ /images/ { alias /var/www/myapp/static/images/; } 当访问/images/logo.png时,该请求将被映射到服务器文件系统中的/var/www/myapp/static/images/logo.png文件。 3. 正则表达式匹配(~和~*):location指定的URI可以是一个正则表达式,如果请求URI与该正则表达式匹配成功,则该location指令之后的操作将被执行。 其中,~表示区分大小写的正则表达式匹配,而~*表示不区分大小写的正则表达式匹配。 例如: location ~ /users/([0-9]+)/photos/(.*)\.jpg$ { proxy_pass http://backend_server/photos/$1/$2.jpg; } 当访问/users/123/photos/myphoto.jpg时,该请求将被映射到http://backend_server/photos/123/myphoto.jpg后端服务器上进行处理。 4. 普通匹配:如果以上三种匹配规则都不匹配成功,那么nginx会采用普通匹配规则。首先,nginx会查找URI中是否包含文件扩展名,如果有,则nginx会按照扩展名指定的类型进行处理;如果没有,nginx则会使用server指令中指定的默认类型进行处理。 例如: location / { index index.html index.php; try_files $uri $uri/ /index.php?$query_string; } 当访问任何URI时,都会先查找该URI所对应的文件是否存在,如果存在,则直接返回该文件内容;否则,重定向到/index.php页面进行处理。 总之,在nginx中,location是一个非常强大的配置指令,可以根据不同的匹配规则,灵活地配置不同的服务和文件,从而提高网站的性能和稳定性。熟练掌握location匹配规则对于nginx的使用和维护都是非常重要的。 ### 回答3: Nginx是一种高性能的Web服务器和反向代理服务器,同时也是一个邮件代理服务器。当使用Nginx作为Web服务器时,可以使用location匹配规则来确定如何处理请求。location匹配规则Nginx服务器使用的一个非常重要的配置选项。它可让用户在指定的URL路径中,对请求做出特定的响应。 Nginxlocation匹配规则可以分为两种类型:正则和非正则。正则匹配是基于正则表达式匹配。它可以根据URL路径中的模式匹配,使用正则表达式的语法来表示匹配规则。非正则匹配是基于URL路径的匹配。它可以直接匹配特定的URL路径,而不需要使用正则表达式。 当Nginx服务器接收到一个请求时,它首先会按照配置文件中location的顺序进行匹配匹配成功后,Nginx会按照顺序处理该请求,直到找到一个匹配完全符合要求的location为止。如果找不到一个匹配完全符合请求的location,则会使用默认的location做出响应。 对于正则匹配规则,需要使用~或~*开头,其中~表示区分大小写,~*表示不区分大小写。比如:location ~ ^/download/.+\.(zip|rar)$ {},表示当请求路径以/download/开头,并且以.zip或.rar结尾时,使用当前配置块进行处理。 对于非正则匹配规则,需要使用=或^~开头,其中=表示完全匹配,^~表示使用前缀匹配。比如:location = /index.html {},表示当请求路径为/index.html时,使用当前配置块进行处理。 除了以上两种匹配规则外,Nginx还提供了一些其他的匹配规则,比如按文件夹优先匹配,按通用优先匹配等,具体可以根据业务场景来选择相应的匹配规则,来达到更好的匹配效果。 总之,Nginxlocation匹配规则非常重要,可以用来处理各种不同的请求,帮助Web服务器实现更加灵活的请求处理方式,提高Web服务器的性能和安全性。熟悉和掌握Nginxlocation匹配规则,对于Web服务器的运维和开发都有着非常大的帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值