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

相关推荐
XUEYUAN52128 小时前
ASN 自治系统号风控:平台如何通过 IP 所属自治域批量识别代理流量
python·网络协议·http·网络安全·socks5
CHENKONG_CK9 小时前
破解制鞋打磨痛点:RFID赋能去毛刺工序自动化升级
网络·单片机·嵌入式硬件·网络协议·tcp/ip
2501_916007479 小时前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
Lsetea9 小时前
证书没到期却报certificate has expired:OpenSSL定位中间证书与系统时间
运维·https·ssl证书·openssl·证书链
z落落10 小时前
C#UDP+串口服务端+UDP 客户端(含 CRC16 校验)
网络·网络协议·udp
2501_9160088910 小时前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
SDWAN_Cheap11 小时前
HTTPS到底加密了什么?
https·数据加密
曼巴UE511 小时前
UE5 客户端 需要的网络同步概念总结(3 )-事件同步 RPC 以及三种调用方式
网络·c++·网络协议·学习·rpc·ue5
captain37611 小时前
HTTP(2)
java·网络·http·java-ee
开开心心就好12 小时前
安卓手写文字生成工具,多种纸张一直免费
网络·网络协议·tcp/ip·leetcode·智能手机·电脑·模拟退火算法