使用HTTPS将所有子域redirect到主域,而不使用通配符证书(nginx)

我有一个主站点和几个虚拟主机的域名。 我希望所有网站(主要和虚拟主机)只能通过HTTPS(使用HSTS)访问,我希望不存在的子域redirect到主站点。

总结一下我想要的:

  • https:/example.com:无redirect(主站点)
  • http:/example.com:redirect到https:/example.com
  • https:/vhost.example.com:无redirect(虚拟主机)
  • http:/vhost.example.com:redirect到https:/vhost.example.com
  • https:/doesntexist.example.com:redirect到https:/example.com
  • http:/doesntexist.example.com:redirect到https:/example.com

我非常接近这一点,但倒数第二次redirect不起作用。 我正在使用来自Let's Encrypt的证书,此证书目前不提供通配证书,因此证书仅对主域和虚拟主机(我在创build证书时明确列出)有效。 由于证书无效,浏览器会阻止连接。

如何避免这个问题,最好的办法是什么?

这是我的(简化)nginxconfiguration,如果有帮助:

server { listen 80; server_name _; return 301 https://example.com$request_uri; } server { listen 443 spdy; server_name _; # SSL settings snipped for brevity. SPDY and HSTS are enabled. return 301 https://example.com$request_uri; } server { listen 443 spdy; server_name example.com; root /var/www/example.com; index index.html index.htm; } server { listen 443 spdy; server_name vhost.example.com; root /var/www/vhost.example.com; index index.html index.htm; } 

实际上,如果没有通配证书,就无法解决问题。 如果您不能提供与所请求的名称相匹配的证书,将会出现连接错误 – 这是协议的基本部分。

我想在理论上,你可以写一些东西,当它接收到一个不在证书中的名字的SNI头部时,从Let's Encrypt中做了一个快速的证书颁发,并且在TLS握手中发回了这个新的证书。好吧,这不是一个实际的解决scheme,是吗?