java项目使用宝塔面板部署服务器nginx不能反向代理找到图片资源

1.查看文件是否上传成功,宝塔面板也很方便查看文件地址

2.检查拦截器是否放行图片下载接口,以防没有token权限

3.图片上传是否使用云服务器地址

复制代码
//@PostMapping("/upload")
//public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
//    //找到文件的位置
//    String filePath = System.getProperty("user.dir") + "/files/";//获取当前项目的根路径
//    if(!FileUtil.isDirectory(filePath)){
//        FileUtil.mkdir(filePath);
//    }
//    byte[] bytes = file.getBytes();
//    String filename = System.currentTimeMillis() + "_" +file.getOriginalFilename(); //文件的原始名称
//    //写入文件
//    FileUtil.writeBytes(bytes,filePath +filename);
//
//    // 修改这里:使用服务器IP或域名替换localhost
//    String url = "http://云服务器地址/files/download/" + filename;
//    // 或者使用相对路径(推荐)
//    // String url = "/files/download/" + filename;
//
//    return Result.success(url);

4.确定网站nginx配置是否写入

location ^~ /files/download/ {

proxy_pass http://127.0.0.1:9999/files/download/;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-Proto $scheme;

跨域配置

add_header 'Access-Control-Allow-Origin' 'http://49.234.157.76' always;

add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always;

if ($request_method = 'OPTIONS') {

return 204;

}

}

注意:Nginx location 优先级冲突:正则规则 location ~ .*\.(gif|jpg|jpeg|png...) 覆盖了 /files/download/ 路径

如果还是没有成功,可以使用先使用命令行直接访问后端接口,如果没问题就着重排查nginx配置

相关推荐
Magic--9 分钟前
深入解析管道:最基础的进程间通信(IPC)实现
java·服务器·unix
架构师沉默38 分钟前
为什么国外程序员都写独立博客,而国内都在公众号?
java·后端·架构
带刺的坐椅44 分钟前
SolonCode v2026.4.1 发布(比 ClaudeCode 简约的编程智能体)
java·ai·llm·agent·solon-ai·claudecode·soloncode
殷紫川44 分钟前
从单体到亿级流量:登录功能全场景设计指南,踩过的坑全给你填平了
java
Filwaod44 分钟前
Cursor+IDEA开发问题
java·idea·cursor
爱丽_1 小时前
Spring 事务:传播行为、失效场景、回滚规则与最佳实践
java·后端·spring
陳10301 小时前
Linux:基础开发工具
linux·运维·服务器
timi先生1 小时前
语料库全栈项目部署 (Vue + Java + CQPweb)
java·前端·vue.js
我爱学习好爱好爱1 小时前
Ansible 常用模块详解:cron、archive、unarchive实战
linux·服务器·ansible
sunwenjian8862 小时前
Java进阶--IO流
java·开发语言