在使用nginx做静态资源服务器的时候,配置完成后通过浏览器访问一直报404 Not Found错误
nginx配置信息如下
location /r/ {
root /home/data/file/;
}
所有文件放在 /home/data/file/
下面
发现是配置的问题,之前配置直接是在URL中写根目录,而现在是有一直前缀/r/
,所以报404错误,root会在配置的目录后跟上URL,组成对应的文件路径,即访问地址为
https://amoqi.cn/r/a.png
nginx走的文件路径为:
/home/data/file/r/a.png
而我们需要走的是/home/data/file/a.png
现在Nginx提供弄个了另外一种静态路径配置:alias
配置
官方root配置
Sets the root directory for requests. For example, with the following configuration
location /i/ {
root /data/w3;
}
The /data/w3/i/top.gif file will be sent in response to the “/i/top.gif” request
官方alias配置
Defines a replacement for the specified location. For example, with the following configuration
location /i/ {
alias /data/w3/images/;
}
on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.
root响应的路径:配置的路径+完整访问路径(完整的location配置路径+静态文件)
alias响应的路径:配置路径+静态文件(去除location中配置的路径)
解决办法
location /r/ {
alias /home/data/file/;
}
注意:使用alias时目录名后面一定要加“/”
;一般情况下,location /
中配置root
在location /*
中配置alias