06-Location详解之精准匹配

Location 语法

location 有”定位”的意思, 根据Uri来进行不同的定位。
在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上。
比如, 碰到.php, 如何调用PHP解释器? --这时就需要location

location [=|~|~*|^~] patt {}

location = patt {} [精准匹配]

location patt{}  [一般匹配]

location ~ patt{} [正则匹配]

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.htm
        location = /index.htm {
            root   /var/www/html/;
            index  index.htm index.html;
        }
       location / {
            root   html;   #相对路径
            index  index.html index.htm;
           }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值