jeudi 27 mai 2021

Redirect https://www domain to https://non-www domain in Nginx

I have around 50 domains on the Nginx server for which an SSL certificate(non-www SSL certificate) is issued.

Now, When we try to open a site like https://www[Domain], there is a 'common name' error as the certificate is not installed for www.

I want to redirect any https://www[domain] to https://[domain], I tried several solutions available on blogs, but it seems the below code only works when SSL is installed on a domain as I reissued www certificate for one of the domain then I can redirect using below code block. but my manager doesn't want to reissue certificates for all domains with www

server {
    listen 443 ssl;
    server_name www.[domain];    
    return 301 $scheme://[domain]$request_uri;
}

I also tried some regex with Server block but it also doesn't work

server { 
    server_name "www.(.+?\.\w+)" ; 
    return 301 https://$1$request_uri; 
}

How I can make this work?




Aucun commentaire:

Enregistrer un commentaire