[英]Spring Boot - Why is the app port attached to dns after reverse proxying?
我在 nginx 代理后面創建了一個 spring 引導應用程序。
當從 nginx 端口 80 接收到特定的 dns (example.com) 時,將反向代理配置為 go 到 spring 啟動應用程序。(spring 啟動的端口為 8080。)
這里奇怪的是,當你連接到example.com時,8080,spring boot的端口,是附在后面的。
ex) example.com:8080/
所以,在“/resources/index.html”前面加上“:8080”,這是對“/”的重定向。
example.com/ -> example.com:8080/resources/index.html
我想在這里擺脫:8080。
我的 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");
}
}
我的代理代碼
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;
}
}
請告訴我為什么訪問example.com時附加了8080以及如何解決,將不勝感激。
您還需要將以下內容添加到 nginx 配置中:
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
如果spring-boot
版本足夠新,則無需在spring-boot
端進行設置,一切都應該可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.