当前位置: 首页 > 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端口
  • 保留原始路径不变

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

相关文章:

  • [Zynq] Zynq Linux 环境下 AXI UART Lite 使用方法详解(代码示例)
  • opencv如何在仿射变换后保留完整图像内容并自动裁剪
  • OpenCV 图像通道的分离与合并
  • elementUI点击浏览table所选行数据查看文档
  • 解决el-select选择框右侧下拉箭头遮挡文字问题
  • vue2 , el-select 多选树结构,可重名
  • 【请关注】MySQL 碎片整理提升性能
  • 在word中点击zotero Add/Edit Citation没有反应的解决办法
  • 文档处理组件Aspose.Words 25.5全新发布 :六大新功能与性能深度优化
  • 【Android基础回顾】七:内存管理机制
  • Android7 Input(十)View 处理Input事件pipeline
  • 【android bluetooth 协议分析 02】【bluetooth hal 层详解 7】【高通蓝牙hal-读流程介绍】
  • 国产linux系统(银河麒麟,统信uos)使用 PageOffice在线编辑word文件保存数据同时保存文件
  • 第46节:多模态分类(图像+文本)
  • Java在word中指定位置插入图片。
  • 如何在电脑上轻松访问 iPhone 文件
  • LabVIEW自感现象远程实验平台
  • Java编程课(一)
  • 力扣HOT100之二分查找: 34. 在排序数组中查找元素的第一个和最后一个位置
  • Android Test2 获取系统android id
  • 创业型企业网站模板/关键词排名零芯互联关键词
  • 广告制作材料价格单/qq群怎么优化排名靠前
  • 网站开发评审时间安排/百度seo排名360
  • 关键词优化seo/seo网站推广案例
  • 福建省华荣建设集团有限公司网站/软文写作技巧有哪些
  • 电影网站开发/seo排名点击软件推荐