Nginx root和alias的区别

前言

这是我的第一篇技术博客,希望可以记录自己的学习过程,还有分享经验。

 

问题:使用了nginx作为静态文件服务器,写入了如下配置:

server{
  listen 80; #监听的端口
  server_name  ********;   #监听的ip和端口

  location / {
    root /usr/share/nginx/html;
    index index.html;
  }

  # 静态文件服务器
  location /downloads {
    root /opt/product/wechat/downloads;
    autoindex on;
  }
}

放入/opt/product/wechat/downloads/1.jpeg后,发现访问http://********/downloads/1.jpeg,服务器返回404错误。

 

查看nginx官方文档(https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/),原来这是由于root和alias的分别所致:

root:

The root directive specifies the root directory that will be used to search for a file. To obtain the path of a requested file, NGINX appends the request URI to the path specified by the root directive.

root指令指明了查找文件的根目录。Nginx拼接root的根目录和请求URI,来得到文件的路径。

alias:

Alias就是将uri部分替代为alias的目录名。Alias指令的路径,最后必须添加/

 

最终配置:

server{
  listen 80; #监听的端口
  server_name  ********;   #监听的ip和端口

  location / {
    root /usr/share/nginx/html;
    index index.html;
  }

  # 静态文件服务器
  location /downloads {
    alias /opt/product/wechat/downloads/;
    autoindex on;
  }
}

 

终于可以成功访问!

转载于:https://www.cnblogs.com/JansenGao/p/9028824.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值