Location 语法
location 有”定位”的意思, 根据Uri来进行不同的定位。
在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上。
比如, 碰到.php, 如何调用PHP解释器? --这时就需要location
|
Location精准匹配
如果访问http://xxx.com/ ,定位流程就是
1、精准匹配中 “/”,得到index页为index.htm
2、再次访问/index.htm,此次内部转跳uri已经是“/index.htm",根目录为/usr/local/nginx/html/
3、最终结果访问了/usr/local/nginx/html/index.htm
#首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配.当有匹配成功时候,停止匹配,按当前匹配规则处理请求
[root@tiejiang nginx]# cat /var/www/html/index.htm #先找一个测试页面放在/var/www/html目录下
<html>
wecome to z.com:8080 admin panel
</html>
如果访问http://xxx.com/ #定位流程就是
1、精准匹配中 “/”,得到index页为index.htm
2、再次访问/index.htm,此次内部转跳uri已经是“/index.htm",根目录为/usr/local/nginx/html/
3、最终结果访问了/usr/local/nginx/html/index.htm
[root@tiejiang nginx]# vim conf/nginx.conf
文件存在引导就能完成
location =/ { #用#来做精准匹配,root /var/www/html/; #为了和下面的做区别,这里指向/var/www/html/目录
index index.htm index.html;
}location / {
root /usr/local/nginx/html/;
index index.html index.htm;
}
[root@tiejiang nginx]# ./sbin/nginx -s reload #重新加载一下配置文件
[root@tiejiang nginx]# vim conf/nginx.conf #这次精准匹配到文件index.htm,
/var/www/html/index.htmlocation = /index.htm {
root /var/www/html/;
index index.htm index.html;
}
location / {
root html; #相对路径
index index.html index.htm;
}