解决故障码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:
server {
listen 443;
server_name blog.yoodb.com;
charset UTF-8;
ssl on;
ssl_certificate /usr/local/nginx/conf/ssl/blog/2539791_blog.yoodb.com.pem;
ssl_certificate_key /usr/local/nginx/conf/ssl/blog/2539791_blog.yoodb.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
if ($scheme = http) {
return 301 https://$host$request_uri;
}
location / {
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;
}
location ~*/upload/images/ {
expires 1h;
root /mnt/app/project/files;
}
location ~*/dynamic/images/ {
expires 1h;
root /mnt/app/project/files;
}
}
以上的配置看上去都很正常,但是用户请求如果通过80端口来访问网站时,例如使用http://blog.yoodb.com,那么这个请求就会在浏览器收到错误nginx 400 bad request“The plain HTTP request was sent to HTTPS port
”,示例图片如下:
Nginx报这种错误是因为每一次用户请求试图通过HTTP访问你的网站,这个请求被重定向到HTTPS。于是Nginx预计使用SSL交互,但原来的请求(通过端口80接收)是普通的HTTP请求,于是会产生错误。
另一方面,如果一个用户使用https://blog.yoodb.com访问网站,他们就不会遇到上述错误。此外,如果你有其他的网站配置为不使用SSL,Nginx会尝试使用HTTPS,这种情况下也会造成上述错误。
解决办法:
将上面配置文中的“ssl on; ” 注释掉或者修改成 “ssl off;”;“listen 443;”修改为“listen 443 ssl”;新增“listen 80”,这样Nginx就可以同时处理HTTP请求和HTTPS请求了,具体参考如下:
server {
listen 80
listen 443 ssl;
server_name blog.yoodb.com;
charset UTF-8;
ssl_certificate /usr/local/nginx/conf/ssl/blog/2539791_blog.yoodb.com.pem;
ssl_certificate_key /usr/local/nginx/conf/ssl/blog/2539791_blog.yoodb.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
if ($scheme = http) {
return 301 https://$host$request_uri;
}
location / {
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;
}
location ~*/upload/images/ {
expires 1h;
root /mnt/app/project/files;
}
location ~*/dynamic/images/ {
expires 1h;
root /mnt/app/project/files;
}
}
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。
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中不包括访问端口。
本文转自:https://blog.yoodb.com/yoodb/article/detail/1527
解决故障码400,“”The plain HTTP request was sent to HTTPS port“”的更多相关文章
- git学习------> 解决Gitlab 版本升级之后,发送 merge request 出现 http 500 的返回码错误
今天有同事在Gitlab上发送 Merge Request的时候,直接出现如下所示的界面,提示http 500,服务器内部出错. 一.错误描述 1.1 创建新的 Merge Request 1.2 填 ...
- 解决 android studio 出现:"AndroidStudio:Could not GET 'https://dl.google.com Received status code 400 from server: Bad Request"问题
一.android studio 编译项目时出现"AndroidStudio:Could not GET 'https://dl.google.com Received status cod ...
- Diagnostic Trouble Code诊断故障码
所有电子控制单元(ECU)都会根据整车厂规范要求对相关故障进行记录,并储存在NVM(也称作EEPROM)相当于平常电脑上硬盘里.除故障代码外,还会记录故障发生时相关整车情况(如供电电压,环境温度 ...
- docker升级后启动报错400 Client Error: Bad Request ("Unknown runtime specified docker-runc")
宝塔面板docker升级后启动容器时报错400 Client Error: Bad Request ("Unknown runtime specified docker-runc" ...
- Android Studio Gradle build 报错:Received status code 400 from server: Bad Request
错误提示如下 Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.1.2/ ...
- DRF框架(一)——restful接口规范、基于规范下使用原生django接口查询和增加、原生Django CBV请求生命周期源码分析、drf请求生命周期源码分析、请求模块request、渲染模块render
DRF框架 全称:django-rest framework 知识点 1.接口:什么是接口.restful接口规范 2.CBV生命周期源码 - 基于restful规范下的CBV接口 3.请求组件 ...
- (转)Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议
转自http://www.cnblogs.com/interdrp/p/4881785.html 最近在做一个项目, 架构上使用了 Nginx +tomcat 集群, 且nginx下配置了SSL,to ...
- Nginx SSL+tomcat集群,request.getScheme() 取到https正确的协议
最近在做一个项目, 架构上使用了 Nginx +tomcat 集群, 且nginx下配置了SSL,tomcat no SSL,项目使用https协议 但是,明明是https url请求,发现 log里 ...
- request.getScheme() 取到https正确的协议(转载)
最近在做一个项目, 架构上使用了 Nginx +tomcat 集群, 且nginx下配置了SSL,tomcat no SSL,项目使用https协议 但是,明明是https url请求,发现 log里 ...
随机推荐
- 读写Session
读写Session Session是保存在服务端的字典 Session与Cookie有些类似,都是通过字典管理key-value对,只不过Cookie是保存在客户端的字典,而Session是保存在服务 ...
- MemCache在.NET中使用Memcached.ClientLibrary详解
本文说明:memcached分布式缓存的负载均衡配置比例,数据压缩,socket的详细配置等,以及在.net中的常用方法 首先下载客户端的3个dll,ICSharpCode.SharpZipLib.d ...
- IOI 2005/bzoj 1812:riv 河流
Description 几乎整个Byteland王国都被森林和河流所覆盖.小点的河汇聚到一起,形成了稍大点的河.就这样,所有的河水都汇聚并流进了一条大河,最后这条大河流进了大海.这条大河的入海口处有一 ...
- mysql 触发器检测表数据添加,进而调用存储过程检测数据,进而调用存储过程添加数据
触发器: beginDECLARE user_mes INT(64);SELECT user_id into user_mes FROM order_orderlist where id = new. ...
- Docker 安装 PHP
安装 PHP 镜像 查找Docker Hub上的php镜像 docker search php 这里我们拉取官方的镜像,标签为5.6-fpm docker pull php:5.6-fpm Nginx ...
- jumpserver0.4.0与python3版本安装
环境: 系统:CentOS 6.5 Python版本:Python3.6 安装目录:/Data/apps/ 一. 环境准备: 1. 基本工具库: # yum -y install sqlite-de ...
- windows 清理 cbs.log 文件
请参考以下步骤 1.win+r输入 services.msc进入服务,找到并双击Windows Modules Installer点击停止, 2.然后就可以手动删除 3.按一的步骤开启TrustedI ...
- 用java转换文件的字符集
中文乱码真的是让人很头疼问题,有了这个方法应该能缓解这种头疼,用的是递归方式查找文件,直接在原文件中修改,小心使用(在本地测试效果有点诡异呀,没有达到预期效果). package com.hy.uti ...
- redis主从复制初识
一.作用 slave会通过被复制同步master上面的数据,形成数据副本 当master节点宕机时,slave可以升级为master节点承担写操作. 允许有一主多从,slave可以承担读操作,提高读性 ...
- SSM框架的整合与使用——实现简单的转账系统
一.整合思路 SSM框架即SpringMVC + Spring + MyBati框架集,是一种轻量级的Web开源框架.它们各自在JAVA三层架构中负责的模块如下图所示: 其中,SpringMVC与Sp ...