Cloudflare: Too Many Redirects Error when Proxying

May 16, 2024

TL;DR: this happens when Cloudflare sends unencrypted HTTP proxy requests to the server and the server redirects them to HTTPS. To fix this, set the SSL/TLS encryption mode to Full or Full (Strict) in the Cloudflare dashboard.

Cloudflare is a great service that can help you protect your website from DDoS attacks, speed up your website, and more. It is one of my favorite services not only for its security features but also for its speed and reliability.

Majority of my projects are either deployed on Cloudflare or have Cloudflare in front of them. One of the projects is time.fyi which provides several time-related tools and services. The project has two parts:

  • A static website that is hosted on Cloudflare Pages
  • A Node.js API that is hosted on Railway

API calls are proxied through Cloudflare to the API server. To setup the proxy, I mapped a CNAME record from Cloudflare to the Railway server. The setup looks simple enough, however I started getting the Too Many Redirects error when trying to access the API.

The reason for this is that Cloudflare, by default, when proxying requests sends unencrypted HTTP requests to the server. Railway on the other hand is configured to redirect any HTTP requests to HTTPS. This creates a loop where Cloudflare sends an HTTP request, Railway redirects it to HTTPS, Cloudflare sends another HTTP request, and so on. This is a well-known issue and is documented in the Cloudflare documentation as well.

To fix this issue, you need to visit the SSL/TLS settings page in the Cloudflare dashboard and set the SSL/TLS encryption mode to Full or Full (Strict). This will ensure that Cloudflare sends encrypted HTTPS requests to the server and the server does not redirect the requests to HTTPS.

Cloudflare SSL/TLS settings

After making this change, the Too Many Redirects error should be resolved and you should be able to access your website without any issues. I hope this helps you fix the issue and get your website up and running.