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这些文件结尾的后缀名,才能匹配这个规则。