#StackBounty: #14.04 #16.04 #nginx #ssl #letsencrypt mydomain.com redirected you too many times. ERR_TOO_MANY_REDIRECTS

Bounty: 50

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/ ?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.