400 Bad Request The plain HTTP request was sent to HTTPS port

配置 https 的时候遇到这样一个问题,直接访问 https://www.domain.com 正常,访问 http://www.domain.com 报错:400 Bad Request The plain HTTP request was sent to HTTPS port。


nginx 配置如下:

server {
    listen 80 default_server;
    listen 443 default_server;

    ssl on;
    ssl_certificate /vagrant/cert/nginx.crt;
    ssl_certificate_key /vagrant/cert/nginx.key;
}

有两种解决方案:

server {
    listen 80 default_server;
    return 301 https://$host$request_uri;
}

server {
    listen 443 default_server;
    ssl on;
    ssl_certificate /vagrant/cert/nginx.crt;
    ssl_certificate_key /vagrant/cert/nginx.key;
}
server {
    listen 80 default_server;
    listen 443 ssl default_server;

    ssl_certificate /vagrant/cert/nginx.crt;
    ssl_certificate_key /vagrant/cert/nginx.key;

    if ($https != 'on') {
        return 301 https://$host$request_uri;
    }
}

 584 total views,  4 views today

Leave a Reply

Your email address will not be published. Required fields are marked *