DEV Community

Attila Albert
Attila Albert

Posted on

AI coding tools and ERR_TOO_MANY_REDIRECTS: fix the HTTPS redirect loop behind a proxy

By Tomás Herrera, NBT

Your app works on localhost. You deploy it behind a load balancer, Cloudflare, nginx or a platform like Render or Fly, and the browser says:

This page isn't working
example.com redirected you too many times.
ERR_TOO_MANY_REDIRECTS
Enter fullscreen mode Exit fullscreen mode

A very common cause is a line that AI coding tools add when you ask them to "force HTTPS": the app checks whether the request is secure and, if not, redirects to https://. Behind a proxy, the proxy talks to your app over plain HTTP, so the app never sees HTTPS, keeps redirecting, and the browser gives up.

This guide shows how to confirm the loop, fix it by trusting the proxy correctly, and avoid the security hole that a careless fix creates.

Step 1: Watch the loop with curl

Browsers hide the details. curl shows each hop:

curl -sI -L --max-redirs 5 https://example.com/ | grep -iE '^(HTTP|location)'
Enter fullscreen mode Exit fullscreen mode

A loop looks like this:

HTTP/2 301
location: https://example.com/
HTTP/2 301
location: https://example.com/
...
curl: (47) Maximum (5) redirects followed
Enter fullscreen mode Exit fullscreen mode

The same URL redirecting to itself means the server thinks the request isn't secure even though you asked over HTTPS. If instead you see https://example.com/ → https://www.example.com/ → https://example.com/, it's a www/non-www conflict between two layers. Different cause, same symptom.

Step 2: Understand what the app sees

The chain is usually:

Browser --HTTPS--> Proxy / load balancer --HTTP--> Your app
Enter fullscreen mode Exit fullscreen mode

The proxy ends the TLS connection. To tell the app what the original request looked like, it adds headers:

Header Meaning
X-Forwarded-Proto https if the browser used HTTPS
X-Forwarded-For the client's IP (and any proxies)
X-Forwarded-Host the host the browser asked for

Your framework ignores these by default, which is correct. Anyone can send them, so trusting them blindly would let a client pretend to be on HTTPS or spoof its IP. You have to tell the framework which proxy to trust.

Step 3: Trust the proxy in your framework

Express

app.set('trust proxy', 1);  // trust the first hop (your load balancer)

app.use((req, res, next) => {
  if (req.secure) return next();
  res.redirect(308, `https://${req.headers.host}${req.originalUrl}`);
});
Enter fullscreen mode Exit fullscreen mode

With trust proxy set, req.secure and req.protocol read X-Forwarded-Proto from the trusted hop. A number means "trust this many hops", and you can also give the proxy's IP ranges instead, which is stricter. Express's docs warn that the last trusted proxy must strip or overwrite the forwarded headers clients send, otherwise clients can set them.

Django

SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
SECURE_SSL_REDIRECT = True
Enter fullscreen mode Exit fullscreen mode

Only set this if your proxy always sets or strips that header.

Flask (Werkzeug)

from werkzeug.middleware.proxy_fix import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1)
Enter fullscreen mode Exit fullscreen mode

Rails: config.force_ssl = true and config.assume_ssl = true when a proxy terminates TLS.

Laravel: configure trusted proxies (TrustProxies middleware or trustProxies in bootstrap/app.php).

The number of hops matters. If traffic goes Cloudflare → load balancer → app, that's two proxies. Trust exactly what's in front of you, no more.

Step 4: Check the proxy actually sends the header

If the app still loops, the header may be missing. For nginx in front of your app:

location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Enter fullscreen mode Exit fullscreen mode

Managed load balancers (AWS ALB, Google Cloud, most platforms) add X-Forwarded-Proto automatically. To be sure, add a temporary debug route that returns the request headers, check it, then delete it.

Step 5: Fix the Cloudflare "Flexible" variant

With Cloudflare's SSL mode set to Flexible, Cloudflare talks to your server over HTTP. If your server also redirects HTTP to HTTPS (in nginx, Apache or the app), you get a loop: Cloudflare asks over HTTP, your server says "go to HTTPS", the browser comes back through Cloudflare, which asks over HTTP again.

The fix is to give your origin a certificate (Let's Encrypt or a Cloudflare origin certificate) and set SSL mode to Full (strict). Then Cloudflare connects over HTTPS and the server's redirect is never triggered for proxied requests.

Step 6: Redirect in one place only

Loops also come from two layers each doing their own redirect: the platform forces HTTPS, nginx forces www, and the app forces non-www. Pick one layer for each rule:

  • HTTPS: at the edge (load balancer or CDN) if it can, otherwise in the app.
  • www vs non-www: one rule, one layer.

Search your stack for all of them before changing anything: the app's middleware, the nginx or Apache config, .htaccess, the platform's settings and the CDN's page rules.

Step 7: Give your AI tool the full picture

When asking for help, describe the deployment, not just the error:

"In production, the app runs behind [Cloudflare in Full strict mode → AWS ALB → app on port 3000]. We get ERR_TOO_MANY_REDIRECTS. Show me every place that redirects (app code, configs, platform settings in the repo). Propose one fix that trusts only the ALB. Don't trust all proxies and don't remove the HTTPS redirect."

The last two sentences matter. "Trust all proxies" (trust proxy: true with no limit) and "just remove the redirect" both stop the loop and both create new problems: spoofable client IPs in your rate limiting and logs, or a site that serves plain HTTP. When the answer comes back, check the diff for those two. If you want a sturdier way to write these requests, this guide on prompting AI coding tools with the right context goes deeper.

Step 8: Test after the fix

curl -sI http://example.com/ | grep -iE '^(HTTP|location)'    # one redirect to https
curl -sI https://example.com/ | grep -iE '^HTTP'               # 200, no redirect
Enter fullscreen mode Exit fullscreen mode

Also check that your logs now show real client IPs, not the proxy's address. That tells you the trust setting matches your actual chain.

Checklist

  • Use curl to see the hops, not the browser.
  • Trust the exact number of proxies (or their IPs) in front of the app.
  • Make sure the proxy sends X-Forwarded-Proto.
  • On Cloudflare, use Full (strict), not Flexible.
  • One layer per redirect rule.
  • Never fix it by trusting everything or dropping HTTPS.

Top comments (0)