Pritam Bohra
Pritam Bohra

Reputation: 4319

ERR_TOO_MANY_REDIRECTS with nginx

I want to redirect all my http traffic to redirect to https. I am using letsencrypt. I read online that return 301 https://$server_name$request_uri; would redirect all the traffic to my website over to https but instead it results in ERR_TOO_MANY_REDIRECTS.

Everything works fine without the above mention statement, but then I have to specifically specify https in the URL. Here's my /etc/nginx/sites-available/default file:

server {
        listen 80 default_server;
        listen 443 ssl default_server;

        ssl_certificate /etc/letsencrypt/live/mywebsite.me/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/mywebsite.me/privkey.pem;

        root /home/website/mywebsite/public;

        index index.html index.htm index.php;

        server_name mywebsite.me www.mywebsite.me;

        return 301 https://$server_name$request_uri;

        location / {
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        }
}

Where am I going wrong?

Upvotes: 29

Views: 72928

Answers (4)

Vit
Vit

Reputation: 488

In addition to @ZyQux: Have domain on Cloudflare, which redirecting to another VPS provider, where Certbot. There was loop redirect. To fix it, I have had to switch off proxy (to "DNS only") in DNS management of domain:

enter image description here

Thus thanks to @OritK because after fixing it on Cloudflare even incognito Chrome was showing error on my website. So yes, check it in FireFox

Upvotes: 0

ZyQux
ZyQux

Reputation: 1391

In my case it was Cloudflare. I had to change to Full SSL encryption

Upvotes: 109

OritK
OritK

Reputation: 544

i had the same problem. i found lots of questions/answers and articles about it but nothing helped. i then tried to access my site from a different browser and it worked just fine! deleting the cache from my chrome browser solved it.

So - keep in mind when you try different solutions to also clear your cache

Upvotes: 6

Tarun Lalwani
Tarun Lalwani

Reputation: 146490

Change your config to below

server {
        listen 80 default_server;
        server_name mywebsite.me www.mywebsite.me;

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

server {

        listen 443 ssl default_server;

        ssl_certificate /etc/letsencrypt/live/mywebsite.me/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/mywebsite.me/privkey.pem;

        root /home/website/mywebsite/public;

        index index.html index.htm index.php;

        server_name mywebsite.me www.mywebsite.me;

        location / {
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        }
}

Your current config redirects on both http and https to https. So it becomes a infinite loop because of the return statement. You want return statement only when connection is http. So you split it into two server blocks

Upvotes: 32

Related Questions