将Nginx HTTP / HTTPS wwwredirect到非www

我刚刚购买了通配符SSL证书,允许我访问dynamic子域。 我可以使用我的configuration访问以下域名:

https://test.example.co/

https://example.co/

http://example.co/转到 – > https://example.co/

所以我强制所有HTTP到HTTPS并删除www

我的问题是,我有dynamic的子域,允许用户有他们想要的任何子域( https://user1.example.co,https://user2.example.co,https://user3.example。 co )。

我的问题是当用户访问http://www.user1.example.co/或https://www.user1.example.co/我得到以下内容:

 NET::ERR_CERT_COMMON_NAME_INVALID 

我的configuration:

 server { server_name www.example.co; return 301 $scheme://example.co$request_uri; } server { listen 443; ssl on; ssl_certificate /etc/nginx/blah; ssl_certificate_key /etc/nginx/blah; server_name example.co *.example.co; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; location / { proxy_pass http://127.0.0.1:3000; proxy_set_header X_FORWARDED_PROTO https; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Url-Scheme $scheme; proxy_redirect off; proxy_max_temp_file_size 0; } } 

我已经删除了证书和里面的逻辑,但我的目标是有任何www. 除去。 所以它会这样:

http://www.user1.example.com – > https://user1.example.com http://www.user2.example.com – > https://user2.example.com

当然,我所有的领域都像现在这样工作。

只有在手头有全部子域名的情况下才有可能。

您可以在同一个证书中拥有多个带有通配符的子域。 所以你需要一个包含你要使用的所有子域的证书

 *.example.com *.user1.example.com *.user2.example.com *.user3.example.com *.user4.example.com 

这意味着你不能动态地将新的子域添加到列表中,因为它需要重新生成证书。

名称中的通配符只反映单个标签,通配符只能放在最左边。 因此没有 .example.org,www。*。example.org是可能的。 而* .example.org既不会匹配example.org,也不会匹配www.subdomain.example.org,只会匹配subdomain.example.org。

但是你可以在同一个证书中有多个通配符名字,也就是说你可以在同一个证书中有* .example.org和* .subdomain.example.org

SSL多级子域通配符