Nginx Location规则
Nginx由内核和模块组成,其中内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端的请求映射到一个location block,而location是Nginx配置中的一个指令,用于访问的URL匹配,而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
默认Nginx.conf配置文件中至少存在一个location /,即表示客户端浏览器请求的URL为:域名+/,如果location /newindex/,则表示客户端浏览器请求的URL为:域名+/newindex/。常见Location匹配URL的方式如下:
|
= 字面精确匹配; ^~ 最大前缀匹配; / 不带任何前缀:最大前缀匹配; ~ 大小写相关的正则匹配; ~* 大小写无关的正则匹配; @ location内部重定向的变量。 |
其中Location =、^~、/属于普通字符串匹配,Location ~、~*属于正则表达式匹配,Location优先级与其在Nginx.conf配置文件中的先后顺序无关。
Location = 精确匹配会第一个被处理,如果发现精确匹配,Nginx则停止搜索其他任何Location的匹配。
普通字符匹配,正则表达式规则和完整URL规则将被优先和查询匹配,^~为最大前缀匹配,如果匹配到该规则,Nginx则停止搜索其他任何Location的匹配,否则nginx会继续处理其他location指令。
正则匹配"~"和"~*",如果找到相应的匹配,则Nginx停止搜索其他任何Location的匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。
Location规则匹配优先级总结如下:
|
(location =) > (location 完整路径) > (location ^~ 路径) > (location ~|~* 正则顺序) > (location 部分起始路径) > (location /) |
Location 案例演示
添加echo-nginx-module模块支持在配置文件中使用echo、sleep、time及exec等类似shell命令
#下载echo模块
wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
#重新预编译、编译、安装Nginx
[root@localhost nginx-1.14.0]# ./configure --add-module=../echo-nginx-module-0.61/
[root@localhost nginx-1.14.0]# make && make install
[root@localhost nginx-1.14.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
configure arguments: --add-module=../echo-nginx-module-0.61/
--------------------------------------------------------------------------------------------------
Location案例
(1)Location = 精确匹配会第一个被处理,如果发现精确匹配,Nginx则停止搜索其它任何Location的匹配。增加配置如下:
server {
省略...
location / {
echo 'test1';
}
location = / {
echo 'test2';
}
省略...
}
[root@localhost nginx]# curl 10.0.0.12
test2
增加匹配 /index.html
location = /index.html {
echo 'test3';
}
[root@localhost nginx]# curl 10.0.0.12/index.html
test3
[root@localhost nginx]# curl 10.0.0.12
test2
[root@localhost nginx]# curl 10.0.0.12/test.html
test1
默认“/”解释:“/”匹配任何请求,因为所有请求都是以“/”开始;但是更长字符匹配或者正则表达式匹配会优先匹配,“/”优先级最低。
注:在这个案例中我们访问精确匹配必须是和配置文件规定的完全匹配才会匹配到对应的内容,如果没有匹配到任何的精确匹配则会交给 / 匹配。
--------------
(2)测试目录匹配,有两种配置模式,,目录匹配或前缀匹配,任选其一或搭配使用。
= #字面精确匹配;
location = /images/ {
echo 'test4';
}
[root@localhost nginx]# curl 10.0.0.12/images
test1
[root@localhost nginx]# curl 10.0.0.12/images/
test4
^~最大前缀匹配;域名后面匹配的目录必须是location写的参数,才能匹配到location中的文件。
location ^~ /images/ {
echo 'test5';
}
[root@localhost nginx]# curl 10.0.0.12/images
test1
[root@localhost nginx]# curl 10.0.0.12/images/
test5
[root@localhost nginx]# curl 10.0.0.12/images/111
test5
注:匹配 / 后面的内容以 /images/ 开头的内容。
域名后面跟的第一个斜杠代表“/”,第二个斜杠就代表在访问的是一个目录,第三个斜杠代表可能有两层目录。
#10.0.0.12/images 代表访问的 images 这个文件
#10.0.0.12/images/ 代表访问的 images 这个目录
(3)正则文件匹配
~ :大小写相关的正则匹配;
~*:大小写无关的正则匹配;
location ~* \.(html|txt|gif|jpg|jpeg)$ {
echo 'test6';
}
location ~ \.(html|txt|gif|jpg|jpeg)$ {
echo 'test7';
}
#正则匹配中“.” 号匹配的时候默认是匹配所有单个字符,加上“\”转义符号,将“.” 号转义成真正意义上的点号。
#(html|txt|gif|jpg|jpeg)表示匹配html、txt等字符。
# $ 号代表必须以对应的字符结尾。
#可以通过调整两个的顺序来测试。
[root@localhost nginx]# curl 10.0.0.12/index.html
test6
[root@localhost nginx]# curl 10.0.0.12/index.HTML
test6
注:~大小写相关匹配,规定必须和配置文件一样(配置文件写的大小写来确定)的小写或者大写才能匹配到该请求。大小写无关匹配,不论是大写还是小写都会匹配到该请求(例如:以HTML和html结尾都会匹配到test7)
(4)基础正则匹配
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
.号:匹配任意单个字符;
*号:匹配前面的字符任意次。
#这个正则匹配的任意字符开头的以gif|jpg|jpeg|bmp|png|ico|txt|js|css这些文件结尾的后缀名,才能匹配这个规则。
Nginx Location规则的更多相关文章
- Nginx location规则匹配
^~ 标识符匹配后面跟-一个字符串.匹配字符串后将停止对后续的正则表达式进行匹配,如location ^~ /images/ , 在匹配了/images/这个字符串后就停止对后续的正则匹配 = 精 ...
- nginx location 路由的几个配置纪要
1:网上没有查到在线测试 nginx location 规则的网址 在服务器上可以通过 return 返回测试比如 把#号去掉 # location /admin\.php(.*) # { #def ...
- 详细解析 nginx uri 如何匹配 location 规则
location 是 nginx 配置中出现最频繁的配置项,一个 uri 是如何与多个 location 进行匹配的? 在有多个 location 都匹配的情况下,如何决定使用哪一个 location ...
- nginx 反向代理 配置 https 实现http https同时存在 经测试 支持location 规则
server { listen ssl; #监听443端口 server_name www.app01.com; ssl on; #启用ssl加密 ssl_certificate /etc/cert/ ...
- nginx里面的location 规则匹配
nginx location语法 ~ # 区分大小写的正则匹配 location ~ \.(gif|jpg|png|js|css)$ { #规则D } ~* # 不区分大小写的正则匹配(和~的功能相同 ...
- 附001.Nginx location语法规则
一 location规则 1.1 location语法 基本语法: location [=|~|~*|^~]/uri/{...} 修饰符释义: 1 = #表示精确严格匹配,只有请求的url路径与后面的 ...
- Nginx location 匹配顺序整理
Nginx location模块整理 具体的Nginx安装就不在这里描述了,这里只是为了对location的描述 Nginx环境 a. 查看当前系统cat /etc/redhat-release [r ...
- Nginx Location配置总结
Nginx Location配置总结 语法规则: location [=|~|~*|^~] /uri/ { - }= 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即 ...
- nginx location语法使用说明
语法规则: location [=|~|~*|^~] /uri/ { … } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可.nginx不对url做编码,因 ...
随机推荐
- IntentService和HandlerThread
上一篇说了说android 系统的UI更新机制.核心点围绕在Looper的使用上.主线程运行起来后,初始化并运行一个静态Looper,H类(handler子类)处理各种事件. 16ms的UI upda ...
- python出现AttributeError: module ‘xxx’ has no attribute ‘xxx’错误时,两个解决办法
运行python程序时,也许会出现这样的错误:AttributeError: module ‘xxx’ has no attribute ‘xxx’: 解决该错误有两种方法 1.手动安装该模块 2.检 ...
- jsp死循环
查看多重循环的 i 或者 j 是否写错
- 使用hash拆分文件
package readImgUrl; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.i ...
- 代码实现:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。 已抽签决定比赛名单。有人向队员打听比赛的名单。 a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单
/*两个乒乓球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人. 已抽签决定比赛名单.有人向队员打听比赛的名单. a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单.*/ ...
- ControlTemplate in WPF —— Window
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- oracle 四舍五入 取得的数值
SELECT ROUND( number, [ decimal_places ] ) FROM DUAL 说明: number : 将要处理的数值 decimal_places : 四舍五入,小数取几 ...
- The window object
At the core of the BOM is the window object, which represents an instance of the browser. The window ...
- java:LeakFilling(JSP,servlet,Ajax,Json,Path)
1. request.setAttribute("test", "测试"); request.getRequestDispatcher(" ...
- cocos2dx[3.2](3) 浅析CCDeprecated.h
CCDeprecated.h中存放的都是2.x将要被抛弃的命名规范,主要包含类名.枚举类型. 虽然说2.x的这些命名规范被CCDeprecated.h保留了.但是为了彻底学习3.x的新特性,就必须要尽 ...