您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
nginx-Location之正则匹配
发布时间:2017-11-23 12:16:55编辑:雪饮阅读()
波浪号指定使用正则匹配
再来看,正则也来参与.
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
location ~ image {
root /var/www/;
index index.html;
}
如果我们访问 http://xx.com/image/logo.png
此时, “/” 与”/image/logo.png” 匹配
同时,”image”正则 与”image/logo.png”也能匹配,谁发挥作用?
正则表达式的成果将会使用.
图片真正会访问 /var/www/image/logo.png
再来看看两个普通模式的对比
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
location /foo {
root /var/www/html;
index index.html;
}
我们访问 http://xxx.com/foo
对于uri “/foo”, 两个location的patt,都能匹配他们
即 ‘/’能从左前缀匹配 ‘/foo’, ‘/foo’也能左前缀匹配’/foo’,
此时, 真正访问 /var/www/html/index.html
原因:’/foo’匹配的更长,因此使用之.;
那么如下案例也是同理,image会被访问到
location / {
root z.com;
index index.html index.htm;
}
location /image {
root /usr/local/nginx/z.com2;
index index.htm index.html;
}
访问http://192.168.101.108/image/xy.jpg
将会访问到root /usr/local/nginx/z.com2/image/xy.jpg
关键字词:nginx,location,正则