在使用nginx做静态资源服务器时,配置完成后通过浏览器访问一直报404 not found错误,本人nginx配置信息如下:
location /images/ { root /mnt/upload/files; }
所有文件存放在/mnt/upload/files
分析:
发现是配置的问题,配置静态路径有两种方式,之前配置的是直接在url里写根目录,而现在配置是一个有前缀的url,所以报404 not found错误了。
root配置会在配置的目录后跟上url,组成对应的文件路径,即想访问的地址是:
https://jb51.net/images/a.png
nginx根据配置走的文件路径是
/mnt/upload/files/images/a.png
而我需要的是
/mnt/upload/files/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.
当访问/i/top.gif时,root是去/data/w3/i/top.gif请求文件,alias是去/data/w3/images/top.gif请求,也就是说
root响应的路径:配置的路径+完整访问路径(完整的location配置路径+静态文件)
alias响应的路径:配置路径+静态文件(去除location中配置的路径)
解决办法:
location /images/ { alias /mnt/upload/files/; }
注意:使用alias时目录名后面一定要加“/”;一般情况下,在location/中配置root,在location /*中配置alias。
到此这篇关于nginx配置静态资源文件404 not found问题解决方法的文章就介绍到这了,更多相关nginx 静态资源文件404 内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论