400 The plain HTTP request was sent to HTTPS port

接口请求发生问题:

解决方法:

Nginx HTTP服务器的报错 "400 Bad Request: The plain HTTP request was sent to HTTPS port",本文将讲解如何解决这个问题。简单从报错的字面意思上来看,是因为HTTP请求被发送到HTTPS端口,这种报错多出现在Nginx既处理HTTP请求又处理HTTPS请求的情况。

以下是Nginx常用的SSL配置(出于安全原因,我们使用了本站域名),配置文件将让Nginx侦听80和443端口,并将所有的HTTP请求重定向到HTTPS:

bash 复制代码
upstream dce_ingress_https {
#DMP2.4        server 10.x x x.134:39604;
        server 10.xxx.xxx.52:32822;
        server 10.xxx.xxx.54:80;
        server 10.xxx.xxx.54:443;
        keepalive 256;
}

server {
      listen 80 ;
      listen 443 ssl;
      server_name  xxx.xxx.net;      
      ssl_certificate /etc/pki/tls/certs/xxx.xxx.net/xxx.xxx.net.crt;
      ssl_certificate_key /etc/pki/tls/private/xxx.xxx.net/xxx.xxx.net.nokey;
      
      ssl_session_cache  builtin:1000  shared:SSL:10m;
#      ssl_protocols  TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
      ssl_protocols  TLSv1.2 TLSv1.3;
      ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
      ssl_prefer_server_ciphers on;

      location / {
        proxy_pass http://dce_ingress_https/;
        proxy_next_upstream error timeout http_503 http_500 http_504 non_idempotent;
        #proxy_read_timeout 8;
#      proxy_set_header Host $host;
        proxy_set_header Host xxx.xxx.net;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Connection keep-alive;            
      }

      location ~/abc/1.0/(.*)/12/ {
          add_header "new-header" $1;
          default_type "text/html";
          return 200 $1;
      }

      error_page 404 /404.html;
          location = /40x.html {
      }

      error_page 500 502 503 504 /50x.html;
          location = /50x.html {
      }
}

以上的配置看上去都很正常,但是用户请求如果通过80端口来访问网站时,例如使用http://blog.yoodb.com,那么这个请求就会在浏览器收到错误nginx 400 bad request"The plain HTTP request was sent to HTTPS port" , 例如:

bash 复制代码
curl 'http://xxx.xxx.net/xxx/v1.0.0/enquiry/bu/xxx/stocks' \
--header 'Content-Type: application/json' \
--data '{
    "locations": [
        {
            "location": "3228",
            "locationGroup": "STORE"
        }
    ],
    "items": [
        {
            "plu": "100141",
            "pluType": "ITEM"
        }
    ]
}'

Nginx报这种错误是因为每一次用户请求试图通过HTTP访问你的网站,这个请求被重定向到HTTPS。于是Nginx预计使用SSL交互,但原来的请求(通过端口80接收)是普通的HTTP请求,于是会产生错误。

另一方面,如果一个用户使用 https://xxx.xxx.net/xxx/v1.0.0/enquiry/bu/xxx/stocks 访问,他们就不会遇到上述错误。此外,如果你有其他的网站配置为不使用SSL,Nginx会尝试使用HTTPS,这种情况下也会造成上述错误。

解决办法:

1、将上面配置文中的"ssl on; " 注释掉或者修改成 "ssl off;";

2、"listen 443;"修改为"listen 443 ssl";新增"listen 80",这样Nginx就可以同时处理HTTP请求和HTTPS请求了;

3、去掉 upstream 中 server 10.xxx.xxx.54:80; server 10.xxx.xxx.54:443; 配置; <我们属于这种情况>

问题补充:

java redirect重定向https跳转http问题,如果https访问nginx通过nginx proxy_pass到http的tomcat服务正常能够访问,但是java redirect就跳转到http,导致报错"400 Bad Request: The plain HTTP request was sent to HTTPS port"。

解决办法:

bash 复制代码
proxy_set_header Host $host;
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;
proxy_pass http://172.17.6.114:8082;
proxy_redirect http:// https://;

实现流程是根据nginx的不同执行阶段,来完成Location http到https。

1)proxy_pass执行前,先设置了request head host 为https外网访问的域名+端口

2)proxy_pass执行后,tomcat结果返回response

3)proxy_redirect修改response中的location中的协议http为https外网访问的协议。

注:java redirect重定向主要是通过访问tomcat服务的请求head项来决定的,默认是http协议,域名是通过读取host地址,默认host中不包括访问端口。

相关推荐
F1FJJ15 分钟前
Shield CLI 命令全解析:15 个命令覆盖所有远程访问场景
网络·数据库·网络协议·容器·开源软件
nbsaas-boot3 小时前
基于 HTTP 构建 MCP Tools 的完整工程解析
网络·网络协议·http·ai
i建模3 小时前
SSL: CERTIFICATE_VERIFY_FAILED feishu 机器人CoPaw
运维·网络·网络协议·ssl·openclaw
王码码20353 小时前
Flutter for OpenHarmony:使用 pluto_grid 打造高性能数据网格
flutter·http·华为·架构·harmonyos
先跑起来再说3 小时前
从原理到实践:彻底搞懂Cookie和Session的区别
计算机网络·http·https
兰.lan3 小时前
【黑马ai测试】HTTP协议-抓包工具定位-弱网测试-缺陷介绍
网络·python·网络协议·http
liwulin05065 小时前
【ESP32-S3】WiFi 网页遥控 + WebSocket 实时控制编码电机+ 实时数据显示 + 急停 + 速度滑块
网络·websocket·网络协议
℡ 萧6 小时前
OSPF开销值、协议优先级及计时器的修改-新版(17)
网络·网络协议·网络安全·智能路由器·信息与通信
阿捏利7 小时前
详解网络协议(二)OSI七层参考模型
网络·网络协议
2301_794799517 小时前
35_简单快捷不可靠的_UDP ## 网络协议那些事儿
网络·网络协议·udp