Nginx HTTP未重定向到HTTPS 400错误请求“普通HTTP请求已发送到HTTPS端口". [英] Nginx HTTP not redirecting to HTTPS 400 Bad Request "The plain HTTP request was sent to HTTPS port"
问题描述
我正在docker中运行nginx. HTTPS可以正常工作,但是当我明确发出HTTP请求时,出现以下错误
I'm running nginx in docker. HTTPS works fine but when I explicitly make HTTP request I get the following error
400错误的请求 普通的HTTP请求已发送到HTTPS端口
400 Bad Request The plain HTTP request was sent to HTTPS port
nginx.conf如下
nginx.conf is as follows
worker_processes auto ;
events {}
http {
include /etc/nginx/mime.types;
access_log /var/log/nginx/main.access.log;
server {
listen 80;
location / {
return 301 https://localhost:3000$request_uri;
}
}
server {
listen 443 ssl;
server_name localhost:3000;
root /var/www/html;
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
location / {
try_files $uri /index.html;
}
}
}
我使用
docker run -p 3000:443 -it -d --name nginxtest nginx-test
并出现以下错误
docker文件如下
docker file is as follows
FROM nginx:latest
COPY ./build /var/www/html
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./ssl /etc/nginx/ssl
EXPOSE 443
CMD [ "nginx","-g","daemon off;" ]
奇怪的是,有时它可以很好地工作,突然间,如果我重新创建容器,它就会停止工作,甚至无法工作.
Weird thing is that sometimes it works perfectly fine, and all of a sudden it stops working and won't even work if I recreate the containers.
甚至尝试执行以下操作.还是没有运气
Even tried doing the following. Still no luck
server {
listen 80;
server_name localhost:3000
return 301 https://localhost:3000$request_uri;
}
当我运行以下docker命令时另一个奇怪的事情
Another odd thing when I run the following docker command
docker run -p 3000:443 -p 3001:80 -it -d --name nginxtest nginx-test
并转到localhost:3001,它将我重定向到https很好,但是其他事情确实中断了. 很抱歉这个问题
and go to localhost:3001 it redirects me to https just fine but other things do break. Sorry for the long question
推荐答案
将以下指令放入您侦听端口443的服务器块中.
Put the following directive to the server block where you listen for port 443.
error_page 497 https://$host:$server_port$request_uri;
此伪指令意味着发生纯HTTP请求已发送到HTTPS端口"时,将其重定向到当前主机名,端口和URI的https版本.
This directive implies that when "The plain HTTP request was sent to HTTPS port" happens, redirect it to https version of current hostname, port and URI.
Kinda hacky但可以.
Kinda hacky but works.
这篇关于Nginx HTTP未重定向到HTTPS 400错误请求“普通HTTP请求已发送到HTTPS端口".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!