简体   繁体   中英

Spring Boot - Why is the app port attached to dns after reverse proxying?

I created a spring boot app behind an nginx proxy.
When a specific dns (example.com) is received from nginx port 80, a reverse proxy is configured to go to the spring boot app.(The port of spring boot is 8080.)

The strange thing here is that when you connect to example.com, 8080, the port of spring boot, is attached to the back.

ex) example.com:8080/

So, ":8080" is added in front of "/resources/index.html", which is a redirect of "/".

example.com/ -> example.com:8080/resources/index.html

I want to get rid of:8080 here.

my spring code

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;

@Configuration
@EnableWebMvc
public class WebConfiguration implements WebMvcConfigurer {

    @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");
    }
}

my proxy code

server {
    listen       80;
    server_name  example.com;

    location / {
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header HOST $http_host;
         proxy_set_header X-NginX-Proxy true;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 
         proxy_pass [app-ip]:8080;
         proxy_redirect off;
   }
}

I would appreciate it if you could tell me why 8080 is attached when accessing example.com and how to solve it.

you need to also add following into nginx config:

proxy_set_header    X-Forwarded-Host   $host;
proxy_set_header    X-Forwarded-Port   $server_port;
proxy_set_header    X-Forwarded-Proto  $scheme;

if spring-boot version is recent enough everything should work without setup on spring-boot side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM