Nginx 笔记与总结(6)Location:精准匹配
在 /usr/local/nginx/conf/nginx.conf 的 server 段中,location 表示根据 URI 来进行不同的定位:把网站的不同部分定位到不同的处理方式上,例如遇到 .php 文件如何调用 PHP 解释器。
location 语法:
location [=|~|~*|^~] /uri/ { … }
location 类型分为:
location = patt {}[精准匹配]
location patt {}[一般匹配]
location = ~patt {}[正则匹配]
精准匹配
匹配时首先看有没有精准匹配,如果有,则停止匹配过程:
location = patt {
config A
}
如果 $uri == patt ,匹配成功,使用 config A。
【例 step 1】
注释掉 /usr/local/nginx/conf/nginx.conf 中之前配置的 server 信息,在默认的 server 段中进行编辑,此时访问 192.168.254.100,显示

此时 server 段中 location 的配置为:
location / {
root html;
index index.html index.htm;
}
此时,访问 192.168.254.100 实际访问的目录是:/usr/local/nginx/html/index.html
注意:root 中的 html 的绝对路径是 /usr/local/nginx/html,该目录下只有一个 index.html 文件
【例 step 2】
此时在一般匹配 location / 的上文中添加一段精准匹配(优先匹配精准匹配,一旦匹配则立即停止匹配过程),此时的 location 信息为:
location = / {
root /var/www;
index index.html index.htm;
}
location / {
root html;
index index.html index.htm;
}
为了以示区别,精准匹配的目录设置为 /var/www,该目录下只有一个文件 index.htm:i'm /var/www/index.htm
平滑重启 nginx
访问 http://192.168.254.100/,仍然显示

原因是:当输入 192.168.254.100 这个 ip 时,实际上是访问一个文件,通过精准匹配找到该文件是 index.html,也就是说访问 192.168.254.100 即访问 192.168.254.100/index.html,此时只能通过一般匹配访问 html(绝对路径为 /usr/local/nginx/html)下的 index.html ,这就解释了为什么会出现 Welcome to nginx 的页面。
【例 step 3】
但是一旦把精准匹配的 index.html 和 index.htm 调换位置:
location = / {
root /var/www;
index index.htm index.html;
}
location / {
root html;
index index.html index.htm;
}
访问 http://192.168.254.100/,则会出现 404 Not Found:

查看 /usr/local/nginx/logs/error.log:

提示 /usr/local/nginx/html/index.htm 找不到
【例 step 4】
修改精准匹配的 location,此时的 location 信息为:
location = /index.htm {
root /var/www/;
index index.htm index.html;
}
location / {
root html;
index index.html index.htm;
}
平滑重启 nginx,访问 http://192.168.254.100/index.htm,显示 i'm /var/www/index.htm

说明发生了精准匹配,匹配到了文件 /var/www/index.htm
【例 step 5】
修改普通匹配的 location,此时的 location 信息为:
location = /index.htm {
root /var/www/;
index index.htm index.html;
}
location /index.htm {
root html;
index index.html index.htm;
}
平滑重启 nginx,访问 http://192.168.254.100/index.htm,显示 i'm /var/www/index.htm:

说明精确匹配优先于普通匹配。
【例 step 6】
此时直接访问 http://192.168.254.100/,即既不精确匹配,也不普通匹配,访问的实际页面是 /usr/local/nginx/html/index.html:

【例 step 7】
添加一条 location 配置信息(第 2 条),此时的 location 配置信息为:
location = /index.htm {
root /var/www/;
index index.htm index.html;
}
location = / {
root /var/www/;
index index.htm index.html;
}
location /index.htm {
root html;
index index.htm index.html;
}
此时访问 http://192.168.254.100/,则会命中第 2 条精准匹配,显示:i'm /var/www/index.htm

参考:
《Linux nginx 配置 location 语法 正则表达式》
Nginx 笔记与总结(6)Location:精准匹配的更多相关文章
- Nginx location规则匹配
^~ 标识符匹配后面跟-一个字符串.匹配字符串后将停止对后续的正则表达式进行匹配,如location ^~ /images/ , 在匹配了/images/这个字符串后就停止对后续的正则匹配 = 精 ...
- 06 nginx Location详解之精准匹配
一:Location详解之精准匹配 location 语法 location 有”定位”的意思, 根据Uri来进行不同的定位. 在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分, ...
- Nginx 笔记与总结(7)Location:正则匹配
在 /usr/local/nginx/conf/nginx.conf 的默认 server 段中,保留默认的 location 信息(之前测试的 location 配置删除): location / ...
- Nginx 笔记与总结(8)Location:归纳总结
首先对 URI 进行精准匹配,如果匹配上则退出匹配,返回精准匹配结果: 如果没有匹配上则寻找普通匹配,如果命中多个普通匹配,则记忆最长的匹配结果(不考虑 location 的顺序): 如果后面还有正则 ...
- nginx之location的匹配规则
nginx之location的匹配规则 一.语法规则 location [=|~|~*|^~] /uri/ { - } 符号 含义 = 开头表示精确匹配 ^~ 开头表示 uri 以某个常规字符串开头 ...
- 前端开发掌握nginx常用功能之server&location匹配规则
nginx主要是公司运维同学必须掌握的知识,涉及到反向代理.负载均衡等服务器配置.前端开发尤其是纯前端开发来说对nginx接触的并不多,但是在一些情况下,nginx还是需要前端自己来搞:例如我们公司的 ...
- nginx多虚拟主机优先级location匹配规则及tryfiles的使用
nginx多虚拟主机优先级location匹配规则及tryfiles的使用 .相同server_name多个虚拟主机优先级访问 .location匹配优先级 .try_files使用 .nginx的a ...
- nginx location 正则匹配
nginx 统计语句1.根据访问IP统计UV awk '{print $1}' access.log|sort | uniq -c |wc -l2.统计访问URL统计PV awk '{print $7 ...
- Nginx 关于 location 的匹配规则详解
有些童鞋的误区 1. location 的匹配顺序是“先匹配正则,再匹配普通”. 矫正: location 的匹配顺序其实是“先匹配普通,再匹配正则”.我这么说,大家一定会反驳我,因为按“先匹配普通, ...
随机推荐
- 用于主题检测的临时日志(ba86b8a0-7ed7-4b0b-bf1f-ce41aa2a5780 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
这是一个未删除的临时日志.请手动删除它.(ea9f667f-3be0-45c8-ad82-3acf819d571c - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
- Mac OS X 上的安装haskell开发环境
到haskell官方下载haskell的工具包: https://downloads.haskell.org/~platform/2014.2.0.0/Haskell%20Platform%20201 ...
- 无法获得锁 /var/lib/dpkg/lock - open (11: 资源临时不可用)
转自:http://www.cnblogs.com/ManMonth/archive/2010/01/14/1648010.html 问题: 运行程序更新时出现报错: 无法获得锁 /var/lib/d ...
- FactoryBean的使用
一般情况下,Spring通过反射机制利用bean的class属性指定实现类来实例化bean .在某些情况下,实例化bean过程比较复杂,如果按照传统的方式,则需要在<bean>中提供大量的 ...
- JVM的数据类型
Java虚拟机是通过某些数据类型来执行计算的,数据类型可以分为两种:基本类型和引用类型,基本类型的变量持有原始值,而引用类型的变量持有引用值. Java语言中的所有基本类型同样也都是Java虚拟机中的 ...
- js:语言精髓笔记4----面向对象概要与运算符二义性
实例创建:obj = new contructor[(arguments)]; //如果没有参数可以忽略括号:所以注意这不是函数调用: 直接量与初始器:在之前的基本表达式中将直接量与初始器分开,这时因 ...
- 简单几何(半平面交+二分) LA 3890 Most Distant Point from the Sea
题目传送门 题意:凸多边形的小岛在海里,问岛上的点到海最远的距离. 分析:训练指南P279,二分答案,然后整个多边形往内部收缩,如果半平面交非空,那么这些点构成半平面,存在满足的点. /******* ...
- MVC Area Usage
ASP.NET MVC Area操作 新建 Area:右键 -> Add –> Area 继承 AreaRegistration,重写AreaName属性与RegisterArea方法 p ...
- 寒冰王座[HDU1248]
寒冰王座 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- BZOJ4377 : [POI2015]Kurs szybkiego czytania
因为$a$与$n$互质,所以对于$0$到$n-1$里每个$i$,$ai\bmod n$的值互不相同. 设匹配成功的起点为$i$,那么可以得到$3m$段$ai\bmod n$的值不能取的禁区,每段都是连 ...