广州总部电话:020-85564311
广州总部电话:020-85564311

广州网站建设-小程序商城开发-广州小程序开发-企业微信开发公司-网站建设高端品牌-优网科技

20年
互联网应用服务商
请输入搜索关键词
知识库 知识库

优网知识库

探索行业前沿,共享知识宝库

nginx 路径匹配规则是怎样的?
发布日期:2025-05-14 18:22:53 浏览次数: 806 来源:打破砂锅聊技术

当一个服务的 nginx 配置路径规则较多时,存在多条规则可以匹配到一个url可能,这种情况下nginx 是如何决定使用哪条规则呢?在配置规则比较多的时候,会经常遇到这个问题。

Location定义规则是怎样的?

nginx 官方文档中语法规则定义如下:


location [ = | ~ | ~* | ^~ ] uri

说明:

  1. uri 有两种格式

    a)前缀字符串 prefix string,前缀字符串匹配必须是从开头精确匹配,例如  /my-site/some/path 就不会匹配  /some/path/

    b)正则表达式,正则表达式规范为 Perl-Compatible-Rx。

  2. 匹配符号有五种情况,<空> =^~~~* , 匹配符的术语叫modifier。

  3. 其中,<空>, =^~ 适用于前缀表达式

  4. 另外两种~开头的适用正则表达式, “~” modifier (for case-sensitive matching) 即大小写敏感, “~*” modifier (for case-insensitive matching) 大小写不敏感。

匹配顺序是怎样的?

当有多条规则可以匹配到一个url 时,nginx 是如何决定使用哪条规则呢?这就是匹配顺序的问题。

nginx 官方文档 是这样描述的

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.

另一个文档  里这样描述:

  1. Test the URI against all prefix strings.
  2. The = (equals sign) modifier defines an exact match of the URI and a prefix string. If the exact match is found, the search stops.
  3. If the ^~ (caret-tilde) modifier prepends the longest matching prefix string, the regular expressions are not checked.
  4. Store the longest matching prefix string.
  5. Test the URI against regular expressions.
  6. Stop processing when the first matching regular expression is found and use the corresponding location.
  7. If no regular expression matches, use the location corresponding to the stored prefix string.

总结:

  1. 先检查所有前缀表达式规则(非正则)。
  2. 如果有精确匹配 =规则匹配,使用该规则。
  3. 其余规则中,计算哪一条是“最长匹配”。
  4. 如果最长匹配的这一条 匹配符号是 ^~ 开头 ,则停止匹配并使用该条规则。
  5. 按配置文件顺序检查所有正则规则,遇到匹配的即Stop且使用。
  6. 如果正则规则中没有匹配项,则使用4记录的最长匹配规则。

能否举个例子?

这是来自官方文档的一个例子。


location = / {    [ configuration A ]}
location / {    [ configuration B ]}
location /documents/ {    [ configuration C ]}
location ^~ /images/ {    [ configuration D ]}
location ~* \.(gif|jpg|jpeg)$ {    [ configuration E ]}
  • The “/” request will match configuration A,  精确匹配
  • the “/index.html” request will match configuration B, 因为仅 B规则匹配。
  • the “/documents/document.html” request will match configuration C。 B和C都匹配,C 最长,且正则均不匹配
  • the “/images/1.gif” request will match configuration D。 B + D,D匹配最长,且是~- 匹配符,Stop,不检查正则。
  • the “/documents/1.jpg” request will match configuration E.  B 和 E匹配,B 没有~- , 需挨个检查正则, E正则匹配,Stop并使用。。

参考文档有哪些?

  • https://nginx.org/en/docs/http/ngx_http_core_module.html#location

  • https://docs.nginx.com/nginx/admin-guide/web-server/web-server/#configuring-locations


优网科技,优秀企业首选的互联网供应服务商

优网科技秉承"专业团队、品质服务" 的经营理念,诚信务实的服务了近万家客户,成为众多世界500强、集团和上市公司的长期合作伙伴!

优网科技成立于2001年,擅长网站建设、网站与各类业务系统深度整合,致力于提供完善的企业互联网解决方案。优网科技提供PC端网站建设(品牌展示型、官方门户型、营销商务型、电子商务型、信息门户型、DIY体验、720全景展厅及3D虚拟仿真)、移动端应用(手机站APP开发)、微信定制开发(微信官网、微信商城、企业微信)、微信小程序定制开发等一系列互联网应用服务。


我要投稿

姓名

文章链接

提交即表示你已阅读并同意《个人信息保护声明》

专属顾问 专属顾问
扫码咨询您的优网专属顾问!
专属顾问
马上咨询
联系专属顾问
联系专属顾问
联系专属顾问
扫一扫马上咨询
扫一扫马上咨询

扫一扫马上咨询

和我们在线交谈!