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配置

相关推荐
古法老登瞎折腾7 小时前
大数据智能运维实战(十一):运维 MCP 的工程化——权限、限流、审计与错误处理
java·大数据·运维·spring ai·mcp
琥珀.7 小时前
mysql中json数组转列表查询
linux·运维·服务器
智能运维指南8 小时前
研制过程“两张皮“怎么破:研制一体化支撑平台的定位、能力边界与落地前提
java·前端·人工智能·嘉为蓝鲸
yz_aiks9 小时前
Spring AI 2.1 ChatMemory 已过时用什么替代:迁移到 Session Memory 实战(JDBC 存储与会话压缩)
java·spring boot·大模型·ai agent·spring ai
金銀銅鐵9 小时前
[Java] 用 GUI 展示 EnumMap 增删键值对时的内部变化
java·后端·ai编程
学长毕设9 小时前
基于SpringBoot的上门洗车服务系统设计与实现
java·spring boot·后端
jason成都9 小时前
RTKLIB-Java 多基线联合RTK:FIX率从0%到91.5%的三层根因修复
java·gnss·rtklib·rtklib_java
鱼宵9 小时前
Stream 终端操作一次讲透:collect、count、reduce 怎么选
java·stream·终端操作
白山编程大哥9 小时前
Java 后端如何优雅地把 List 集合返回给前端
java·前端·list
Linux运维技术栈10 小时前
CVE-2026-91403 高危漏洞修复:Rocky Linux 9 多版本 JDK 环境清理实操
java·linux·运维