I have the following code:server {listen 443;server_name www.domain.com;ssl on;ssl_certificate    /etc/nginx/ssl/domain.com.crt;ssl_certificate_key    /etc/nginx/ssl/domain.com.key;return 301 https://domain.com$request_uri;}If I access https://domain.com everything works, if I access https://www.domain.com I get a SSL warning that the Certificate does not match the domain, which it doesn't. Once you accept the invalid SSL cert it redirects you to https://domain.com with the valid cert.But if I remove the ssl on; bit etc, then neither https://domain.com or https://www.domain.com work.So how can I remove the WWW without the SSL Invalid warning?I only have an SSL cert for domain.com and not for www.domain.com.
Remove www from HTTPS request in Nginx
https;nginx
HTTPS is HTTP over in TLS/SSL connection. Before any HTTP transaction, the client must be verify server certificate through SSL-Handshake ritual. Your redirection, which is HTTP transaction, will be start after SSL-Handshake.If you want https://www.domain.com redirect to https://domain.com without SSL Invalid warning, your certificate must be valid to both domain.com and www.domain.com.The simplest way is ask your CA to append www.domain.com to subjectAltName field in your certificate, so you can serve multiple domain with one certificate. Some CAs will do it without cost. The other options is obtain new certificate for www.domain.com although it's pretty overkill.