东莞在哪里学网站建设,微信做一个小程序需要多少钱,网站权重2的网站,自己怎么制作小程序商城接口请求发生问题#xff1a; 解决方法#xff1a;
Nginx HTTP服务器的报错 “400 Bad Request: The plain HTTP request was sent to HTTPS port”#xff0c;本文将讲解如何解决这个问题。简单从报错的字面意思上来看#xff0c;是因为HTTP请求被发送到HTTPS端口#x…接口请求发生问题 解决方法
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
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” , 例如
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 访问他们就不会遇到上述错误。此外如果你有其他的网站配置为不使用SSLNginx会尝试使用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”。
解决办法
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。
1proxy_pass执行前先设置了request head host 为https外网访问的域名端口
2proxy_pass执行后tomcat结果返回response
3proxy_redirect修改response中的location中的协议http为https外网访问的协议。
注java redirect重定向主要是通过访问tomcat服务的请求head项来决定的默认是http协议域名是通过读取host地址默认host中不包括访问端口。