I get a redirect loop on my Rails 4 app when enforcing ssl with the force_ssl
method (SSL implemented using CloudFlare's one-click SSL capabilities).
Too Many Redirects If you're receiving the ERR_TOO_MANY_REDIRECTS error when accessing your HTTPS enabled site that is being proxied through Cloudflare, it's likely that you have SSL set to Flexible. This should be set to Full (strict), which will ensure connections to your site are fully encrypted end-to-end.
To enable or disable Always Use HTTPS with the API, send a PATCH Open external link request with the value parameter set to your desired setting ( "on" or "off" ).
This stopped happening when I switched from "flexible" to the "full" SSL setting in CloudFlare.
Cloudflare's trying to force connect via HTTP because your server doesn't have HTTPS, but your app is forcing SSL.
This happens in a specific set of circumstances:
Flexible
and HTTP is allowed between CF and Heroku.What happens is:
HTTPS
This causes an infinite redirect loop.
Upgrade your application server to HTTPS, this solves the redirect issue. Then, just to be safe, switch Cloudflare to Full
.
First: I configured SSL on my server. In my case, I was using Heroku, which can use Let's Encrypt to provision SSL automatically. You would use Let's Encrypt directly if you weren't. Cloudflare also provides self-signed origin certificates, but why bother when Let's Encrypt is easy and free.
Second: I configured my app to force SSL by adding the following to production.rb
config.force_ssl = true
Third: Since I no longer needed an HTTP connection between my server and Cloudflare, I switched it to from Flexible
to Full
. If you've used Let's Encrypt or a CA to provision the server's certificate, you can switch to Flexible (Strict)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With