Nginx 的 Location 配置指令块

简介: 最近一段时间在学习 Nginx ,以前一直对 Nginx 的 Location 配置很头大,最近终于弄出点眉目。总结如下:nginx 配置文件,自下到上分为三种层次分明的结构: |    http block        the protocol level |    server block        the server level V    location block        the requested URINginx 允许用户定义 Location block ,并指定一个匹配模式(pattern)匹配特定的 URI。

最近一段时间在学习 Nginx ,以前一直对 Nginx 的 Location 配置很头大,最近终于弄出点眉目。总结如下:
nginx 配置文件,自下到上分为三种层次分明的结构:
 |    http block        the protocol level
 |    server block        the server level
 V    location block        the requested URI

Nginx 允许用户定义 Location block ,并指定一个匹配模式(pattern)匹配特定的 URI。除了简单的字符串(比如文件系统路径),还允许使用更为复杂的匹配模式(pattern)。
Location block 的基本语法形式是:
    location [=|~|~*|^~|@] pattern { ... }
[=|~|~*|^~|@] 被称作 location modifier ,这会定义 Nginx 如何去匹配其后的 pattern ,以及该 pattern 的最基本的属性(简单字符串或正则表达式)。

------- 关于 location modifier -------
1. =
这会完全匹配指定的 pattern ,且这里的 pattern 被限制成简单的字符串,也就是说这里不能使用正则表达式。
Example:
server {
    server_name website.com;
    location = /abcd {
    […]
    }
}
匹配情况:
    http://website.com/abcd        # 正好完全匹配
    http://website.com/ABCD        # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
    http://website.com/abcd?param1&param2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1&param2
    http://website.com/abcd/    # 不匹配,因为末尾存在反斜杠(trailing slash),Nginx 不认为这种情况是完全匹配
    http://website.com/abcde    # 不匹配,因为不是完全匹配

2. (None)
可以不写 location modifier ,Nginx 仍然能去匹配 pattern 。这种情况下,匹配那些以指定的 patern 开头的 URI,注意这里的 URI 只能是普通字符串,不能使用正则表达式。
Example:
server {
    server_name website.com;
    location /abcd {
    […]
    }
}
匹配情况:
    http://website.com/abcd        # 正好完全匹配
    http://website.com/ABCD        # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
    http://website.com/abcd?param1&param2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1&param2
    http://website.com/abcd/    # 末尾存在反斜杠(trailing slash)也属于匹配范围内
    http://website.com/abcde    # 仍然匹配,因为 URI 是以 pattern 开头的

3. ~
这个 location modifier 对大小写敏感,且 pattern 须是正则表达式
Example:
server {
    server_name website.com;
    location ~ ^/abcd$ {
    […]
    }
}
匹配情况:
    http://website.com/abcd        # 完全匹配
    http://website.com/ABCD        # 不匹配,~ 对大小写是敏感的
    http://website.com/abcd?param1&param2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1&param2
    http://website.com/abcd/    # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
    http://website.com/abcde    # 不匹配正则表达式 ^/abcd$
注意:对于一些对大小写不敏感的系统,比如 Windows ,~ 和 ~* 都是不起作用的,这主要是操作系统的原因。

4. ~*
与 ~ 类似,但这个 location modifier 不区分大小写,pattern 须是正则表达式
Example:
server {
    server_name website.com;
    location ~* ^/abcd$ {
    […]
    }
}
匹配情况:
    http://website.com/abcd        # 完全匹配
    http://website.com/ABCD        # 匹配,这就是它不区分大小写的特性
    http://website.com/abcd?param1&param2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1&param2
    http://website.com/abcd/    # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
    http://website.com/abcde    # 不匹配正则表达式 ^/abcd$

5. ^~
匹配情况类似 2. (None) 的情况,以指定匹配模式开头的 URI 被匹配,不同的是,一旦匹配成功,那么 Nginx 就停止去寻找其他的 Location 块进行匹配了(与 Location 匹配顺序有关)

6. @
用于定义一个 Location 块,且该块不能被外部 Client 所访问,只能被 Nginx 内部配置指令所访问,比如 try_files or error_page

------- 搜索顺序以及生效优先级 -------
因为可以定义多个 Location 块,每个 Location 块可以有各自的 pattern 。因此就需要明白(不管是 Nginx 还是你),当 Nginx 收到一个请求时,它是如何去匹配 URI 并找到合适的 Location 的。
要注意的是,写在配置文件中每个 Server 块中的 Location 块的次序是不重要的,Nginx 会按 location modifier 的优先级来依次用 URI 去匹配 pattern ,顺序如下:
    1. =
    2. (None)    如果 pattern 完全匹配 URI(不是只匹配 URI 的头部)
    3. ^~
    4. ~ 或 ~*
    5. (None)    pattern 匹配 URI 的头部


----------------------------------------------------------------------------------
注:虽说是总结,其实基本上就是 Nginx HTTP Server 该部分的翻译……,原文见该书电子版的第四章p133-138

目录
相关文章
|
22天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
24 0
|
1月前
|
应用服务中间件 nginx
Nginx中如何配置中文域名?
Nginx中如何配置中文域名?
40 0
|
21天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
85 0
|
1月前
|
PHP
百度虚拟机 bcloud_nginx_user.conf配置
百度虚拟机 bcloud_nginx_user.conf配置
22 0
|
1天前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
8 0
|
4天前
|
域名解析 缓存 负载均衡
Nginx正向代理域名的配置
Nginx正向代理域名的配置
|
4天前
|
前端开发 JavaScript 应用服务中间件
修改Jeecg-boot context-path(附加图片+Nginx配置)
修改Jeecg-boot context-path(附加图片+Nginx配置)
12 0
|
15天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
22 0
|
16天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
26 0
|
25天前
|
应用服务中间件 nginx
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
9 0