65人参与 • 2025-03-05 • Redis
在使用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配置
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
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 内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论