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

相关推荐
无名之逆8 分钟前
Build High-Performance Web Services with Hyperlane
服务器·开发语言·前端·http·rust
前进的程序员1 小时前
FTP 协议介绍与 C 语言开发案例
网络协议·ftp
网安小Q2 小时前
kali工具集-sslscan安全检测与HTTPS服务器部署实践
linux·网络安全·https
Hello.Reader2 小时前
零基础弄懂 ngx_http_slice_module分片缓存加速
网络协议·http·缓存
源远流长jerry2 小时前
计算机网络相关面试题
chrome·网络协议·http·tcp
jiaxingcode5 小时前
HTTPS和HTTP区别
服务器·网络协议·https
IP管家7 小时前
跨境支付风控失效?用代理 IP 构建「地域 - 设备 - 行为」三维防护网
网络·网络协议·tcp/ip·网络安全
YGGP10 小时前
应用层协议简介:以 HTTP 和 MQTT 为例
mqtt·网络协议·http·rpc
Chocolate_men10 小时前
ftp、http下载远程文件(多线程、断点续传)
python·网络协议·http
LB211212 小时前
UDP的单播组播与广播
网络·网络协议·tcp/ip·udp