修复 Nginx 中的“普通 HTTP 请求已发送到 HTTPS 端口”错误
在本文中,我们将展示如何解决 Nginx HTTP 服务器中的“400 Bad Request: The plain HTTP request was sent to HTTPS port”。当您尝试配置 Nginx 来处理 HTTP 和 HTTPS 请求时,通常会出现此错误。
出于本指南的目的,我们正在考虑一种场景,其中 nginx 为通过服务器块(或 Apache 中的虚拟主机)实现的多个网站提供服务,只有一个网站使用 SSL,其余网站不使用。
另请阅读:保护、强化和提高 Nginx 性能的终极指南
我们还将考虑下面的 SSL 配置示例(出于安全原因,我们更改了实际域名),它告诉 nginx 监听端口 80 和 443。默认情况下,所有 HTTP 请求都应重定向到 HTTPS。
Nginx 示例配置
server{
listen 80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com www.example.com;
root /var/www/html/example.com/;
index index.php index.html index.htm;
#charset koi8-r;
access_log /var/log/nginx/example.com/example.com_access_log;
error_log /var/log/nginx/example.com/example.com_error_log error;
# SSL/TLS configs
ssl on;
ssl_certificate /etc/ssl/certs/example_com_cert_chain.crt;
ssl_certificate_key /etc/ssl/private/example_com.key;
include /etc/nginx/ssl.d/ssl.conf;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html/example.com/;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /var/www/html/example.com/;
fastcgi_pass 127.0.0.1:9001;
#fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include /etc/nginx/fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
使用上述配置,一旦客户端尝试通过端口 80(即 http://example.com
)访问您的网站,相关错误将显示如下截屏。
您遇到此错误是因为每次客户端尝试通过 HTTP 访问您的站点时,请求都会重定向到 HTTPS。这是因为 nginx 期望在事务中使用 SSL,但原始请求(通过端口 80 接收)是纯 HTTP,它会抱怨该错误。
另一方面,如果客户端使用 https://example.com
,则不会遇到上述错误。此外,如果您将其他网站配置为不使用 SSL,nginx 会默认尝试使用 HTTPS,从而导致上述错误。
要修复此错误,请在配置中注释掉以下行或将其设置为关闭。
#ssl on
OR
ssl off
保存并关闭文件。然后重启nginx服务。
systemctl restart nginx
OR
sudo systemctl restart nginx
这样,您可以使 nginx 处理多个服务器块的 HTTP 和 HTTPS 请求。
最后,下面是有关在常见 Linux 发行版和 FreeBSD 上设置 SSL HTTPS 的文章列表。
- 使用 Let’s Encrypt SSL 证书为 RHEL/CentOS 上的 Nginx 设置 HTTPS
- 在 Ubuntu 和 Debian 上使用免费的 Let’s Encrypt SSL 证书保护 Nginx
- 如何在 FreeBSD 中使用 SSL 和 Let’s Encrypt 保护 Nginx
目前为止就这样了。如果您知道解决此错误的任何其他方法,请通过下面的反馈表告知我们。