Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

ERR_TOO_MANY_REDIRECTS after turning on HTTPS behind a proxy or CDN

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

If a browser shows ERR_TOO_MANY_REDIRECTS (Cloudflare's page names two browser messages: ERR_TOO_MANY_REDIRECTS and The page isn’t redirecting properly) right after you enable HTTPS in front of a site, two layers disagree about whether the visitor is on HTTP or HTTPS. One loop pattern: the proxy or CDN forwards the request to your server over plain HTTP, your server answers "go to HTTPS", and the proxy sends the visitor to HTTPS, which it again forwards as HTTP.

Trace the redirect chain with curl -I -L, find which layer issues each redirect, then either make the server act on the X-Forwarded-Proto header the proxy sends, or change the proxy's mode so it talks HTTPS to the server.

What the error means

Redirection is a normal HTTP feature. MDN describes it this way: "redirection is triggered by a server sending a special redirect response to a request. Redirect responses have status codes that start with 3, and a Location header holding the URL to redirect to." A loop is "additional redirections follow the one that has already been followed", so that "there is a loop that will never be finished and no page will ever be found". MDN adds that the server sometimes detects it and returns a 500 error, and sometimes does not, because the loop "can spread over several servers".

Browsers stop after a limit and show the error. curl has its own limit, and the curl manual says that with --location "this option prevents curl from following too many redirects. By default the limit is set to 50 redirects."

Why a proxy creates the loop

Your server's view of the request is the proxy's request, not the visitor's. MDN explains the header that carries the visitor's view: X-Forwarded-Proto "is a de-facto standard header for identifying the protocol (HTTP or HTTPS) that a client used to connect to a proxy or load balancer", and "server access logs contain the protocol used between the server and the load balancer, but not the protocol used between the client and the load balancer." MDN notes it is "not part of any current specification" and that the standardised form is the Forwarded header; RFC 7239 defines proto as one of its parameters, with an example Forwarded: for=192.0.2.60;proto=http;by=203.0.113.43.

AWS documents the same behaviour for its Application Load Balancers: "Your application or website can use the protocol stored in the X-Forwarded-Proto request header to render a response that redirects to the appropriate URL."

Cloudflare's troubleshooting page spells out two loops that match the pattern:

  • In Flexible mode, "Cloudflare sends unencrypted requests to your origin server over HTTP. Redirect loops will occur if your origin server automatically redirects all HTTP requests to HTTPS."
  • In Full or Full (strict) mode, "Cloudflare sends encrypted requests to your origin server over HTTPS. Redirect loops will occur if your origin server automatically redirects all HTTPS requests to HTTP."

The same page lists other loops: Always Use HTTPS combined with an origin that redirects to HTTP, HTTP Strict Transport Security (HSTS) with an origin that redirects to HTTP or with the encryption mode set to Off, and conflicting redirect rules.

Likely causes

Cause What you see How to confirm
The proxy connects to your server over HTTP, and your server redirects every HTTP request to HTTPS Loop starts when HTTPS is turned on at the proxy; direct HTTP requests to the server redirect once curl -I straight to the server without an X-Forwarded-Proto header gets a 301 or 302 to https://; the proxy then repeats
The application builds its redirect from its own scheme, not from X-Forwarded-Proto The loop appears only on certain pages, such as login or admin The Location header in the loop is an https:// URL that the proxy turns back into HTTP, or the application's site URL setting is http://
The server redirects HTTPS to HTTP Loop only when the proxy connects over HTTPS (Full mode) Location points at http:// for an HTTPS request
Two layers each redirect Loop disappears when one layer's redirect is disabled curl -L shows Location headers coming from alternating sources (check Server or Via headers)
HSTS or an "always HTTPS" setting at the CDN conflicts with the origin Browser behaves differently from curl A browser loops but curl does not; compare with a private window
Conflicting redirect rules between hosts (for example www and non-www) The loop alternates between two host names The two Location values are different host names

Prove the cause before changing anything

All of these only read.

1. Follow the redirects and print every hop. The curl manual says -I "fetch[es] the headers only", and that when --location is used "together with --show-headers or --head, headers from all requested pages are shown". Limit the hops so that the output stays short.

curl -I -L --max-redirs 5 http://app.example.com/

Each hop shows a status line and a Location header. A loop shows the same two URLs alternating. Note the Server and Via headers on each hop, because they tell you whether the proxy or your server issued the redirect.

2. Talk to your server directly, the way the proxy does. Replace 203.0.113.10 with your server's address, and use the host name your site uses. -H adds a header to the request.

curl -I -H "Host: app.example.com" http://203.0.113.10/
curl -I -H "Host: app.example.com" -H "X-Forwarded-Proto: https" http://203.0.113.10/

Read the first response. If it is a redirect to https://, your server is redirecting on its own scheme. Read the second response: if it now returns 200 (or the site itself), your server already honours X-Forwarded-Proto and the fault is in the proxy. If it still redirects, the server or application ignores the header.

3. Check what the proxy sends. Look for the setting that sets X-Forwarded-Proto. In nginx used as a proxy, the WordPress documentation shows proxy_set_header X-Forwarded-Proto $scheme; in the location block. nginx also documents that proxy_set_header directives "are inherited from the previous configuration level if and only if there are no proxy_set_header directives defined on the current level", so a new proxy_set_header line in a location can silently drop one set higher up.

Example (illustrative): A company puts app.example.com behind a CDN in Flexible mode, so the CDN reaches web01 (203.0.113.10) over HTTP. web01 runs nginx with return 301 https://$host$request_uri; in its port 80 server block. Step 1 shows http://app.example.com/ redirecting to https://app.example.com/, which redirects again. Step 2's first command shows web01 returning a 301 to https:// for plain HTTP; the CDN never sees HTTPS on that leg, so it asks again. The loop is the origin redirect, not the CDN.

Fix

Choose A or B; C applies where two layers both redirect. Take a copy of the configuration files first.

A. Make the server trust the proxy's header

Only do this if the proxy is the only way traffic reaches the server, because any visitor could otherwise send X-Forwarded-Proto: https themselves.

nginx behind a proxy. nginx documents the map directive, which creates a variable whose value depends on another variable, with default as the fallback. Redirect only when the proxy says the visitor used HTTP:

map $http_x_forwarded_proto $force_https {
    default 0;
    http    1;
}

server {
    listen 80;
    server_name app.example.com;
    if ($force_https) {
        return 301 https://$host$request_uri;
    }
    # ... normal site configuration ...
}

$http_x_forwarded_proto is the nginx variable for the X-Forwarded-Proto request header: for $http_name the field name is "converted to lower case with dashes replaced by underscores". return with a URL and $host, $request_uri is the documented redirect form. A request that reaches the server directly, without a proxy, carries no header and is not redirected, which is why this is only safe when the proxy is the only path in.

Risk: a direct request that bypasses the proxy is no longer redirected to HTTPS. To back out, restore the previous server block and reload.

Apache behind a proxy. The RewriteCond test string %{HTTP:header} reads a request header, =http compares exactly, and [R=301,L] flags an external redirect (the manual's flag is redirect|R[=code]):

RewriteEngine on
RewriteCond "%{HTTP:X-Forwarded-Proto}" "=http"
RewriteRule "^/?(.*)" "https://%{HTTP_HOST}/$1" [R=301,L]

The Apache manual shows the HTTPS variable as "on" if the connection uses SSL/TLS, "off" otherwise, so a rule that tests %{HTTPS} alone sees only the proxy-to-server leg and redirects every proxied request.

Risk: a visitor who can reach the server directly can send X-Forwarded-Proto and choose the scheme. To back out, remove the rule and reload.

Applications. Many applications build their own redirects. WordPress's documentation says that behind a proxy "you may configure WordPress to recognize the HTTP_X_FORWARDED_PROTO header (assuming you have properly configured the reverse proxy to set that header)" and a code comment there says "in some setups HTTP_X_FORWARDED_PROTO might contain a comma-separated list e.g. http,https", so the code tests for https within the value. Check your application's own documentation for the equivalent setting, and make sure its configured site URL starts with https://.

B. Make the proxy talk HTTPS to the server

Where your server has a valid certificate, switch the proxy mode that talks plain HTTP (Cloudflare's Flexible) to the one that connects over HTTPS (Full or higher), and keep the server's HTTP-to-HTTPS redirect. Cloudflare's page recommends exactly this: "either remove HTTPS redirects from your origin server or update your SSL/TLS Encryption Mode to be Full or higher (requires an SSL certificate configured at your origin server)". With that mode, check that the server does not redirect HTTPS to HTTP.

Risk: changing the proxy's connection mode alters the certificate the proxy validates at your server, and a bad certificate can turn a loop into a 5xx error. Make the change in a quiet period, and have the previous setting written down so you can switch it back.

C. Remove a duplicate redirect

If two layers both redirect, keep only one and remove the other. Cloudflare's page names the options to adjust: remove the origin's redirects, or disable Always Use HTTPS or HSTS at the CDN, or fix conflicting redirect rules.

Risk: removing a redirect or turning off HSTS changes what visitors and browsers do at once. To back out, restore the previous setting.

Apply the change

Disruptive step: reload the web server so the new redirect takes effect. For nginx, run nginx -t first (the program's own check, not described on the nginx pages read), then nginx -s reload; on reload the master process "checks the syntax validity of the new configuration file" (Beginner's Guide). For Apache, apachectl configtest then apachectl graceful, which "automatically checks the configuration files" and restarts without aborting open connections. To back out, restore the saved files and reload again.

Confirm the fix

  1. curl -I -L --max-redirs 5 http://app.example.com/ ends in a 200 response, or one redirect to HTTPS followed by the page, with no repeating pair.
  2. Both direct tests from step 2 behave as intended: the server alone does not loop.
  3. Open the site in a private window, so no saved HSTS state or cookies are involved, and log in if the application has a login page.
  4. Test the www and non-www names and a deep link, not only the home page.

What to watch afterwards

  • Record in your change notes which layer owns the HTTP-to-HTTPS redirect. Two layers that both redirect can loop, so name one owner.
  • When you add a proxy, load balancer or CDN, test redirects before and after, and test the application's login and admin pages, which build their own URLs.
  • Real client addresses also come from proxy headers; see "Logs show the load balancer's IP, not the client's: X-Forwarded-For and PROXY protocol" when you reach that problem.

Risks and limits

  • These commands have not been run or lab-tested. They are taken from the vendor documentation cited in Sources.
  • X-Forwarded-Proto is a de-facto header, not a standard. Any client can send it, so only trust it when the proxy is the sole route to the server, or when the server accepts the header only from the proxy's addresses.
  • The nginx map and if example redirects only requests that arrive with the header set to http. If your proxy sends a different value, change the map.
  • Where HSTS is in play, browsers are told to turn HTTP links into HTTPS ones, so a browser can behave differently from curl. Test in a private window as well as with curl.

Sources