Plesk

Varnish for WordPress in a Docker container

Is your website experiencing heavy traffic? Are you looking for a solution that will reduce server load and will improve website speed? Varnish might just be what you need. Varnish listens for duplicate requests and provides a cached version of your website pages, mediating between your users’ requests and your server.

So how do you activate Varnish? In this article, I will show you how you can easily increase your website speed by using Varnish as a one click Docker container. I will demonstrate how using a website caching solution like Varnish can easily improve both page response times and the maximum number of concurrent visitors on your website. 

Varnish blog Plesk

What is Varnish and why should you use it?

Varnish caching service is an open-source web application that operates as an HTTP reverse-proxy. While it creates a cache after serving a request, it uses it to serve the other visitor’s request in case the similar one was cached earlier. Hence, generally, there are the following benefits of such a solution:

  • Performance. The main advantage of Varnish, since even most of the dynamic content can be served using cache and thus avoid any backend processing time.
  • Less load on the server. Lower number of requests served to avoid using the backend functions.
  • Configuration flexibility. VCL(Varnish configuration language) allows the administrator to perform additional fine-tuning of the caching environment.

Now speaking of the “HTTP Reverse-proxy” part, which is the method Varnish uses. In Plesk we already have NGINX operating as a frontend webserver, so how can we simultaneously run Varnish? That is where a Docker container with its proxy rules comes in.

Running Varnish in a Docker container

Docker is an open-source project that makes deploying service applications, including their native environment, incredibly simple. In Plesk, we have this software implemented as an extension, which is thoroughly described in this Plesk documentation. Hence, instead of installing Varnish on the Plesk server, we are going to deploy the official Varnish Docker container:

Activate Varnish in Plesk and test it on a static page

Alright, now the theoretical questions are settled, let’s get to the practical part. In default Plesk configuration we have Apache2 listening on 7080/tcp(HTTP), and 7081/tcp(HTTPS), and NGINX, which acts as a proxy and actually serves the content via 80/tcp and 443/tcp.

Meanwhile, we will now also have a Varnish container in between those two. Hence, the Varnish container would receive a request from NGINX and pass back the content from Apache2. But don’t worry, it’s simpler than it sounds!

Before we proceed, first we need to make sure that the Docker extension is installed. In the Plesk panel, Navigate to Extensions > Extensions catalog, and in the search bar type “Docker”. Then, click “Get it free” if it is not installed, or “Open” if it is.

In the images catalog, find the official Varnish image and select a stable version of the container (6.6.2) to install and run the Varnish container:

On the page that opens, edit the container’s settings. Uncheck the “Automatic Port Mapping” function and apply the following manual mappings instead:

That means that all HTTP requests that are sent to the 32780/tcp of the server would be redirected to the port 80/tcp of the Varnish Docker container. Even though we are not going to use the 32781 rule, it is necessary to set it up by design.

Click “Ok” to run the container.

Once this is done, it is now required to configure the domain. Navigate to Domains > example.com > Docker Proxy Rules and create the following proxy rule:

Then, navigate to Domains > example.com > Hosting Settings and disable the “Permanent SEO-safe 301 redirect from HTTP to HTTPS” option, since we want to test the Varnish functionality over HTTP first.

Now we need to permit access to the Apache2 port from the local Docker subnet (172.17.0.0/24). This can be achieved by means of IPtables using the following command:

# iptables -I INPUT -p tcp --dport 7080 -s 172.17.0.0/24 -j ACCEPT

Once added, save the IPtables ruleset with this one:

# iptables-save

However, in case there is a Plesk Firewall extension installed on the server, it is required to add the rule using Plesk Firewall GUI via Tools & Settings > Security(Firewall) > Add Custom Rule:

Once we have the firewall configured, it is time we configure Varnish itself to address the IP:Port of the local Apache2 server.

Important note: In the below commands, it is required to replace 203.0.113.2 with the actual IP address of the domain as well as the container’s name (Varnish) if it was set to a custom one.

Now to edit the /etc/varnish/default.vcl within the container, run the next 2 commands:

# docker ps | grep varnish | awk {'print $1'} | xargs -i docker exec -t {} sed -i '/.port/c\ .port = "7080";' /etc/varnish/default.vcl
# docker ps | grep varnish | awk {'print $1'} | xargs -i docker exec -t {} sed -i '/.host/c\ .host = "203.0.113.2";' /etc/varnish/default.vcl

After the configuration changes are applied, restart the container:

# docker ps | grep varnish | awk {'print $1'} | xargs -i docker restart {}

Now when we open our website, http://example.com, and check it via the browser’s developer tools (Headers), we can see that after the initial request the page was loaded using Varnish cache:

Configuring Varnish in Plesk to serve a WordPress instance

At this point, we already have WordPress installed on our example.com domain.

First of all, let’s download the WordPress configuration preset and put it into the Docker’s configuration catalog:

# curl -o /tmp/default.vcl --silent https://support.plesk.com/hc/en-us/article_attachments/4405703421586/default.vcl && docker ps | grep varnish | awk {'print $1'} | xargs -i docker cp /tmp/default.vcl {}:/etc/varnish/

Once the configuration is copied, it’s time to adjust the config file to use the domain’s IP Address:

# docker ps | grep varnish | awk {'print $1'} | xargs -i docker exec -t {} sed -i 's/XXX.XXX.XXX.XXX/203.0.113.2/g' default.vcl

And restart the container to apply changes:

# docker ps | grep varnish | awk {'print $1'} | xargs -i docker restart {}

Use a WordPress Plugin to activate support for HTTPS

Important: Do not use the option “Permanent SEO-safe 301 redirect from HTTP to HTTPS” within Plesk in “Hosting Settings” because this will lead to a redirect loop in our special environment constellation.

Instead, configure the next additional Apache2 HTTP/HTTPS directives at Domains > example.com > Apache & Nginx Settings:

SetEnvIf X-Forwarded-Proto "https" HTTPS=on

Header append Vary: X-Forwarded-Proto

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{HTTPS} !=on

RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]


RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

That’s it! Now we have our WordPress website powered by a Varnish caching server. In the case that you would like to use Varnish for different CMS, you can check the official Varnish tutorials.

Summary: Varnish for WordPress within a Docker container on Plesk

Let me make a small checklist:

Varnish in Docker container? Yes.
Varnish in WordPress? Yes.
Varnish in Plesk? Yes.
Varnish for WordPress within Docker container in Plesk? Absolutely, yes!
Mission accomplished! 🙂

As you’ve seen, Varnish can greatly improve the performance of your WordPress website and reduce the CPU-load of your server. It’s relatively easy to setup a working environment using Varnish in a Docker container between Nginx and Apache within Plesk. The most important part is the correct configuration of Varnish for your specific CMS.