3

I was trying to Secure Nginx with Let's Encrypt on Ubuntu 16.04.

mydomain.conf file before obtaining an SSL Certificate

server {
    server_name mydomain.com www.mydomian.com ;
    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/backup/mycode/public;
    # Turn on Passenger
    passenger_enabled on;
    rails_env development;
    passenger_ruby /usr/local/rvm/gems/ruby-2.5.6/wrappers/ruby;

}

http://mydomain.com/ is working fine.

I try to Obtain an SSL Certificate by

sudo certbot --nginx -d mydomain.com -d www.mydomain.com

the result was

Your existing certificate has been successfully renewed, and the new certificate
has been installed.

The new certificate covers the following domains: https://mydomain.com and
https://www.mydomain.com

mydomain.conf file after obtaining an SSL Certificate

server {
    server_name mydomain.com www.mydomain.com ;
    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/backup/mydomain.com/public;
    # Turn on Passenger
    passenger_enabled on;
    rails_env development;
    passenger_ruby /usr/local/rvm/gems/ruby-2.5.6/wrappers/ruby;




    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mydomain.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.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name mydomain.com www.mydomain.com ;
    listen 80;
    return 404; # managed by Certbot

}

http://mydomain.com/ is redirecting to https://mydomain.com/ too many times

mydomain.com redirected you too many times.
ERR_TOO_MANY_REDIRECTS
  1. Why is it redirecting too many times?

  2. what is the purpose of the second server block?

    server {
    if ($host = www.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    
    
    if ($host = mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    
    
    server_name mydomain.com www.mydomain.com ;
    listen 80;
    return 404; # managed by Certbot
    
     }
    
    1. How to make all redirects to https://www.mydomain.com/ ?

1 Answers1

1

This block is causing the issue, like you suspected:

server {
    if ($host = www.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
if ($host = mydomain.com) {
    return 301 https://$host$request_uri;
} # managed by Certbot


server_name mydomain.com www.mydomain.com ;
listen 80;
return 404; # managed by Certbot

}

This server block redirects the user to https. However, it redirects https to https as well, which is causing the issue. You can change it to:

server {
    listen 80;
    server_name mydomain.com www.mydomain.com;
    return 301 https://mydomain.com$request_uri;
}