标签:匹配 name config 测试命令 bbb 注意 法规 文件中 images
安装第三方模块echo-nginx-modulegit clone https://github.com/openresty/echo-nginx-module.git
./configure --add-module=/nginx安装目录/echo-nginx-module
make && make install
安装之后,可以直接在配置文件中使用 echo ‘‘ 命令
location [=|^~|~|~*] /uri/ { …. }
coding.net/u/aminglinux/p/nginx/git/blob/master/location/ruler.md
nginx location语法规则:location [=|~|~*|^~] /uri/ { … }
nginx的location匹配的变量是$uri
规则示例
location = "/12.jpg" { ... }
如:
www.aminglinux.com/12.jpg 匹配
www.aminglinux.com/abc/12.jpg 不匹配
location ^~ "/abc/" { ... }
如:
www.aminglinux.com/abc/123.html 匹配
www.aminglinux.com/a/abc/123.jpg 不匹配
location ~ "png" { ... }
如:
www.aminglinux.com/aaa/bbb/ccc/123.png 匹配
www.aminglinux.com/aaa/png/123.html 匹配
location ~* "png" { ... }
如:
www.aminglinux.com/aaa/bbb/ccc/123.PNG 匹配
www.aminglinux.com/aaa/png/123.html 匹配
location /admin/ { ... }
如:
www.aminglinux.com/admin/aaa/1.php 匹配
www.aminglinux.com/123/admin/1.php 不匹配
小常识
有些资料上介绍location支持不匹配 !~,
如: location !~ ‘png‘{ ... }
这是错误的,location不支持 !~
如果有这样的需求,可以通过if来实现,
如: if ($uri !~ ‘png‘) { ... }
注意:location优先级小于if
= 高于 ^~ 高于 ~* 等于 ~ 高于 /
coding.net/u/aminglinux/p/nginx/git/blob/master/location/priority.md
对比/和~
示例1:
server{
listen 80;
server_name www.aminglinux.com;
root /tmp/123.com;
location /abc/
{
echo "/";
}
location ~ ‘abc‘
{
echo "~";
}
}
测试命令:curl -x127.0.0.1:80 ‘www.aminglinux.com/abc/1.png‘
结果是:~
对比^~和~
示例4:
server
{
listen 80;
server_name www.aminglinux.com;
root /tmp/123.com;
location ~ ‘/abc‘
{
echo ‘~‘;
}
location ^~ ‘/abc‘
{
echo ‘^~‘;
}
}
测试命令:curl -x127.0.0.1:80 ‘www.aminglinux.com/abc/123.html
结果是:^~
对比=和^~
示例5:
server
{
listen 80;
server_name www.aminglinux.com;
root /tmp/123.com;
location ^~ ‘/abc.html‘
{
echo ‘^~‘;
}
location = ‘/abc.html‘
{
echo ‘=‘;
}
}
测试命令:curl -x127.0.0.1:80 ‘www.aminglinux.com/abc.html
结果是:=
标签:匹配 name config 测试命令 bbb 注意 法规 文件中 images
原文地址:http://blog.51cto.com/jacksoner/2315945