nginx代理出https,request.getRequestURL()得到http问题解决

问题:

Java Web 应用中 request.getRequestURL() 返回的 URL 协议是 http,但实际客户端是通过 https 访问的。

原因:

请求路径是:Client → HTTPS → Nginx (SSL Termination) → HTTP → java后端

  • Nginx 负责处理 HTTPS 解密(SSL 终止)。
  • 然后 Nginx 通过 HTTP 协议转发请求到后端 Java 应用(如 Tomcat)。
  • 后端 Java 应用看到的是 HTTP 请求,所以 request.getRequestURL() 返回 http://xxx
  • 但它不知道前端是通过 HTTPS 访问的。

解决方法:

1.Nginx 配置添加标准 Header

bash 复制代码
location /project/ {
        proxy_set_header Host $host:58090;
        proxy_set_header X-Real-IP $remote_addr;
		# 解决request.getRequestURL()请求地址不正确问题
		proxy_set_header X-Forwarded-Proto  $scheme;
		proxy_set_header X-Forwarded-Port $server_port;
		# 将客户端的来源页面信息传递给后端应用
		proxy_set_header Referer $http_referer;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass http://192.168.244.166:8086/project/;
   }

2.springboot项目添加以下配置

java 复制代码
# application.yml Spring Boot 2.1 及更早版本
server:
  use-forward-headers: true
# Spring Boot 2.2+
server:
  forward-headers-strategy: native  # 依赖容器(如 Tomcat)的原生支持
相关推荐
喵个咪2 天前
Go-Wind HTTP 服务器从入门到精通
后端·http·go
Goodbye8 天前
大模型无状态架构:从 HTTP 协议到 Harness AI 工程的深度解析
http
霜落长河14 天前
抛弃TCP改用UDP,HTTP3怎么了?
http
网络研究院15 天前
2026年网络安全
网络·安全·法律·法规·趋势·发展
酣大智15 天前
ARP代理--工作原理
运维·网络·arp·arp代理
treesforest15 天前
AI安全系统如何识别异常访问?IP风险识别正在成为关键能力
网络·人工智能·tcp/ip·安全·web安全
shushangyun_15 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
2601_9618451515 天前
粉笔行测题库|系统班|刷题
网络·百度·微信·微信公众平台·facebook·新浪微博
程序猿阿伟15 天前
《Chrome离线扩展安装的底层逻辑与场景落地指南》
服务器·网络·chrome
之歆15 天前
现代 HTTP 客户端深度解析:Fetch 与 Axios
chrome·网络协议·http