nginx访问不同服务器docker部署的java服务上传文件地址

B服务器上nginx配置

location /fz/ {

proxy_pass http://ipA:8036/;

proxy_set_header host $host;

proxy_set_header x-real-ip $remote_addr;

proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;

}

#把/file/file/ 前缀改写为 /upload/,其余路径原样转发

location /file/file/ {

proxy_pass http://ipA:8036/upload/file/;

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;

}

请求地址图片

http://ipB:8030/file/file/upload/TestImg/20261203/17D4408E-1227-4A93-B2BB-3AF7C8767167.jpg

实际转向地址

http://ipA:8036/upload/file/upload/TestImg/20261203/17D4408E-1227-4A93-B2BB-3AF7C8767167.jpg

http://ipA:8036/upload/file/upload/TestImg/20261203/17D4408E-1227-4A93-B2BB-3AF7C8767167.jpg

upload = currentDir (/app) = 真实位置/home/sunplus

  • 请求:/file/file/upload/TestImg/20261203/xxx.jpg

  • 切掉 /file/file/ → 剩余 upload/TestImg/20261203/xxx.jpg

  • 拼到 /upload/file/ → /upload/file/upload/TestImg/20261203/xxx.jpg

java代码

复制代码
public class MyWebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/file/**").addResourceLocations("file:D:/");
        String currentDir = System.getProperty("user.dir");
        currentDir = currentDir.replaceAll("\\\\", "/")+"/";
        System.out.println(currentDir);
        log.info("=========================currentDir:"+currentDir);
        registry.addResourceHandler( "/upload/**", "/uploads/**")
                .addResourceLocations("file:"+currentDir);
    }
}

currentDir=/app

挂载配置为 -v /home/sunplus:/app \

文件上传地址为/app空间内 /file/upload/TestImg/

实际宿主机位置为/home/sunplus/file/upload/TestImg/

复制代码
registry.addResourceHandler("/upload/**", "/uploads/**")

这意味着 Spring MVC 只会拦截以 /upload/ 或 /uploads/ 开头的请求,并去你配置的目录(即 System.getProperty("user.dir")) /app==>/home/sunplus/ 下查找文件。

相关推荐
Z21-X26 分钟前
云原生运维学习笔记:Kubernetes 动态卷供应与 StatefulSet 实战
运维·笔记·学习·云原生·kubernetes
dog2501 小时前
Linux 7.4 最新的 TCP_ROCCET 拥塞控制解析
linux·运维·tcp/ip
zjeweler1 小时前
Nginx 502 Bad Gateway怎么解决?Docker反代与PHP-FPM报错全链路排查
nginx·docker·php-fpm·502 bad gateway·实用建站
火云代理10 小时前
动态IP是什么及其应用场景
服务器·网络·tcp/ip·动态代理ip·代理ip·火云代理·静态代理ip
.道阻且长.10 小时前
Linux基础IO:系统文件IO
linux·运维·服务器
迪康coolmu10 小时前
基于迪康端点安全一体化管理系统的屏幕录像审计:录屏的留存、回放与取证链
运维·服务器·网络·经验分享·安全·网络安全·制造
xurileidian10 小时前
第 24 篇 · 边玩边学Python · while 与死循环怎么退
服务器·前端·python
GZlangtian11 小时前
锂电池化成设备自动化产线集成设计:物流节拍、设备通信与MES接口
运维·自动化·制造·化成分容设备·电池化成分容设备·锂电检测设备·锂电池化成分容
zxcvb15312 小时前
运维实践里把备份mysql数据库做稳的一些思路
运维·数据库·mysql