当前位置: 首页 > news >正文

负载均衡将https请求转发后端http服务报错:The plain HTTP request was sent to HTTPS port

https请求报错:The plain HTTP request was sent to HTTPS port


示例背景描述:

  • www.test.com:11001服务需要对互联网使用https提供服务
  • 后端java服务不支持https请求,且后端程序无法修改,仅支持http请求

问题描述:

  • 因此在访问https://www.test.com:11001,URL会跳转http://www.test.com:11001,然后报错The plain HTTP request was sent to HTTPS port

报错如下:
在这里插入图片描述

解决思路:通过nginx代理

此处暂时将后端服务器设定为192.168.1.1,具体配置如下:

server {listen 11001;server_name  localhost;location / {proxy_pass http://192.168.1.1:11001/;proxy_set_header Host $host:$server_port; # 包含端口号防止丢失proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto https; # 强制设置协议为 HTTPSproxy_set_header X-Forwarded-Port $server_port;  # 传递端口# 重定向修正proxy_redirect ~^(http://[^:/]+)(:([0-9]+))?/(.*)$ https://$host:$server_port/$4;proxy_redirect ~^(http://)([^/]+)/(.*)$ https://$host:$server_port/$3;# 可选:日志调试access_log /var/log/nginx/redirect_fix.log main;error_log /var/log/nginx/redirect_error.log debug;# 可选配置proxy_connect_timeout 60s;proxy_read_timeout 60s;}# 可选配置error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}

关键配置说明

1、proxy_redirect 正则表达式​:

proxy_redirect ~^(http://[^:/]+)(:([0-9]+))?/(.*)$ https://$host:$server_port/$4;
  • 匹配任何 HTTP 重定向:http://domain:port/path 或 http://domain/path
  • 替换为:https://原始域名:11001/path

2、​备用规则​:​

proxy_redirect ~^(http://)([^/]+)/(.*)$ https://$host:$server_port/$3;
  • 匹配格式:http://anything/path
  • 替换为:https://原始域名:11001/path

3、​强制协议头部​:

proxy_set_header X-Forwarded-Proto https;
  • 即使后端服务不使用这个头部,也设置它以保持一致性

测试与验证方法
1、测试配置:

nginx -t

2、重载配置:

nginx -s reload

3、监控日志:

tail -f /var/log/nginx/access.log 
tail -f /var/log/nginx/error.log
tail -f /var/log/nginx/redirect_fix.log
tail -f /var/log/nginx/redirect_error.log

4、重新访问测试
https://www.test.com:11001


常见问题排查表

问题现象可能原因解决方案
重定向循环后端服务也尝试重定向检查后端响应是否已经是HTTPS
端口号不正确正则匹配失败添加更通用的匹配规则
HTTPS证书错误证书不匹配检查SLB证书是否有效
404 错误路径被修改检查正则中的路径捕获组 如 $4 和 $3

如果上述配置仍有问题,使用这个更强大的版本:

proxy_redirect ~^http(://[^/]+)?(:\d+)?/(.*)$ https://$host:$server_port/$3;
proxy_redirect ~^http(://)([^/]+)/(.*)$ https://$host:$server_port/$3;
proxy_redirect default;

这个规则会:

  • 匹配任何以 http 开头的重定向
  • 替换为 https + 原始域名 + 11001端口
  • 保留原始路径不变

最后我想说:请不要以此视为定论,这只是我的个人经验

http://www.lqws.cn/news/144865.html

相关文章:

  • 模块化架构下的前端调试体系建设:WebDebugX 与多工具协同的工程实践
  • 【图像处理3D】:焦距的像素单位标定
  • 深入浅出 Scrapy:打造高效、强大的 Python 网络爬虫
  • Xcode 16.4 + iOS 18 系统运行时崩溃:___cxa_current_primary_exception 符号丢失的原因与解决方案
  • 基于cornerstone3D的dicom影像浏览器 第二十八章 LabelTool文字标记,L标记,R标记及标记样式设置
  • AMFCNN-RKD:齿轮故障诊断的轻量级多传感器融合模型详解(python代码复现)
  • STM32 NVIC中断控制器
  • 鸿蒙APP测试实战:从HDC命令到专项测试
  • XHR / Fetch / Axios 请求的取消请求与请求重试
  • 【Linux】网络--数据链路层--以太网
  • 4.2 HarmonyOS NEXT分布式AI应用实践:联邦学习、跨设备协作与个性化推荐实战
  • Elasticsearch:spring2.x集成elasticsearch8.x
  • CB/T 3361-2019 甲板敷料检测
  • HarmonyOS:Counter计数器组件
  • 免费工具-微软Bing Video Creator
  • 塑料回收新突破!Nature 重磅:2 小时解聚碳纤维废料
  • 【推荐算法】WideDeep推荐模型:融合记忆与泛化的智能推荐引擎
  • 【Linux】POSIX信号量
  • 第20讲、Odoo 18 翻译机制与 PO 文件详解
  • YOLOv8 × VisDrone 全流程实战:训练你的无人机识别模型 AI(第一部分:数据集准备)
  • 鸿蒙缺少WMIC missing WMIC
  • 《C++ 模板》
  • 仓库自动化搬运:自动叉车与AGV选型要点及核心技术解析
  • MyBatis之测试添加功能
  • 18650锂电池组点焊机:高效组装锂电池的关键工具|比斯特自动化
  • XDMA pcie环路测试
  • Oracle 的 SEC_CASE_SENSITIVE_LOGON 参数
  • 代码中文抽取工具并替换工具(以ts为例)
  • 在 CentOS 上将 Ansible 项目推送到 GitHub 的完整指南
  • 高效线程安全的单例模式:Python 中的懒加载与自定义初始化参数