You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot反向代理后DNS附加应用端口的原因及解决方法

问题原因
  1. Spring Boot未感知代理环境
    Spring Boot默认用自身监听的8080端口生成重定向URL,它不知道前端有Nginx代理,无法获取外部访问的实际端口(80),所以会把8080附加到域名后。

  2. Nginx配置存在疏漏

  • proxy_set_header HOST $http_host会传递客户端的Host头,虽然这里客户端请求的是80端口(默认可不显式带),但Spring Boot仍可能优先用自身端口构建URL;
  • 你设置了proxy_redirect off,关闭了Nginx自动修正后端返回的重定向URL的功能,导致带8080的URL直接返回给客户端。
解决办法

一、修正Nginx代理配置

修改Nginx的server块,传递正确的代理头并开启重定向修正:

server {
    listen       80;
    server_name  example.com;

    location / {
         proxy_set_header X-Real-IP $remote_addr;
         # 传递纯域名的Host,避免带端口
         proxy_set_header HOST $host;
         proxy_set_header X-NginX-Proxy true;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         # 告诉Spring Boot外部访问的协议和端口
         proxy_set_header X-Forwarded-Proto $scheme;
         proxy_set_header X-Forwarded-Port $server_port;
 
         proxy_pass http://[app-ip]:8080;
         # 自动修正后端返回的重定向URL,替换掉8080端口
         proxy_redirect http://[app-ip]:8080/ http://example.com/;
   }
}

二、让Spring Boot识别代理头

配置Spring Boot读取Nginx传递的代理头,生成正确的外部访问URL:

方法1:配置文件(最简单)

在application.properties中添加:

# Spring Boot 2.2及以上版本用这个
server.forward-headers-strategy=framework
# 如果你用的是旧版本,替换成下面这行
# server.use-forward-headers=true

方法2:添加过滤器(自定义场景)

如果需要更灵活的配置,可以在你的WebConfiguration类中添加ForwardedHeaderFilter:

package kcnd.campaign.configuration.web;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.filter.ForwardedHeaderFilter;
import org.springframework.context.annotation.Bean;

@Configuration
@EnableWebMvc
public class WebConfiguration implements WebMvcConfigurer {

    // 注册过滤器处理代理头
    @Bean
    public ForwardedHeaderFilter forwardedHeaderFilter() {
        return new ForwardedHeaderFilter();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("classpath:public/resources/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/", "/resources/index.html");
    }
}

验证

修改配置后重启Nginx和Spring Boot应用,访问example.com应该不会再出现:8080后缀,重定向也会正常指向example.com/resources/index.html。

内容的提问来源于stack exchange,提问作者john_smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 08:00:59