Nginx location匹配规则的方法示例

1、语法

  location [=|~|~*|^~|@] /uri/ {   ...  }  

2、说明

从上面的语法出发,可以了解到 location 可以区分为三个部分,接下来一个一个的研究一下。

1) [=|~|~*|^~|@]

  • = : 表示精确匹配后面的url
  • ~ : 表示正则匹配,但是区分大小写
  • ~* : 正则匹配,不区分大小写
  • ^~ : 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
  • @ : "@" 定义一个命名的 location,使用在内部定向时,例如 error_page

上面定义了几个不同的符号,表示不同的匹配规则,那么先后顺序呢?

  1. = 前缀的指令严格匹配这个查询。如果找到,停止搜索;
  2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用 ^~ 前缀,搜索停止;
  3. 正则表达式,在配置文件中定义的顺序;
  4. 如果第 3 条规则产生匹配的话,结果被使用。否则,使用第 2 条规则的结果。

测试示例1:

  location = /world {   return 600;  }    location = /hello {   return 600;  }    location ~ /hellowo {   return 602;  }    location ^~ /hello {   return 601;  }    

  - 请求 localhost/world 返回600  - 请求 localhost/world2 localhost/test/world 返回其他  - 请求 localhost/hello 返回600  - 请求 localhost/hello/123 返回601  - 请求 localhost/hellow 返回601  - 请求 localhost/hellowo 返回601  - 请求 localhost/test/hellowo 返回602  - 请求 localhost/test/hello 返回其他

因此可以知道:

  • = 是精确完整匹配,且优先级最高;
  • 正则匹配时,如果 ~ 和 ^~ 同时匹配规则,则 ^~ 优先;
  • ^~ 这个规则不会匹配请求 url 中后面的路径,如上面的 /test/hello 没有匹配上
  • ^~ 不支持正则,和 = 相比,范围更广,hellowo 是可以被 ^~ 匹配,但是 = 不会匹配;
  • ~ 路径中只要包含就可以匹配,如上面的 /test/hellowo 返回了 602

测试示例2:

  location ~ /hello {   return 602;  }    location ~ /helloworld {   return 601;  }    

  - 请求 localhost/world/helloworld 返回 602  - 请求 localhost/helloworld 返回 602

调整上面的顺序

  location ~ /helloworld {   return 601;  }    location ~ /hello {   return 602;  }    

  - 请求 localhost/helloworld 返回601  - 请求 localhost/world/helloworld 返回601  - 请求 localhost/helloWorld 返回602  

所以同时正则匹配时

  • 放在前面的优先匹配
  • 注意如果不区分大小写时,使用 ~*
  • 尽量将精确匹配的放在前面

测试示例3:

  location ^~ /hello/ {   return 601;  }    location /hello/world {   return 602;  }    

这种场景中,存在一个没有符合的路由规则,那么实际的测试是怎样呢?

  - http://localhost/hello/wor 返回601  - http://localhost/hello/world 返回602  - http://localhost/hello/world23 返回602  - http://localhost/hello/world/123 返回602

从上面的示例可以看出

  • 没有符合时,全匹配是优先 ^~ 的

2) [uri]

这里主要填的是需要匹配的 path 路径,根据前面的符号,这里可以填写精确到 path 路径,也可以填正则表达式,下面则主要针对正则进行说明

  • . : 匹配除换行符以外的任意字符
  • ? : 重复0次或1次
  • + : 重复1次或更多次
  • * : 重复0次或更多次
  • d :匹配数字
  • ^ : 匹配字符串的开始
  • $ : 匹配字符串的介绍
  • {n} : 重复n次
  • {n,} : 重复n次或更多次
  • [c] : 匹配单个字符c
  • [a-z] : 匹配a-z小写字母的任意一个
  • 小括号()之间匹配的内容,可以在后面通过$1来引用,$2表示的是前面第二个()里的内容。正则里面容易让人困惑的是转义特殊字符。

路由转发

请求 path 匹配只是第一步,匹配完成之后,如何将请求转发给其它的 web 服务呢?

1、反向代理

通常可见的一种使用姿势就是使用 nginx 代理请求,转发到内部的其它 web 服务上

主要通过 prixy_pass 来实现

  location ^~ /webs {   proxy_pass http://127.0.0.1:8080/webs;  }  

上面规则的含义是,将所有以 webs 开头的请求,转发到 8080 端口的 web 服务上。

上面是直接写死转发到一个 ip 上,如果是多个机器提供服务,可以这样配置

  ## 下面放在http的括号内,作为第一层  upstream test.online {   server 120.11.11.11:8080 weight=1;   server 120.11.11.12:8080 weight=1;  }    location ^~ /webs {   proxy_pass http://test.online;   proxy_redirect default;  }    

2、Rewrite 命令

rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。

rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用, 如

http://jb51.net/a/we/index.php?id=1&u=str

只对/a/we/index.php重写。

语法: rewrite regex replacement [flag];

示例:

  location ^~ /hexo {   root '/Users/yihui/GitHub/';  }    location ~ /hello {   rewrite ^(/hello).*$ /hexo/public/index.html last;   return 603;  }    

将hello开头的,全部转发到/hexo/public/index.html

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支Fatmouse

参与评论