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中不包括访问端口。

相关推荐
天宇&嘘月3 小时前
Nginx的https搭建
网络·nginx·https
rainmanqqst3 小时前
C#Netcore支持Https
网络协议·http·https·c#
rising start6 小时前
三、FastAPI :POST 请求、用户接口设计与 Requests 测试
python·网络协议·http·fastapi
老蒋新思维8 小时前
创客匠人 2025 峰会深度解析:AI 赋能垂直领域,创始人 IP 变现的差异化路径
大数据·网络·人工智能·网络协议·tcp/ip·重构·知识付费
北京耐用通信9 小时前
耐达讯自动化Profibus光纤转换器为您的水处理系统装上“光纤高速路”,数据从此畅通无阻!
网络·人工智能·科技·网络协议·自动化·信息与通信
老蒋新思维9 小时前
创客匠人 2025 峰会深度解析:AI 激活创始人 IP 变现的核心价值
网络·人工智能·网络协议·tcp/ip·创始人ip·创客匠人·知识变现
8***848210 小时前
Nginx代理到https地址忽略证书验证配置
运维·nginx·https
a***113512 小时前
用nginx正向代理https网站
运维·nginx·https
ILL11IIL12 小时前
nginx的https的搭建
网络协议·http·https
2501_9151063212 小时前
iOS 抓不到包怎么办?从 HTTPS 代理排查到 TCP 数据流捕获的全链路解决方案
android·tcp/ip·ios·小程序·https·uni-app·iphone