将Nginx HTTP/HTTPS www重定向到非www

问题描述:

我刚刚购买了通配符SSL证书,该证书允许我访问动态子域。我可以访问以下罚款域与我的配置:将Nginx HTTP/HTTPS www重定向到非www

https://test.example.co/

https://example.co/

http://example.co/去 - >https://example.co/

所以我强迫所有HTTP到HTTPS和去除www

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

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

NET::ERR_CERT_COMMON_NAME_INVALID 

我的配置:

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

,当然还有我所有的域上述工作像他们现在这样。

+0

进出口寻找这也是。你可以设置nginx拒绝来自'http:// *.com'的访问(仅举例),只允许访问'http:// www.*.com'?如果是这样,请教我。谢谢 –

+0

通配符SSL证书是一个级别:'* .example.com'包含“www.example.com”和“user1.example.com”,但不包含“www.user1.example.com”。请参阅https://*.com/questions/32510641/wildcard-certificate-does-not-work-for-sub-domain和其他链接(模糊)。 –

+0

@ dave_thompson_085有没有办法强制它避免www.user1.example?还是我需要另一份证书?或者也许改变我的?目前,它的设置为* .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 Multilevel Subdomain Wildcard