如何使用nginx使用自己的证书设置多个子域?

除非我读过的每一个答案都是错误的,否则SNI应该可以做我想做的事情,然而每个指南都告诉我要做我正在做的事情。

然而,nginx正在提供错误的证书,所以我显然做错了什么。

❯ sudo nginx -V | grep SNI %1 nginx version: nginx/1.10.3 built with OpenSSL 1.1.0f 25 May 2017 TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-qJwWoo/nginx-1.10.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/ngi nx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fa stcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_reques t_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --wit h-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --add-dynamic-module=/build/nginx-qJwWoo/nginx-1.10.3/debian/modules/nginx-auth-pam --add-dynamic-module=/build/nginx-qJwWoo/nginx-1.10.3/debian/modules/nginx-dav- ext-module --add-dynamic-module=/build/nginx-qJwWoo/nginx-1.10.3/debian/modules/nginx-echo --add-dynamic-module=/build/nginx-qJwWoo/nginx-1.10.3/debian/modules/nginx-upstream-fair --add-dynamic-module=/build/nginx-qJwWoo/nginx-1.10.3/debian/modules/ngx_http_substitutions_filter_m odule 

这是我的configuration看起来像:

 server { listen 443 ssl default_server; listen [::]:443 ssl; server_name one.example.com; ssl on; ssl_certificate /etc/letsencrypt/live/one.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/one.example.com/privkey.pem; index index.html; root /var/www/one.example.com/site; } server { #listen 443 ssl default_server; listen [::]:443 ssl; server_name two.example.com; ssl on; ssl_certificate /etc/letsencrypt/live/two.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/two.example.com/privkey.pem; index index.html; root /var/www/two.example.com/site; } 

如果我有listen 443 ssl default_server; 指令在任何一台服务器上都会返回该服务器的SSL证书。 如果我从两个域中删除它,那么我什么也得不到 – 两个服务器域都拒绝连接。

我在这里出了什么问题? 我不明白SNI是如何工作的吗? 我的nginx已经启用了SNI支持。 然而,我只获得一个子域的SSL证书。

 listen 443 ssl default_server; listen [::]:443 ssl; 

第一行允许在IPv4上监听端口443。 第二行只包括IPv6。 由于您只有一个listen 443 (IPv4)configuration,因此如果您使用IPv4连接,则会使用该configuration。 如果你想连接IPv6而不是SNI应该显示预期的行为。

相反,你可能会使用默认服务器:

  listen 443 ssl default_server; listen [::]:443 ssl default_server; 

而对于其他服务器

  listen 443 ssl; listen [::]:443 ssl; 

它显然与IPv6 listen语法有关。 当我改变

 listen [::]:443 ssl; 

 listen 443 ssl; 

然后它工作。

我不知道这是为什么,并会更多/更好的解释欢迎其他答案。