nginx使用记录——root和alias区别

nginx使用记录——root和alias区别

用法区别

假设现有两个前端项目,office和company,其中

  • office用/office前缀访问,实际路径为http://{ip}:{port}/html/web/office

  • company用/company前缀访问,实际路径为 http://{ip}:{port}/html/web/company

下面分别使用root和alias来配置/office 和 /company

  • 用root配置

    root的处理结果是: 设置请求的根目录,root路径+location路径

    location /office  {
        root html/web;
        #alias html/web/office/:
        index index.html;
    }
    location /company {
        root html/web;
        #alias html/web/company/;
        index index.html;
    }
    

    浏览器地址栏输入

    输入 http://{ip}:{port}/company 
    实际链接到 http://{ip}:{port}/html/web/company
    
    输入 http://{ip}:{port}/office
    实际链接到 http://{ip}:{port}/html/web/office
    
  • 用alias配置

    alias的处理结果是:使用alias路径替换location路径,用来更改location的URI

    location /office {
        alias html/web/office/;
        index index.html;
    }
    location /company {
        alias html/web/company/;
        index index.html;
    }
    

    浏览器地址栏输入

     输入 http://{ip}:{port}/office
     实际链接到 http://{ip}:{port}/html/web/office/index.html
    
     输入 http://{ip}:{port}/company 
     实际链接到 http://{ip}:{port}/html/web/company/index.html
    

    换成以下链接也可访问

    http://{ip}:{port}/office/index.html 
    http://{ip}:{port}/company/index.html 
    
  • 两者区别

    1. alias是一个目录别名的定义,root则是最上层目录的含义。

    2. 如果location路径是以/结尾,则alias也必须是以/结尾,root没有要求

    3. alias 只能配置在location 中, 而root 可以配置在 server, http 和 location 中

使用场景

  • 如果位置与别名路径的末尾匹配,最好使用root。
  • 如果从与 root 指定的目录不同的位置读取数据时,最好使用alias。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值