我是靠谱客的博主 真实苗条,这篇文章主要介绍ServletUriComponentsBuilder遇到Nginx反向代理时,无法识别HTTPS前言ServletUriComponentsBuilder遇到Nginx反向代理时,无法识别HTTPS的原因解决办法参考,现在分享给大家,希望可以做个参考。
前言
- spring mvc 5.2.9.RELEASE
- springboot 2.3.4.RELEASE
ServletUriComponentsBuilder遇到Nginx反向代理时,无法识别HTTPS的原因
spring 5.0.X ServletUriComponentsBuilder 会读取 X-Forwarded-Prefix 头,但spring 5.1.X 就不再处理。
其在官方注释如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16/** * UriComponentsBuilder with additional static factory methods to create links * based on the current HttpServletRequest. * * <p><strong>Note:</strong> As of 5.1, methods in this class do not extract * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the * client-originated address. Please, use * {@link org.springframework.web.filter.ForwardedHeaderFilter * ForwardedHeaderFilter}, or similar from the underlying server, to extract * and use such headers, or to discard them. * * @author Rossen Stoyanchev * @since 3.1 */ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
解决办法
springboot中添加ForwardedHeaderFilter
复制代码
1
2
3
4
5
6
7
8@Configuration public class ConfigForwardedHeaderFilter { @Bean public Filter forwardedHeaderFilter() { return new ForwardedHeaderFilter(); } }
nginx中添加 X-Forwarded-*
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15location ^~ /front{ proxy_pass http://127.0.0.1:8014; # $server_port端口,让request.getServerPort()能够获取正确的端口 proxy_set_header Host $host:$server_port; proxy_set_header Remote_Addr $remote_addr; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # https请求转成http请求时,weblogic设置 proxy_set_header WL-Proxy-SSL true; # https请求转成http请求时,tomcat设置 proxy_set_header X-Forwarded-Proto $scheme; # index页面设置 index index; }
X-Forwarded-For
: HTTP 请求端真实 IPX-Forwarded-Host
: 用户在浏览器中看到的Host。域名或者ip。X-Forwarded-Port
: 用户在浏览器中看到的Port。80和443为默认端口,浏览器不显示这两个端口。X-Forwarded-Proto
: 用户在浏览器中看到的协议。Http或HttpsX-Forwarded-Prefix
: 当nginx转发请求后,省略的url部分。比如将prefix/a/b/c
, 转发到/a/b/c
,prefix
就是X-Forwarded-Prefix
。
参考
https://github.com/spring-projects/spring-hateoas/issues/862
https://github.com/spring-projects/spring-boot/issues/16492
最后
以上就是真实苗条最近收集整理的关于ServletUriComponentsBuilder遇到Nginx反向代理时,无法识别HTTPS前言ServletUriComponentsBuilder遇到Nginx反向代理时,无法识别HTTPS的原因解决办法参考的全部内容,更多相关ServletUriComponentsBuilder遇到Nginx反向代理时,无法识别HTTPS前言ServletUriComponentsBuilder遇到Nginx反向代理时,无法识别HTTPS内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复