Nginx 删除与多个虚拟主机的 SSL 握手

kfr*_*ede 2 ssl redirect nginx

我在具有多个主机(1 个 IP)的 Ubuntu VPS 上设置了一个 Nginx 服务器。以前,1 台主机设置了证书并且没有重定向(允许 http),1 台主机有证书并通过 301 强制使用 HTTPS。现在我正在尝试强制所有主机使用 SSL 并强制使用 HTTPS,我看到 Nginx 正在下降当我有超过 1 个带有 301 指令的 vhost 时握手。特别是,我看到的错误是:

[error] 12370#0: *30 no "ssl_certificate" is defined in server listening on SSL port while SSL handshaking, client: (removed), server: 0.0.0.0:443

问题似乎肯定出在我的 301 上,因为如果我将它们排除在外,我就没有问题。在我的服务器块中强制使用 HTTPS 和非 www 的最佳方法是什么?

我所有的虚拟主机都在/etc/nginx/conf.d,以及ssl.conf(如下所列)。nginx.conf如果需要,我可以提供,但我在那里没有看到任何有用的东西。

example1.conf

server {
    server_name www.example1.com example1.com;
    return 301 https://example1.com$request_uri;
}

server {
    listen 443;

    server_name www.example1.com
    return 301 https://example1.com$request_uri;
}

server {
    listen 443 ssl;

    server_name example1.com;

    ssl_certificate /etc/letsencrypt/live/example1.com/cert.pem;
    ssl_certificate_key /etc/letsencrypt/live/example1.com/privkey.pem;

    root /var/www/example1.com;

    location / {
        try_files $uri $uri/ $uri.html =404;
    }

    access_log /var/log/nginx/example1.com.access.log;
    error_log /var/log/nginx/example1.com.error.log;
}
Run Code Online (Sandbox Code Playgroud)

example2.conf
相同example1.conf(除了 example2.com 而不是 example1.com)

ssl.conf

ssl_session_cache    shared:SSL:10m;
ssl_session_timeout  10m;

# Perfect Forward Security
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS +RC4 RC4";

# HSTS
add_header Strict-Transport-Security max-age=31536000;
Run Code Online (Sandbox Code Playgroud)

指出其他明显错误也值得赞赏。

kfr*_*ede 6

通过删除解决(至少现在)

server {  
    listen 443;

    server_name www.example1.com
    return 301 https://example1.com$request_uri;
}
Run Code Online (Sandbox Code Playgroud)

似乎由于没有指定证书,因此所有 HTTPS 请求都会命中此块,然后连接将被删除。