最近一段时间在学习 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

转载于:https://www.cnblogs.com/moqiang02/p/4061233.html

Nginx 的 Location 配置指令块相关推荐

  1. Nginx的location配置详解

    Nginx的location配置详解 匹配顺序 location 的匹配顺序其实是先匹配普通,再匹配正则 正则匹配会覆盖普通匹配(实际的规则,比这复杂) 执行顺序 普通 location的匹配规则是& ...

  2. Nginx之location配置

    location指令可以用于虚拟服务器server部分,并且意味着提供来自客户端的URI或者内部重定向访问.除少数情况外,location也可以被嵌套使用 格式如下 location [modifie ...

  3. nginx的location配置root、alias用法和区别

    root & alias区别 root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上 root的处理结果是:root ...

  4. Nginx教程-location配置

    语法规则 location [=|~|~*|^~] /uri/ { - } = 表示精确匹配,这个优先级也是最高的 ^~ 表示uri以某个常规字符串开头,理解为匹配 url路径即可.nginx不对ur ...

  5. nginx中location介绍

    http://www.cnblogs.com/lidabo/p/4169396.html   博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅  :: 管理 ::   ...

  6. nginx之location匹配规则

    Nginx之location 匹配规则详解 有些童鞋的误区 1. location 的匹配顺序是"先匹配正则,再匹配普通". 矫正: location 的匹配顺序其实是" ...

  7. nginx 的location详细理解

    关于一些对location认识的误区 1. location 的匹配顺序是"先匹配正则,再匹配普通". 矫正: location 的匹配顺序其实是"先匹配普通,再匹配正则 ...

  8. nginx的rewrite配置

    nginx的rewrite配置 文章目录 nginx的rewrite配置 指令语法 应用位置 flag参数 常用正则表达式 指令语法 rewrite regex replacement [flag]; ...

  9. Nginx之二:nginx.conf简单配置(参数详解)

    vim /usr/local/nginx/conf/nginx.conf #user nobody;#程序运行使用账户 worker_processes 1;#启动的进程,通常设置成和cpu的数量相等 ...

最新文章

  1. Sprint第二个冲刺(第八天)
  2. C/C++隐式类型转换导致的若干问题
  3. Cocos2d-x V3.2+Cocos Studio1.6 实现一个简单的uibutton点击功能
  4. 每天一点Swift(五)控制器的生命周期和SizeClass
  5. 万年历c语言大作业实验,C语言实现万年历效果
  6. (转)淘淘商城系列——分布式文件系统FastDFS
  7. 春节临近|传统彩色手绘年画素材,满满东方韵味
  8. find和grep命令合集
  9. IT运维面试问题总结-基础服务、磁盘管理、虚拟平台和系统管理
  10. 在白宫,郎朗为什么选择了《我的祖国》
  11. Nexus Indexer 2.0:增量下载
  12. 中文手写数据集训练识别
  13. matlab行星运动轨迹仿真动画,Matlab动画模拟太阳系行星运动
  14. cannot import name ‘DistanceMetric‘ from ‘sklearn.metrics‘
  15. Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
  16. 作业二 20182325袁源
  17. 服务器维护与管理专业好就业吗,windows server服务器配置与管理专业就业方向
  18. Srping 中的AOP
  19. 香帅的北大金融学课笔记20 -- 金融术与道
  20. python 峰_python-如何在具有多个峰的数据集中查找FWH...

热门文章

  1. GNN 模型在生物化学和医疗健康中的典型应用
  2. 硬核知识,C/C++移植法则分享
  3. ICCV 2019 | 视频综合理解:行为识别、场景识别以及视频综述
  4. CVPR 2019 Oral 目标跟踪最强算法SiamRPN++开源实现
  5. Python虚拟环境你会用了吗?手把手教你
  6. 收藏 | 一文带你深入理解深度学习最新进展
  7. 问答系统设计的一些思考
  8. 深度学习算法 | LSTM算法原理简介及Tutorial
  9. 深度学习(四十七)DSD正则化训练方法
  10. LogBack 打印 ES的DSL日志信息