Nginx单主机单IP绑定的多域名对应多HTTPS证书

需求

在一个阿里云ECS下面要架设两个网站,拥有不同的域名,并且都是HTTPS。但是该ECS只有一个IP,所以需要为指向同一个IP的两个域名设置两个不同的HTTPS证书

困境

在没有SNI支持的情况下,一个IP绑定多个域名时。是无法为各个域名配置不同的证书的,具体原因见https://nginx.org/en/docs/http/configuring_https_servers.html

The SSL connection is established before the browser sends an HTTP request and nginx does not know the name of the requested server. Therefore, it may only offer the default server’s certificate.

解决

1. 所以首先要查看自己的Nginx是否支持SNI。由于Nginx在Docker中运行,故先进入Nginx容器命令行来查看SNI支持情况:

docker exec -it my-nginx /bin/sh
nginx -V

得到输出

nginx version: nginx/1.17.1
built by gcc 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)
built with OpenSSL 1.1.0j  20 Nov 2018
TLS SNI support enabled

可见支持SNI

2. 下一步就是申请证书,得到key和pem文件,放入Nginx的Docker构建目录中的cert文件夹中,然后编辑Dockerfile文件,通过COPY命令在构建Nginx镜像的时候复制整个cert文件夹到镜像文件系统中的/etc/nginx/cert/

如下为完整的Nginx的Dockerfile

FROM nginx:latest

COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./cert/ /etc/nginx/cert/

EXPOSE 80
EXPOSE 443

3. 在Nginx配置文件中添加监听443端口的server,并指定ssl_certificate和ssl_certificate_key为pem文件和key文件

如下为完整的监听443端口的server

server {
    listen       443 ssl;
    server_name  zongheng.pro www.zongheng.pro;

    ssl_certificate      cert/2463914_www.zongheng.pro_nginx/2463914_www.zongheng.pro.pem;
    ssl_certificate_key  cert/2463914_www.zongheng.pro_nginx/2463914_www.zongheng.pro.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        root /www/zongheng_pro/;
        index  index_zh.html;
    }
}

4. 添加对80端口的http访问的监听,并跳转到443端口的https

如下为完整的监听80端口的server

server {
    listen       80;
    server_name  zongheng.pro www.zongheng.pro;
    return 301 https://www.zongheng.pro;
}

5. 重新构建Nginx镜像,启动Nginx容器,测试通过http和https访问域名,发现成功,查看证书,也正确

参考文献

https://en.wikipedia.org/wiki/Server_Name_Indication

https://nginx.org/en/docs/http/configuring_https_servers.html

http://blog.chinaunix.net/uid-20639775-id-3213595.html

Share

You may also like...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注