zvv

nginx配置静态文件目录404

今天使用nginx配置静态文件代理时,访问一直是404,罪魁祸首就是我没搞清楚root与alias的区别。

错误

一开始配置:

1
2
3
location /static/ {
root /home/static;
}

 

当我访问/static/a.css这样实际上要到/home/static/static/下找文件,而我的目的是让/static/去访问/home/static/下的文件,这样就造成了404

解决

1
2
3
location /static/ {
root /home;
}

或者

1
2
3
location /static/ {
alias /home/static/;
}

 

alias与root区别

The /data/w3/i/top.gif file will be sent in response to the “/i/top.gif” request

on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.

当访问/i/top.gif时,root是去/data/w3/i/top.gif请求文件,alias是去/data/w3/images/top.gif请求,也就是说
root响应的路径:配置的路径+完整访问路径(完整的location配置路径+静态文件)
alias响应的路径:配置路径+静态文件(去除location中配置的路径)

注意

  1. 使用alias时目录名后面一定要加“/”
  2. 一般情况下,在location /中配置root,在location /other中配置alias

via.https://zhangguodong.me/2017/01/22/nginx%E9%85%8D%E7%BD%AE%E9%9D%99%E6%80%81%E6%96%87%E4%BB%B6%E7%9B%AE%E5%BD%95404/

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »

因本文不是用Markdown格式的编辑器书写的,转换的页面可能不符合AMP标准。