Mixed content and ERR_TOO_MANY_REDIRECTS·续

When your browser returns the ERR_TOO_MANY_REDIRECTS error, it means it got stuck in an infinite redirect loop and gave up after a number of attempts.

The ERR_TOO_MANY_REDIRECTS error in the browser

This can also be related to a lack of TLS awareness and usually happens when HTTP to HTTPS redirection is enforced in WordPress.

Here’s an example of an HTTP to HTTPS redirection in WordPress. We call the plain HTTP version of the homepage using curl, and you see the 301 status code and the Location header that redirects you to the HTTPS version:

$ curl -I http://localhost

HTTP/1.1 301 Moved Permanently
Date: Tue, 21 Nov 2023 14:00:24 GMT
Server: Apache/2.4.56 (Debian)
X-Powered-By: PHP/8.0.30
X-Redirect-By: WordPress
Location: https://localhost/
Content-Length: 0
Content-Type: text/html; charset=UTF-8
X-Varnish: 262374
Age: 0
Via: 1.1 varnish (Varnish/7.4)
Connection: keep-alive

Varnish itself isn’t aware that the HTTP version of a response can sometimes differ from the HTTPS version. When Varnish fetches the plain HTTP version of an HTTPS-enforced WordPress page and stores it in the cache, the 301 response is essentially cached.

As long as the cached object hasn’t expired, the 301 redirect is going to be served to everyone, even if the request was originally made over HTTPS. That’s how you get stuck in a redirect loop and that’s why the ERR_TOO_MANY_REDIRECTS appears.

Vary: X-Forwarded-Proto#

The way we tackle this issue is by giving Varnish some TLS awareness. Although Varnish sets the X-Forwarded-Proto header, it doesn’t use the value of that header when creating a lookup hash.

When looking up objects in the cache, Varnish uses the URL and the Host header to create a lookup hash. There is no reference to the protocol or URL scheme. By creating a cache variation based on the X-Forwarded-Proto header, Varnish will have multiple objects stored in cache for the same URL and Host header combination, basically storing an HTTP and an HTTPS version of each page.

The conventional way of informing HTTP caches about variations is by setting a Vary header. The value of this response header is a valid request header. In our case that would be Vary: X-Forwarded-Proto.

This means that we’re letting Varnish know that cache variations need to be made based on the value of the X-Forwarded-Proto request header it receives during requests.

There are many ways of doing this. Let’s focus on a couple of specific ones.

Setting the Vary header in Apache#

If you’re using Apache as your web server, you can add the following line to your .htaccess file:

Header append Vary: X-Forwarded-Proto

This configuration will either set the Vary header with X-Forwarded-Proto as its value, or it will append X-Forwarded-Proto to any existing Vary header.

If you’re already checking the X-Forwarded-Proto header in Apache, these are the 2 lines that will be added:

SetEnvIf X-Forwarded-Proto "https" HTTPS=on
Header append Vary: X-Forwarded-Proto

Setting the Vary header in Nginx#

If you’re using Nginx as a web server, there is similar syntax to add a Vary header. Simply add this line to your vhost configuration:

add_header Vary X-Forwarded-Proto;

If you’re already checking the X-Forwarded-Proto header in Nginx, this is what your config may look like:

server {
    listen 80;
    root /var/www/html;
    index index.php;
    location / {
        try_files $uri /index.php$is_args$args;
    }

    set $forwarded_https "Off";
    if ($http_x_forwarded_proto = "https") {
        set $forwarded_https "On";
    }

    add_header Vary X-Forwarded-Proto;


    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        include        fastcgi_params;
        fastcgi_param HTTPS $forwarded_https;
    }
}

Adding protocol awareness in Varnish#

Instead of adding the Vary header through your web server, you could also add protocol awareness to Varnish and ensure the value of the X-Forwarded-Proto header is used to create the lookup hash. To do this, simply add this code to your VCL file:

sub vcl_hash {
    if(req.http.X-Forwarded-Proto) {
        hash_data(req.http.X-Forwarded-Proto);
    }
}

This VCL code will extend the lookup hash and will add the value of the X-Forwarded-Proto header to that hash when performing cache lookups. This means that the URL, the host and the scheme are used to identify an object in the cache.

Only use this VCL code if you’re not able to set Vary: X-Forwarded-Proto in your web server or in WordPress. Using a Vary header is a more conventional and portable solution than using VCL code.

The real solution: use native TLS in Varnish#

As mentioned in the beginning of this tutorial: as of Varnish version 9 there is native TLS support built in. Of course Varnish Enterprise has had native TLS support for years.

If you configured TLS in Varnish, incoming connections over HTTPS will not be a problem. If you want to send HTTPS requests to WordPress, simply add .ssl = 1; to your backend definition as seen below:

backend default {
   .host = "127.0.0.1";
   .port = "443";
   .ssl = 1;
}

HTTP to HTTPS redirection#

To avoid that plain HTTP requests are proxied to the origin web server as HTTPS requests, we can perform HTTP to HTTPS redirection in Varnish.

If you’re enforcing HTTP to HTTPS redirection, we suggest doing this in Varnish before WordPress gets hit. Because all backend requests are done over HTTPS, even if the incoming request is plain HTTP.

This VCL code will redirect plain HTTP requests to their HTTPS variant before the cache is even touched:

import std;

backend default {
   .host = "127.0.0.1";
   .port = "443";
   .ssl = 1;
}

sub vcl_recv {
   unset req.http.location;

   if (std.port(server.ip) != 443) {
       set req.http.location = "https://" + req.http.host + req.url;
       return (synth (301));
   }
}

sub vcl_synth {
   if (resp.status == 301 ||
       resp.status == 302 ||
       resp.status == 303 ||
       resp.status == 307) {
       if (!req.http.location) {
           std.log("location not specified");
           set resp.status = 503;
           return (deliver);
       } else {
           set resp.http.location = req.http.location;
           return (deliver);
       }
   }
}

Proxy over plain HTTP and still use X-Forwarded-Proto#

Despite accepting HTTPS requests directly in Varnish, you can still choose to communicate with your WordPress site over plain HTTP. In that case the X-Forwarded-Proto header is still required to signal the original protocol that was used for the request.

This is what the VCL code could look like:

vcl 4.1;

import std;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    if (!req.http.X-Forwarded-Proto) {
        if(std.port(server.ip) == 443) {
            set req.http.X-Forwarded-Proto = "https";
        } else {
            set req.http.X-Forwarded-Proto = "http";
        }
    }
}

sub vcl_backend_response {
    if(beresp.http.Vary) {
        set beresp.http.Vary = beresp.http.Vary + ", X-Forwarded-Proto";
    } else {
        set beresp.http.Vary = "X-Forwarded-Proto";
    }
}

Varnish also comes with a TLS VMOD that can detect TLS connections without checking the port. This leverages the tls.is_tls() function, and this is what that VCL code looks like:

vcl 4.1;

import tls;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    if (!req.http.X-Forwarded-Proto) {
        if(tls.is_tls()) {
            set req.http.X-Forwarded-Proto = "https";
        } else {
            set req.http.X-Forwarded-Proto = "http";
        }
    }
}

sub vcl_backend_response {
    if(beresp.http.Vary) {
        set beresp.http.Vary = beresp.http.Vary + ", X-Forwarded-Proto";
    } else {
        set beresp.http.Vary = "X-Forwarded-Proto";
    }
}