I am pretty new to nginx and have tried fixing the issue with a number of different solutions on this site and elsewhere but still can not resolve the non-www to www. The odd thing is that another server block that I have is doing it without issue. I used letsencrypt for the SSL cert. My config file is:
server {
location / {
root /var/www/example.com;
try_files $uri /index.html;
}
server_name example.com www.example.com;
listen [::]:443 ssl;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
server_name example.com www.example.com;
if ($host = example.com) {
return 301 https://$host$request_uri;
}
if ($host = www.example.com) {
return 301 https://$host$request_uri;
}
listen 80;
listen [::]:80;
return 404;
}
I have tried substituting the https://$host$request_uri; where $host = example.com for https://www.example.com$request_uri; and that doesn't work either. In the network tab I am getting a status of '(failed) net::ERR_NAME_NOT_RESOLVED', but my config is the same as another server block and that one is resolving correctly with both www and non-www to https.