Install Varnish for WordPress

Varnish is a front-end cache to speed up your website, such as WordPress.


Varnish is a front‑end cache, also known as a "web application accelerator". The software acts as a reverse proxy and caches content between the user and the web server to speed up access to your website, especially during high-traffic periods.

In this guide, we'll walk you through installing Varnish, optimized for WordPress, on Debian 13.

A website built with WordPress can be cached effectively when its content does not need to be updated "live" but can be delayed by 2–5 minutes before the visitor sees the new material. In this way, the page does not have to be regenerated for every page load.

In this example, we'll walk you through a standard Varnish installation on a server running Debian 13 (note that Varnish does not have to run on the same server as the web server). The default configuration will cache all content for 120 seconds or for as long as your Cache‑Control header permits.

Installation

In Debian 13, you install Varnish using apt:

Command
sudo apt install varnish

Varnish listens by default on port 6081, but to use it as a cache for web pages, you want to change this so that it listens on port 80. This can be addressed by creating a new service in systemd. Be sure to verify that no other web server is running on port 80 before you make this change.

Multiple commands
sudo cp /lib/systemd/system/varnish.service /etc/systemd/system/
sudo vim /etc/systemd/system/varnish.service

Change the ExecStart so that you replace 6081 with 80. That is, change this section:

Snippet from /etc/systemd/system/varnish.service
ExecStart=/usr/sbin/varnishd \
          -j unix,user=vcache \
          -F \
          -a :6081 \
          -T localhost:6082 \
          -f /etc/varnish/default.vcl \
          -S /etc/varnish/secret \
          -s malloc,256m

Into this:

It’s also in this file that you can increase the amount of RAM Varnish is allowed to use for its cache. To change it, simply replace -s malloc,256m with -s malloc,1024m to raise the limit to 1024 MB.

Then, we need to reload the systemd daemon and restart Varnish for the changes to take effect.

Configuration

Varnish has its own configuration language called VCL (Varnish Configuration Language). By default, it ships with an empty configuration where you can specify which backend server Varnish should cache.

If you don’t set any caching rules, Varnish will fall back to its built‑in defaults, which cache everything except:

  • HTTP methods other than GET and HEAD

  • Users authenticated with HTTP Authorization

  • Requests or responses that contain cookies

What gets cached follows the Cache‑Control header in the HTTP response from the application. If no header is set, Varnish will use a Time‑to‑Live (TTL) of 120 seconds.

The default configuration that comes with Debian 13 looks like this, and is found at /etc/varnish/default.vcl:

To ensure Varnish connects correctly to your backend, you need to update the IP address and port number in the configuration to point to your web server. The section you need to update is this:

Varnish decides whether a request is already in the cache by using a hash. You can control this hash with the vcl_hash function. By default, the hash is based on the URL, the hostname, or the server’s IP address. Note that this hashing occurs after Varnish has determined that the request is eligible for caching.

Because of this, if you are not logged into WordPress and you have no plugins that use cookies, Varnish’s default settings will provide a 120‑second cache for each page for all visitors.

Remember to restart Varnish after you change the configuration. You restart Varnish with:

How do you know if the content is being cached?

Varnish adds an HTTP header called X‑Varnish, which reveals whether the request passed through Varnish and contains the ID of the cached object. In addition, an Age header is set, indicating how old the cached object is.

You can use cURL with the -i options to show all headers. For example:

If the Varnish has served the request, you should see something similar to this:

Here you can see that Varnish is used (X-Varnish: 32779 17 and Via: 1.1 varnish (Varnish/7.1)). You also see that this page has been sitting in the cache for 53 seconds (Age: 53).

Further reading

Official website for Varnish HTTP Cache: https://varnish-cache.org.

User guide for VCL: https://varnish-cache.org/docs/trunk/users-guide/vcl.html.

Need help with Varnish?

With our service, Manage Hosting, we can help you set up and manage Varnish.

Last updated

Was this helpful?