root和alias的区别

文章讲述了Nginx配置中root和alias的作用,以及如何在测试环境中切换两者,清理缓存后验证效果。通过实际操作演示了如何在location块中使用root和alias,并涉及了配置文件的修改和nginx服务的重启。
摘要由CSDN通过智能技术生成
结论:

当配置文件中是root时,nginx会在root的路径后添加location的路径作为需要查找的网页文件的路径即/data/nginx/test

当配置文件中是alias时,nginx会直接去alias的路径下查找网页文件即/data/nginx

测试:

要访问的网页文件:

[root@www nginx]# pwd

/data/nginx

[root@www nginx]# cat index.html

this is alias for test

[root@www test]# pwd

/data/nginx/test

[root@www test]# cat index.html

this is root for test

配置文件:

[root@www conf.d]# vim test.conf

server {

    listen       80;

    server_name  www.hyc.org;

    #access_log  logs/host.access.log  main;

    location /test {

        root   /data/nginx;

        index  index.html index.htm;

    }

}

清理缓存后用浏览器访问www.hyc.org/test:

将以上配置文件test.conf中的root修改为alias:

[root@www conf.d]# vim test.conf

server {

    listen       80;

    server_name  www.hyc.org;

    #access_log  logs/host.access.log  main;

    location /test {

        alias   /data/nginx;

        index  index.html index.htm;

    }

}

重新加载nginx配置文件:

[root@www conf.d]# systemctl reload nginx

清理浏览器缓存后再次测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值