Laravel 9 + Nginx环境下访问存储子文件夹文件出现404 NotFound求助
解决Laravel 9 + Nginx下Storage子目录文件404问题
1. 修正Nginx配置
Nginx默认规则可能未正确匹配Storage子目录的请求,需在站点配置的server块中添加或修改以下规则:
location ^~ /storage { alias /path/to/your-project/storage/app/public; # 替换为你的项目实际路径 try_files $uri $uri/ =404; expires 30d; add_header Cache-Control "public, immutable"; }
修改后重启Nginx生效:
sudo systemctl restart nginx
2. 检查软链接与目录权限
- 确认
public/storage软链接已正确创建,执行ls -la public/查看是否存在指向../storage/app/public的storage链接 - 确保Storage目录及子目录的所有者与Nginx运行用户一致(通常为
www-data):
sudo chown -R www-data:www-data /path/to/your-project/storage/app/public sudo chmod -R 755 /path/to/your-project/storage/app/public
3. 验证上传文件的实际存储路径
通过终端检查文件是否确实存在于对应子目录:
ls -la /path/to/your-project/storage/app/public/your-subdirectory/
确认文件名、子目录名的大小写与请求路径一致(Linux系统路径区分大小写)。
4. 清除Laravel缓存
缓存可能导致路径解析异常,执行以下命令清除缓存:
php artisan cache:clear php artisan config:clear
5. 检查React应用的请求路径
确保React中拼接的文件URL正确,例如:
// 正确示例:指向Storage子目录的文件 const fileUrl = `${process.env.REACT_APP_BASE_URL}/storage/subdirectory/your-file.jpg`;
避免路径拼写错误、遗漏子目录名或大小写不匹配。
内容的提问来源于stack exchange,提问作者aksal
相关产品推荐
相关产品推荐

