Nginx系列教程(6)Nginx location 匹配规则详细解说

简介:
+关注继续查看

Nginx 的 location 实现了对请求的细分处理,有些 URI 返回静态内容,有些分发到后端服务器等,今天来彻底弄懂它的匹配规则

一个最简单的 location 的例子如下

server {
    server_name website.com;
    location /admin/ {
    # The configuration you place here only applies to
    # http://website.com/admin/
    }
}
复制代码

location 支持的语法 location [=|~|~*|^~|@] pattern { ... },乍一看还挺复杂的,来逐个看一下。

location修饰符类型

「=」 修饰符:要求路径完全匹配

server {
    server_name website.com;
    location = /abcd {
    […]
    }
}
复制代码
  • http://website.com/abcd匹配
  • http://website.com/ABCD可能会匹配 ,也可以不匹配,取决于操作系统的文件系统是否大小写敏感(case-sensitive)。ps: Mac 默认是大小写不敏感的,git 使用会有大坑。
  • http://website.com/abcd?param1&param2匹配,忽略 querystring
  • http://website.com/abcd/不匹配,带有结尾的/
  • http://website.com/abcde不匹配

「~」修饰符:区分大小写的正则匹配

server {
    server_name website.com;
    location ~ ^/abcd$ {
    […]
    }
}
复制代码

^/abcd$这个正则表达式表示字符串必须以/开始,以$结束,中间必须是abcd

  • http://website.com/abcd匹配(完全匹配)
  • http://website.com/ABCD不匹配,大小写敏感
  • http://website.com/abcd?param1&param2匹配
  • http://website.com/abcd/不匹配,不能匹配正则表达式
  • http://website.com/abcde不匹配,不能匹配正则表达式

「~*」不区分大小写的正则匹配

server {
    server_name website.com;
    location ~* ^/abcd$ {
    […]
    }
}
复制代码
  • http://website.com/abcd匹配 (完全匹配)
  • http://website.com/ABCD匹配 (大小写不敏感)
  • http://website.com/abcd?param1&param2匹配
  • http://website.com/abcd/ 不匹配,不能匹配正则表达式
  • http://website.com/abcde 不匹配,不能匹配正则表达式

「^~」修饰符:前缀匹配 如果该 location 是最佳的匹配,那么对于匹配这个 location 的字符串, 该修饰符不再进行正则表达式检测。注意,这不是一个正则表达式匹配,它的目的是优先于正则表达式的匹配

查找的顺序及优先级

当有多条 location 规则时,nginx 有一套比较复杂的规则,优先级如下:

  • 精确匹配 =
  • 前缀匹配 ^~(立刻停止后续的正则搜索)
  • 按文件中顺序的正则匹配 ~~*
  • 匹配不带任何修饰的前缀匹配。

这个规则大体的思路是

先精确匹配,没有则查找带有 ^~的前缀匹配,没有则进行正则匹配,最后才返回前缀匹配的结果(如果有的话)

如果上述规则不好理解,可以看下面的伪代码(非常重要)

function match(uri):
  rv = NULL
  
  if uri in exact_match:
    return exact_match[uri]
  
  if uri in prefix_match:
    if prefix_match[uri] is '^~':
      return prefix_match[uri]
    else:
      rv = prefix_match[uri] // 注意这里没有 return,且这里是最长匹配
   
  if uri in regex_match:
    return regex_match[uri] // 按文件中顺序,找到即返回
  return rv
复制代码

一个简化过的Node.js写的代码如下

function ngx_http_core_find_location(uri, static_locations, regex_locations, named_locations, track) {
  let rc = null;
  let l = ngx_http_find_static_location(uri, static_locations, track);
  if (l) {
    if (l.exact_match) {
      return l;
    }
    if (l.noregex) {
      return l;
    }
    rc = l;
  }
  if (regex_locations) {
    for (let i = 0 ; i < regex_locations.length; i ++) {
      if (track) track(regex_locations[i].id);
      let n = null;
      if (regex_locations[i].rcaseless) {
        n = uri.match(new RegExp(regex_locations[i].name));
      } else {
        n = uri.match(new RegExp(regex_locations[i].name), "i");
      }
      if (n) {
        return regex_locations[i];
      }
    }
  }

  return rc;
}
复制代码

案例分析

案例 1

server {
    server_name website.com;
    location /doc {
        return 701; # 用这样的方式,可以方便的知道请求到了哪里
    }
    location ~* ^/document$ {
        return 702; # 用这样的方式,可以方便的知道请求到了哪里

    }
}

curl -I  website.com:8080/document
HTTP/1.1 702
复制代码

按照上述的规则,第二个会有更高的优先级

案例2

server {
    server_name website.com;
    location /document {
        return 701;
    }
    location ~* ^/document$ {
        return 702;
    }
}
curl -I  website.com:8080/document

复制代码

第二个匹配了正则表达式,优先级高于第一个普通前缀匹配

案例 3

server {
    server_name website.com;
    location ^~ /doc {
        return 701;
    }
    location ~* ^/document$ {
        return 702;
    }
}
curl http://website.com/document
HTTP/1.1 701
复制代码

第一个前缀匹配^~命中以后不会再搜寻正则匹配,所以会第一个命中

案例 4

server {
    server_name website.com;
    location /docu {
        return 701;
    }
    location /doc {
        return 702;
    }
}
复制代码

curl -I website.com:8080/document 返回 HTTP/1.1 701

server {
    server_name website.com;
    location /doc {
        return 702;
    }
    location /docu {
        return 701;
    }
}
复制代码

curl -I website.com:8080/document 依然返回 HTTP/1.1 701

前缀匹配下,返回最长匹配的 location,与 location 所在位置顺序无关

案例 5

server {
    listen 8080;
    server_name website.com;

    location ~ ^/doc[a-z]+ {
        return 701;
    }

    location ~ ^/docu[a-z]+ {
        return 702;
    }
}
复制代码

curl -I website.com:8080/document 返回 HTTP/1.1 701

把顺序换一下

server {
    listen 8080;
    server_name website.com;

    location ~ ^/docu[a-z]+ {
        return 702;
    }
    
    location ~ ^/doc[a-z]+ {
        return 701;
    }
}
复制代码

curl -I website.com:8080/document 返回 HTTP/1.1 702

正则匹配是使用文件中的顺序,找到返回

本系列往期文章参考

Nginx系列教程(1) nginx基本介绍和安装入门

Nginx系列教程(2)nginx搭建静态资源web服务器

Nginx系列教程(3)nginx缓存服务器上的静态文件

Nginx系列教程(4)nginx处理web应用负载均衡问题以保证高并发

Nginx系列教程(5)如何保障nginx的高可用性(keepalived)

相关文章
|
3天前
|
Ubuntu 应用服务中间件 nginx
百度搜索:蓝易云【Ubuntu 18.04系统编译安装Nginx 1.22教程。】
现在,您已经成功地在Ubuntu 18.04上编译和安装了Nginx 1.22。您可以通过在浏览器中访问服务器的IP地址来验证Nginx是否正常运行。请确保在实际操作中根据您的需求进行适当的配置和调整。请注意,通过编译安装方式安装的Nginx不会自动更新,您需要手动更新版本或进行维护。
24 1
|
11天前
|
应用服务中间件 nginx
百度搜索:蓝易云【Debian11系统编译安装Nginx教程。】
以上是在Debian 11系统上编译安装Nginx的基本步骤。请根据实际情况进行相应的调整和配置。
19 2
|
13天前
|
应用服务中间件 Linux 网络安全
百度搜索:蓝易云【Cnetos7编译安装Nginx教程。】
现在,您已经成功在CentOS 7上通过编译安装了Nginx。请注意,以上步骤提供了基本的指导,实际操作可能会有所差异。如有需要,您可以参考Nginx官方文档或社区资源获取更详细的信息和帮助。
32 0
|
1月前
|
负载均衡 应用服务中间件 Linux
|
1月前
|
缓存 负载均衡 Java
Nginx负载均衡配置教程-Linux
Nginx负载均衡配置教程-Linux
69 0
|
2月前
|
应用服务中间件 Linux 网络安全
虚拟机Centos下载安装Nginx并安装ssl模块——小白教程
虚拟机Centos下载安装Nginx并安装ssl模块——小白教程
64 0
|
3月前
|
弹性计算 前端开发 应用服务中间件
React全家桶建站教程-Nginx #3
React全家桶建站教程-Nginx #3
45 0
|
6月前
|
人工智能 负载均衡 应用服务中间件
建议收藏chatGPT说的编译安装nginx教程
写在前面 这个是当下最流行最时髦的AI神器chatGPT和我一起合作写的一篇通用技术文章,请读者笑纳! chatGPT说 咚咚咚,咚咚咚,嘿嘿;咚咚咚,咚咚咚,嘿嘿;AI等一会,我来发答案,看图!!! 命令演示 首先,从nginx官方网站(https://nginx.org/en/download.html)下载最新的nginx源码包。 这里我们选择Stable version下的1.22.1版本,先下载然后解压源码包,具体步骤如下: wget https://nginx.org/download/nginx-1.22.1.tar.gz tar -zxvf nginx-1.22.1.ta
185 0
|
7月前
|
缓存 负载均衡 安全
Nginx 负载均衡配置(实战教程)
还在等什么,快来一起讨论关注吧,公众号【八点半技术站】,欢迎加入社群
Nginx 负载均衡配置(实战教程)
|
10月前
|
数据可视化 前端开发 应用服务中间件
flask+nginx+uwsgi部署服务器(详细保姆级教程)
本次项目我利用flask写了接口需要部署到服务器供前端使用,一路走来爬了很多坑,所以这一次做了详细的记录,从零开始教大家将flask项目跑起来
683 1
flask+nginx+uwsgi部署服务器(详细保姆级教程)
相关产品
云迁移中心
推荐文章
更多