尝试设置 nginx 来提供我的 Web 应用程序的静态资产,我想到了这一点,它部分有效:
# error!!!!
# always 404: not found
location ~ ^/my_uploads/(images|pdfs|videos)/ {
# "/opt/my_app123/assets/my_real/uploads_folder/" is the physical path to my uploaded assets
root /opt/my_app123/assets/my_real/uploads_folder;
自动索引关闭;}
# below everything working good
location ~ ^/(images|css|js|fonts|videos)/ {
root /opt/my_app123/assets/static;
autoindex off;
}
location ~ ^/(favicon.ico|robots.txt) {
alias /opt/my_app123/assets/static/$1;
}
location = / {
proxy_set_header Host $host;
[..........]
}
所有权限都是 777。SELinux 甚至没有安装。但我尝试访问的所有内容都一直得到 404my_uploads/
如何修复?
例如
my_domain.com/robots.txt # ok
my_domain.com/images/image1.png # ok
my_domain.com/videos/video1.mp4 # ok
my_domain.com/my_uploads/videos/video2.mp4 # not found
my_domain.com/my_uploads/pdfs/my_doc1.pdf # not found
答案1
从您的评论中:
上传的物理路径 --> /opt/my_app123/assets/my_uploads/{images, pdfs, videos, etc}/{file_name}
对于 URI ,如果物理文件/my_uploads/videos/video2.mp4
位于/opt/my_app123/assets/my_uploads/videos/video2.mp4
文档根目录是/opt/my_app123/assets
。
物理路径是通过连接root
和 URI 的值来计算的。
例如:
location ~ ^/my_uploads/(images|pdfs|videos)/ {
root /opt/my_app123/assets;
autoindex off;
}
看这个文件了解详情。