I am setting up Let's Encrypt's ssl for the first time with nginx. I used the DigitalOcean guide that i linked below, and used Certbot, to complete the installation.
I used the following guide:
How To Secure Nginx with Let's Encrypt on Ubuntu 16.04 | DigitalOcean
After completing my setup I was left with the following configuration:
server {
root /var/www/example.com/html; index index.html index.htm index.nginx-debian.html; server_name example.com www.example.com; location / { try_files $uri $uri/ =404; } listen [::]:443 ssl ipv6only=on; # managed by Certbot listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbotif ($host = example.com) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; listen [::]:80; server_name example.com www.example.com; return 404; # managed by Certbot
}
After some troubleshooting it seemed that I was getting too many redirects from the following portion of the configuration:
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbotif ($host = example.com) { return 301 https://$host$request_uri; } # managed by Certbot
After removing this portion of the code, I was able to get ssl fully working, however, http would not redirect to https.
I then replaced the offending portion of the code with some of the following alternative work around and still was unable to get it working:
return 301 https://example.com$request_uri;
if ($scheme != "https") {
return 301 "https://$host$request_uri";
}
if ($scheme != "https") {
return 301 "https://example.com$request_uri";
}
Am i missing something? How do i get it to redirect from http to https properly?