简体   繁体   English

Spring Boot - 为什么反向代理后应用程序端口连接到dns?

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

I created a spring boot app behind an nginx proxy.我在 nginx 代理后面创建了一个 spring 引导应用程序。
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.)当从 nginx 端口 80 接收到特定的 dns (example.com) 时,将反向代理配置为 go 到 spring 启动应用程序。(spring 启动的端口为 8080。)

The strange thing here is that when you connect to example.com, 8080, the port of spring boot, is attached to the back.这里奇怪的是,当你连接到example.com时,8080,spring boot的端口,是附在后面的。

ex) example.com:8080/

So, ":8080" is added in front of "/resources/index.html", which is a redirect of "/".所以,在“/resources/index.html”前面加上“:8080”,这是对“/”的重定向。

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

I want to get rid of:8080 here.我想在这里摆脱:8080。

my spring code我的 spring 代码

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.请告诉我为什么访问example.com时附加了8080以及如何解决,将不胜感激。

you need to also add following into nginx config:您还需要将以下内容添加到 nginx 配置中:

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.如果spring-boot版本足够新,则无需在spring-boot端进行设置,一切都应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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