I followed this answer https://stackoverflow.com/a/28068250/3108268 but it redirects only from http to https and non www to www, but if I go to my website at https://example.com I get 'your connection is insecure'.
How do I redirect it to https://www?
server{
listen 443 ssl;
server_name www.mydomain.com;
root /www/mydomain.com/;
ssl on;
ssl_certificate /ssl/domain.crt;
ssl_certificate /ssl/domain.key;
.
.
.
}
server{
listen 80;
server_name www.mydomain.com mydomain.com;
return 301 https://$server_name$request_uri;
}
server{
listen 443;
server_name mydomain.com;
return 301 https://www.$server_name$request_uri;
}
the third server is missing SSL certificates which is why the browser is saying the connection is insecure.
replace your last two servers with:
# redirect www.mydomain.com to https
server {
listen 80;
server_name www.mydomain.com;
return 301 https://$server_name$request_uri;
}
# redirect mydomain.com to https
server{
listen 80;
server_name mydomain.com;
return 301 https://www.$server_name$request_uri;
}
A good way to get the correct configuration is using new blocks for each redirect, one from http to https and one to non-www to www.
server {
listen 80;
server_name www.example.com example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
ssl_certificate /path/to/certificate.crt;
ssl_certificate_key /path/to/private/key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
ssl_certificate /path/to/certificate.crt;
ssl_certificate_key /path/to/private/key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
server_name example.com;
# do the proper handling of the request
}