Question

SSL set up gives "redirected you too many times."

Hi,

I am trying to set up SSL with this tutorial - https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04#step-1-install-let’s-encrypt-client

Everything went okay but when I try to go to my subdomain (only this should be encrypted), I am getting following:

The work-bot.be-codified.com page isn’t working

work-bot.be-codified.com redirected you too many times.

Here is my /etc/nginx/sites-available/default file

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name work-bot.be-codified.com;
        return 301 https://$server_name$request_uri;

        # SSL configuration
        #
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        include snippets/ssl-work-bot.be-codified.com.conf;
        include snippets/ssl-params.conf;

Can somebody please tell me what is wrong?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Because you haven’t used a second server block to house the SSL site’s configuration, the return 301 https://$server_name$request_uri; line redirects you to the SSL version of your site regardless of the scheme.

To rectify this, you can split the configuration into 2 server blocks, one for plaintext and another for SSL.

server { # Non-SSL configuration
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name work-bot.be-codified.com;

    return 301 https://$server_name$request_uri;
}

server { # SSL configuration
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    server_name work-bot.be-codified.com;

    include snippets/ssl-work-bot.be-codified.com.conf;
    include snippets/ssl-params.conf;
}

(NOT RECCOMENDED) OR you can put the non-SSL specific configuration inside and if block, and that would make it apply only over http (or vice versa). Note that if blocks are generally not a good idea to use for reasons outlined in https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name work-bot.be-codified.com;

    if ($scheme = "http") { # Only redirect if not using SSL
        return 301 https://$server_name$request_uri;
    }

    # SSL configuration
    #
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    include snippets/ssl-work-bot.be-codified.com.conf;
    include snippets/ssl-params.conf;
}

Thanks! it’s ok

@UKn0Me thank you, fully working now.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.