Nginx TLS-SNI:对 HTTPS 使用主机名相关的 SSL

Utk*_*ava 3 nginx

我需要使用两个不同的 ssl 证书,其中 nginx 指向同一个应用程序。

https://domain1.com指向 1.1.1.1
https://domain2.com指向 1.1.1.1 。



https://domainN.com指向 1.1.1.1

尝试了以下方法:

server {
listen 80;
server_name domain1.com;
return 301 https://$host$request_uri;
}



server {

    listen 443 ssl;
    server_name domain1.com;
    root /app/dist;

    index index.html;

    ssl_certificate /etc/nginx/ssl/d1/certificate.crt;
    ssl_certificate_key /etc/nginx/ssl/d1/private.key;

    location / {

        try_files $uri $uri/ /index.html;

    }

}

server {
listen 80;
server_name domain2.com;
return 301 https://$host$request_uri;
}



server {

    listen 443 ssl;
    server_name domain2.com;
    root /app/dist;

    index index.html;

    ssl_certificate /etc/nginx/ssl/d2/certificate.crt;
    ssl_certificate_key /etc/nginx/ssl/d2/private.key;

    location / {

        try_files $uri $uri/ /index.html;

    }

}
Run Code Online (Sandbox Code Playgroud)

这不起作用,它只是加载第一个证书,导致使用第二个域访问时证书无效。

域证书无法组合。我无法为 nginx 旋转两个不同的实例,因为这种情况需要帮助我解决指向相同 IP 的 n 个域,最好使用一台 nginx 服务器。

有出路吗?

Utk*_*ava 6

感谢理查德·史密斯指出了正确的东西!

因此,要设置 nginx 对指向同一 nginx 的域使用不同的证书密钥对,我们必须依赖 TLS-SNI(服务器名称指示),其中域名作为握手的一部分以未加密的文本发送。这有助于 nginx 决定将哪个证书密钥对用于传入的安全请求。

可以在此处阅读有关 SNI 的更多信息。

继续进行配置。

server {
listen 80;
server_name domain1.com;
return 301 https://$server_name$request_uri;
}



server {

    listen 443 ssl;
    server_name domain1.com;
    root /app/dist;
    index index.html;
    ssl_certificate /etc/nginx/ssl/d1/certificate.crt;
    ssl_certificate_key /etc/nginx/ssl/d1/private.key;

    location / {    
        try_files $uri $uri/ /index.html;
    }
}


server {
listen 80;
server_name domain2.com;
return 301 https://$server_name$request_uri;
}



server {

    listen 443 ssl;
    server_name domain2.com;
    root /app/dist;
    index index.html;
    ssl_certificate /etc/nginx/ssl/d2/certificate.crt;
    ssl_certificate_key /etc/nginx/ssl/d2/private.key;

    location / {    
        try_files $uri $uri/ /index.html;
    }
}
Run Code Online (Sandbox Code Playgroud)

上述配置将domain1和domain2的HTTP(80)转发到各自的HTTPS(443)服务器块,其中加载各自的证书密钥对。
直接处理 HTTPS (443) 请求。
nginx 通过使用 SNI 选择服务器名称来决定要命中哪个块。