Fix SSL ERR_TOO_MANY_REDIRECTS on Nginx Behind a CDN

by Fahim Mahmud Chisti

ERR_TOO_MANY_REDIRECTS right after putting a CDN in front of Nginx almost always means the CDN fetches your site over plain HTTP while Nginx (or WordPress) keeps redirecting every HTTP request to HTTPS. The browser bounces between the two until it gives up. Here is how to find which layer loops and fix ERR_TOO_MANY_REDIRECTS on Nginx behind a CDN, often without touching the server.

Quick answer: The usual cause is a CDN SSL mode such as Cloudflare’s “Flexible”, which connects to your origin over HTTP while Nginx forces HTTPS. If Nginx already has a valid certificate, switching the CDN to Full (strict) stops the loop with no server change.

  1. Run curl -IL https://example.com and see which redirect repeats.
  2. If Nginx has a valid certificate, set the CDN to connect over HTTPS (Cloudflare: Full or Full (strict)).
  3. If the origin has no certificate yet, make Nginx redirect only when X-Forwarded-Proto is http.
  4. On WordPress or PHP, tell PHP the visitor used HTTPS.
  5. Run sudo nginx -t before reloading, then purge the CDN cache and retest in a private window.
In this guide
  1. Before You Start
  2. How the CDN Redirect Loop Happens
  3. Step 1: Trace the Redirect Chain with cURL
  4. Step 2 (Fastest Fix): Make the CDN Connect to Nginx over HTTPS
  5. Step 3 (If the CDN Must Use HTTP): Redirect Only Real HTTP Visitors
  6. Step 4: Fix HTTPS Detection in WordPress and PHP
  7. Step 5: Test and Reload Nginx
  8. Step 6: Purge CDN Cache and Verify
  9. If It Still Loops: Common Causes
  10. Frequently Asked Questions
  11. Next Steps
Fix SSL ERR_TOO_MANY_REDIRECTS on Nginx Behind a CDN

Before You Start

  • Access: a login to your CDN dashboard (its SSL/TLS or origin settings), SSH with sudo to the Nginx server, and the origin server’s public IP.
  • Time: about 5 minutes if only the CDN setting is wrong; 15–30 minutes if you also change Nginx or WordPress.
  • Back up first: before editing, copy the site file, for example sudo cp /etc/nginx/sites-available/example.com /root/example.com.bak, so you can restore it and reload if something breaks.
  • Warning: switch the CDN to Full (strict) only when Nginx serves a valid certificate on port 443. Otherwise the CDN shows its own SSL error instead of your site (on Cloudflare, a 52x error such as 525 or 526).

How the CDN Redirect Loop Happens

Here is the path a request takes when the loop happens:

  1. Visitor → CDN: The user connects over HTTPS on port 443.
  2. CDN → Nginx Origin: If the CDN is set to “Flexible” mode or configured with an HTTP origin protocol, it connects to your server on plain port 80.
  3. Nginx Catch-All: Your Nginx config sees traffic on port 80 and runs an unconditional redirect: return 301 https://$host$request_uri;.
  4. The Infinite Loop: Nginx returns that 301 to the CDN. The CDN sends it to the browser. The browser requests HTTPS again, the CDN hits port 80 on Nginx again, and you’re stuck in a loop.

Even without a redirect in Nginx, apps such as WordPress, Node.js or Laravel send their own HTTPS redirects when they can’t tell that the visitor already used HTTPS. So the fix is at the CDN (Step 2) or, if the CDN must keep using HTTP, in how Nginx and the app read the forwarded protocol (Steps 3 and 4).

Step 1: Trace the Redirect Chain with cURL

Before touching config files, look at the redirect chain with curl, first through the CDN and then straight at your origin IP. This shows whether the redirect comes from the CDN, Nginx or the app behind it.

Run this from your own computer’s terminal:

curl -IL https://example.com

If you see the same 301 or 302 response repeated until curl gives up with curl: (47) Maximum (50) redirects followed, you’re looping. Now bypass the CDN completely and hit your origin IP directly:

curl -I -H "Host: example.com" http://YOUR_ORIGIN_IP/

If port 80 on your origin answers with a 301 Moved Permanently to https://example.com/, the origin forces HTTPS without checking whether the CDN already handled HTTPS for the visitor. That is the loop. To see whether the origin can serve HTTPS itself, run curl -I --resolve example.com:443:YOUR_ORIGIN_IP https://example.com/. It connects straight to your server and checks its certificate. A normal response with no certificate error means Step 2 will work.

Step 2 (Fastest Fix): Make the CDN Connect to Nginx over HTTPS

The most common cause of this error is a CDN that accepts HTTPS from the visitor but talks plain HTTP to your server while your server forces HTTPS. Cloudflare calls this the “Flexible” encryption mode; other CDNs offer the same choice as an origin protocol setting (HTTP only, HTTPS only, or match the visitor).

Fix it in the CDN dashboard by making the CDN connect to your origin over HTTPS. In Cloudflare’s terms, switch the SSL/TLS encryption mode from Flexible to Full or Full (strict):

  • Flexible: Browser → (HTTPS) → CDN → (HTTP) → Nginx. (Avoid this).
  • Full: Browser → (HTTPS) → CDN → (HTTPS) → Nginx. Requires a valid or self-signed cert on Nginx.
  • Full (strict): Browser → (HTTPS) → CDN → (HTTPS) → Nginx. Requires a valid, trusted CA certificate on Nginx (like Let’s Encrypt).

If you haven’t set up an origin certificate yet, generate one using our guide on Let’s Encrypt Wildcard SSL with Certbot on Nginx. Once Nginx listens on port 443 with a valid certificate, set your CDN origin protocol to HTTPS.

Give the change a minute, then rerun curl -IL https://example.com. If the chain now ends in a single 200, skip to Step 6 to clear cached redirects.

Step 3 (If the CDN Must Use HTTP): Redirect Only Real HTTP Visitors

Use this step only when the CDN has to keep talking plain HTTP to your origin. When a CDN proxies a request, it normally sends the visitor’s original protocol in the X-Forwarded-Proto header. Nginx ignores this header unless you tell it to use it.

On the server, open the main Nginx config (usually /etc/nginx/nginx.conf) and add these map blocks inside the existing http {} block. They create a $redirect_to_https variable that is 1 only when the visitor really used plain HTTP:

# Add inside the existing http { } block (do not create a second one)
# Check forwarded protocol from CDN
map $http_x_forwarded_proto $redirect_to_https {
    default 0;
    "http"  1;
    ""      $is_http_without_proxy;
}
# Fallback for direct requests that carry no X-Forwarded-Proto header
map $scheme $is_http_without_proxy {
    default 0;
    "http"  1;
}

Next, edit your virtual host server block (e.g., /etc/nginx/sites-available/example.com). Instead of a blind redirect on port 80, only redirect when the CDN confirms the original visitor was on HTTP:

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    if ($redirect_to_https = 1) {
        return 301 https://$host$request_uri;
    }
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
    }
}

Anyone who reaches your origin IP directly can send a fake X-Forwarded-Proto header. To stop that, allow ports 80 and 443 only from your CDN’s published IP ranges, as shown in our guide to restricting origin server traffic to CDN IPs with UFW and Nginx.

Step 4: Fix HTTPS Detection in WordPress and PHP

If you’re running WordPress or PHP-FPM behind Nginx, PHP checks the HTTPS environment variable. When Nginx receives a proxied request on port 80, $https is empty. The application assumes the connection is insecure and issues its own 301 redirect back to https://.

You can fix this in Nginx by setting the HTTPS FastCGI parameter (the variables Nginx hands to PHP-FPM) from the forwarded protocol header.

The map goes in the http context; the fastcgi_param HTTPS line goes in your PHP location block, after the include:

# http context: map incoming forwarded proto to FastCGI HTTPS flag
map $http_x_forwarded_proto $fastcgi_https_param {
    default $https;
    https   on;
}
server {
    listen 80;
    server_name example.com;
    location ~ \.php$ {
        include fastcgi_params;
        # Use the socket of your installed PHP version
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        # Tells PHP that the visitor's connection is HTTPS
        fastcgi_param HTTPS $fastcgi_https_param if_not_empty;
    }
}

The parameter is omitted for plain HTTP visitors rather than sent as off, because a lot of PHP code only checks whether $_SERVER['HTTPS'] is non-empty.

If you’re on WordPress and can’t edit global Nginx maps, you can configure WordPress to trust reverse proxy headers directly. Open wp-config.php in the WordPress root folder (over SSH, SFTP or your host’s file manager) and add this above the /* That's all, stop editing! */ line:

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https') {
    $_SERVER['HTTPS'] = 'on';
}

This stops WordPress from generating redundant internal canonical redirects when the front-end connection is already secure. If you hit asset issues after doing this, see our walkthrough on how to fix WordPress mixed content warnings after SSL.

Step 5: Test and Reload Nginx

Always validate your Nginx configuration before reloading. A typo makes the reload fail and, worse, stops Nginx from coming back up the next time it restarts. While nginx -t fails, the running site keeps the old config, so fix the error (or restore your backup) first.

Run the configuration test on the server:

sudo nginx -t

If it returns syntax is ok and test is successful, reload Nginx:

sudo systemctl reload nginx

Step 6: Purge CDN Cache and Verify

Here’s a common trap: you fix the Nginx config, test in Chrome, and still see ERR_TOO_MANY_REDIRECTS. That happens because 301 redirects are cached aggressively by both your browser and the CDN edge.

Here is how to get a clean verification:

  1. Purge the CDN Cache: In your CDN dashboard, purge everything, or at least the homepage and every URL you tested, because the edge may have stored the redirect.
  2. Use Incognito or Clear Browser Cache: Open a fresh private window to avoid cached HSTS/301 responses.
  3. Verify with cURL: Test the URL from your own computer’s terminal:
curl -I https://example.com/

A clean setup returns a single HTTP/2 200 (or HTTP/1.1 200 OK) on the first request without bouncing through intermediate redirects.

If It Still Loops: Common Causes

If you’re still seeing redirect loops after applying these changes, check these:

  • CDN SSL error after switching to Full (strict): the CDN cannot complete HTTPS to your origin, usually because Nginx has no certificate on port 443 or the certificate has expired or doesn’t match the domain. Install or renew the certificate, or use Full (not strict) only as a short-term bridge.
  • Conflicting redirects at the edge: On Cloudflare, make sure “Always Use HTTPS”, your Redirect Rules, and any older Page Rules don’t fight a redirect at your origin (an origin that sends HTTPS requests back to HTTP loops just as reliably). See the Cloudflare SSL redirect troubleshooting documentation for the combinations that loop.
  • Nested Reverse Proxies: If Nginx proxies to Docker or Node.js, ensure Nginx passes proxy_set_header X-Forwarded-Proto $scheme; or carries through the edge header properly as described in the Nginx proxy module documentation.
  • Mismatched Site URLs: In WordPress (siteurl / home in the database) or Laravel (APP_URL in .env), make sure the URL begins with https://. If the application thinks its canonical URL is HTTP, it will loop indefinitely.

Frequently Asked Questions

Does clearing cookies fix ERR_TOO_MANY_REDIRECTS?

Only if the loop lives in your browser, such as a stale cookie or a cached redirect. If curl -IL https://example.com still shows the same redirect repeating, the loop is on the CDN or the server, and clearing cookies will not help. Fix the SSL mode or redirect rule first, then clear the browser cache.

Can I just disable SSL on my Nginx origin server?

You can run plain HTTP on your origin if your CDN SSL mode is set to Flexible, but your origin-to-edge traffic will travel unencrypted over the public internet. The safer, standard approach is to put a free cert on Nginx and run Full or Full (Strict) SSL.

How do I confirm Nginx is receiving X-Forwarded-Proto?

Echo it back in a temporary response header. Add add_header X-Debug-Proto $http_x_forwarded_proto; inside your Nginx location block, reload, and run curl -I https://example.com to see which value reaches your server. Remove the line when you’re done.

Why is the error still showing after I fixed Nginx?

Browsers cache 301 redirects for a long time (often until you clear the cache), and CDN edge nodes cache them for their TTL. You must purge your CDN cache and test using an incognito window or curl -IL to bypass stale local caches.

Next Steps

Once curl -IL https://example.com returns a single 200, keep the fix from coming back. With Full (strict), an expired origin certificate breaks the site again, so make sure renewals work. Our guide to fixing Certbot auto-renewal failures on Nginx covers the renewal checks and the reload hook.

all_in_one_marketing_tool