Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 304
Pritam Bohra Avatar asked Sep 25 '17 20:09

Pritam Bohra


People also ask

What does Err_too_many_redirects mean?

What Does "Too Many Redirects" Mean? The ERR_TOO_MANY_REDIRECTS error indicates that the browser is stuck in an infinite redirection loop. An infinite redirection loop happens when you visit a URL pointing to another URL, which points back to the first one.

How many redirects is too many?

There are no limits in using 301 redirects on a site. You can implement more than 100k of 301 redirects without getting any penalty. But: Too many 301 redirects put unnecessary load on the server and reduce speed. Try to reduce direct redirects by using rules.


2 Answers

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

like image 159
ZyQux Avatar answered Oct 12 '22 20:10

ZyQux


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

like image 44
Tarun Lalwani Avatar answered Oct 12 '22 19:10

Tarun Lalwani