mydomain.com redirected you too many times. ERR_TOO_MANY_REDIRECTS

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 ; # 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;
}

is working fine.

I try to Obtain an SSL Certificate by

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

the result was

Your existing certificate has been successfully renewed, and the new certificate
has been installed.
The new certificate covers the following domains: and

mydomain.conf file after obtaining an SSL Certificate

server { server_name mydomain.com ; # Tell Nginx and Passenger where your app's 'public' directory is root /var/www/backup/ # 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/ # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/ # 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 = ) { return 301 } # managed by Certbot if ($host = mydomain.com) { return 301 } # managed by Certbot server_name mydomain.com ; listen 80; return 404; # managed by Certbot
}

is redirecting to 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 = ) { return 301
    } # managed by Certbot
    if ($host = mydomain.com) { return 301
    } # managed by Certbot
    server_name mydomain.com ;
    listen 80;
    return 404; # managed by Certbot }
    1. How to make all redirects to ?

1 Answer

This block is causing the issue, like you suspected:

server { if ($host = ) { return 301 } # managed by Certbot if ($host = mydomain.com) { return 301 } # managed by Certbot server_name 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 return 301
}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like