29

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?

4 Answers 4

113

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

Sign up to request clarification or add additional context in comments.

12 Comments

You are a hero.
As soon as i read CloudFlare i was like "omg i forgot about that completely". Thank you for reminding me!
Yes, I use Cloudflare but still, when you have multiple server blocks it won't force redr to HTTPS is still allows HTTP
Give this guy a medal, please! :D
Would anyone please explain why Cloudflare full SSL would do the job?
|
32

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

Comments

6

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.