- Mixed content and ERR_TOO_MANY_REDIRECTS errors in WordPress when using Varnish
- TLS termination with Varnish Cache#
- A lack of TLS awareness in WordPress#
- Mixed content#
- Setting the X-Forwarded-Proto header#
- Checking the X-Forwarded-Proto header in WordPress#
- Checking the X-Forwarded-Proto header in Apache#
- Checking the X-Forwarded-Proto header in Nginx#
- We’ve solved the mixed content issue#
- The ERR_TOO_MANY_REDIRECTS error#
Mixed content and ERR_TOO_MANY_REDIRECTS errors in WordPress when using Varnish
If you’re running a WordPress site and use Varnish without TLS support, there are 2 typical issues you may encounter when it comes to offloading TLS: mixed content and the dreaded ERR_TOO_MANY_REDIRECTS error. Both issues are related to a lack of TLS awareness in the stack.
This tutorial will show you how to tackle both issues and how to create TLS awareness in a situation where both WordPress and Varnish might be lacking that awareness.
This tutorial assumes that TLS is not enabled in Varnish. Prior to the release of version 9, Varnish did not support native TLS and required a TLS proxy to offload TLS.
We advise against this solution and recommend you use Varnish’s native TLS capabilities to avoid this issue.
TLS termination with Varnish Cache#
If TLS is not enabled in Varnish or you’re using an older version that doesn’t support TLS nativelly, TLS sessions need to be terminated by a TLS Proxy.
There are many TLS proxy servers out there: some of them are pure TLS proxies without any HTTP awareness, but most of them are HTTP proxies or HTTP-based load balancers. We even develop our own open source TLS proxy, called Hitch.
The diagram below shows the various HTTP components in the stack:

When calling an HTTPS-based WordPress page, the user first connects to the TLS proxy. After the TLS session has been terminated, the TLS proxy sends the unencrypted HTTP request to Varnish. Varnish will try to serve the request from the cache or fetch it from WordPress. All of this happens over plain and unencrypted HTTP once the TLS proxy has terminated the TLS session.
A lack of TLS awareness in WordPress#
The fact that all communication between Varnish and WordPress happens over plain HTTP is not necessarily the issue. The fact that WordPress has no TLS awareness is the real issue.
WordPress, written in the PHP language, uses the value of the $_SERVER["HTTPS"] superglobal variable to determine the protocol. When its value is set to on, WordPress knows the page was requested over HTTPS, otherwise plain HTTP is served.
It is the web server that is in charge of communicating the use of HTTPS to the PHP runtime. It does so through an HTTPS environment variable.
Since the web server only receives plain HTTP requests, the HTTPS environment variable will never be automatically enabled, which causes the mixed content problem.
Mixed content#
When mixed content is served to the browser, it means that a mixture of plain HTTP and HTTPS URLs are loaded for a single page. This behavior is deemed unsafe by browsers and causes errors. Modern browsers have switched from solely displaying an error message to proactively upgrading the plain HTTP URLs to HTTPS URLs.
The error message below is an example of mixed content in WordPress:

The image below shows a breakdown of the requests that are responsible for loading mixed content:

Although the web page itself is served using HTTPS, nearly all other resources are loaded over plain HTTP. And as mentioned earlier, this is due to a lack of TLS awareness in WordPress.
Setting the X-Forwarded-Proto header#
If WordPress doesn’t have TLS awareness when proxies are used, let’s give WordPress that awareness! That’s where the X-Forwarded-Proto header comes into play. This conventional HTTP request header is used to transport the protocol of the initial user request across the various nodes in the chain.
Its value can be either be http or https and it’s up to the TLS proxy to set it to https. However, if the TLS proxy has no HTTP awareness, it’s Varnish’s responsibility to set the X-Forwarded-Proto header.

The following VCL code will check if the X-Forwarded-Proto header was set by another proxy or set it if it doesn’t exist:
import std;
sub vcl_recv {
if (!req.http.X-Forwarded-Proto) {
if(std.port(server.ip) == 8443) {
set req.http.X-Forwarded-Proto = "https";
} else {
set req.http.X-Forwarded-Proto = "http";
}
}
}
If the X-Forwarded-Proto header does not exist, Varnish checks which server port was used. Whereas normal HTTP traffic goes over port 80, port 8443 is typically used by the TLS proxy. If the request was received on that port, we know it was an HTTPS request.
If that was the case, we set the value of the X-Forwarded-Proto to HTTPS. Otherwise we set it to HTTP.
While most frameworks and CMS offer native support for the X-Forwarded-Proto header, WordPress doesn’t. And that’s what we need to fix.
Checking the X-Forwarded-Proto header in WordPress#
The easiest way to make WordPress TLS aware is by checking the X-Forwarded-Proto header in the wp-config.php file.
Simply add the following code in your wp-config.php file to solve the mixed content issue:
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
$_SERVER['HTTPS'] = 'on';
}
If you’re using one of the official WordPress Docker images, this check is already included in wp-config.php. This means mixed content is not a factor when using an official WordPress Docker image.
If for some reason you cannot include the X-Forwarded-Proto check in your wp-config.php file, you can set the header in your web server configuration. The paragraphs below describe how to set the X-Forwarded-Proto header directly in Apache or Nginx.
Checking the X-Forwarded-Proto header in Apache#
If you’re using Apache as your web server and you cannot set the X-Forwarded-Proto header in your WordPress configuration, simply add the following line to your .htaccess file to add the X-Forwarded-Proto header to the HTTP response:
SetEnvIf X-Forwarded-Proto "https" HTTPS=on
This line will conditionally set the HTTPS environment variable to on, and that allows WordPress to generate HTTPS URLs.
Checking the X-Forwarded-Proto header in Nginx#
If you’re using Nginx instead of Apache as your web server and you cannot set the X-Forwarded-Proto header in your WordPress configuration, you can also check the value of the X-Forwarded-Proto header in your Nginx configuration.
Unlike Apache, Nginx doesn’t directly interact with the PHP runtime, instead connects to the PHP runtime over the FastCGI protocol. The PHP-FPM service that runs the WordPress code runs on port 9000, which the Nginx web server connects to, as shown in the example below.
By setting the HTTPS FastCGI parameter, WordPress can be made aware of the original client protocol. The value again depends on the X-Forwarded-Proto header, which we check and whose value we store in a custom $forwarded_https variable.
It’s that $forwarded_https variable we use to assign a value to the HTTPS FastCGI parameter, as seen in the configuration below:
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";
}
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;
}
}
We’ve solved the mixed content issue#
Thanks to the X-Forwarded-Proto header and the protocol awareness we provided to WordPress, mixed content no longer appears.
The image below shows that all resources are now loaded using HTTPS:

One problem fixed, let’s move on to the next one.
