How to Use Varnish Cache for WordPress Websites?

How to Use Varnish Cache for WordPress Websites?

Slow websites are a big downer for website owners that significantly affect the website’s performance, sales, Google’s SEO (Search Engine Optimization) and SERP (Search Engine Results Page) rankings. In addition, users do not prefer to wait on a slow loading website for longer than 3 seconds, increasing your website’s bounce rate. Therefore, performance optimization is a must for a successful website.

There are many ways through which you can accelerate your website’s speed. One of the ways is Varnish caching. You can opt for Varnish Cache with your web hosting services. You can choose for the best Linux Hosting plans and get a free preconfigured caching installed.

Many hosting companies provide Shared Hosting and Linux Hosting services with a built-in Varnish Cache, so you do not have to install it. But if you wish to do it yourself, in this article, we will discuss how you can use and install Varnish Cache on your WordPress website in three easy steps.

What is Varnish Cache and why should you use it for your WordPress website?

Varnish is a full-page open-source caching tool that works as a reverse proxy. It stores data in virtual memory using caching. Thus, a Varnish Cache acts as an intermediary between your website and your end-users. When a visitor visits your website, the tool will check its cache to see if the web page’s copy is present to be used as a proxy.

This practice significantly reduces your website’s loading time and enhances its performance. It is much easier to present the cached content than to render and load each website content from scratch. Renewing the cache periodically delivers picture-perfect rendering of your web pages.

Varnish uses the language VCL (Varnish Configuration Language) that provides you with complete control over your proxy server configuration.

For Varnish to work on your WordPress website, you will need to install the tool on your server.

How to set up Varnish on my WordPress website?

If you are hesitant about modifying your server, it is always best to keep your website’s backup just in case.

Step 1: Installing Varnish on your server

  • To install Varnish on your server, you must first access it through the command line.
  • Then you must install the tool, modify the VCL file, and make relevant server configurations.
  • You can check out and refer to the Varnish official installation guide and instructions for this step for UNIX-based servers. It comprises a step-by-step tutorial and all the commands to use in the correct order.

Once you complete this step, restart everything to calibrate your WordPress website along with the installed tool.

Step 2: Setting up proxy cache purge plugin

This step is important so that your website’s visitors don’t view outdated content. In order to avoid serving your customers with old content, it is required for you to purge the reverse proxy cache each time you update or create a new page or a post. Proxy cache purge plugin makes this step possible.

Instead of purging the entire cache, this plugin targets and purges only the edited content, frontend of your website, and associated taxonomies.

When you change your website’s theme, this plugin automatically purges the entire reverse proxy cache.

Step 3: Configuring Varnish Settings for WordPress

You must incorporate the following small modifications in your VCL file to optimize Varnish for your WordPress website:

  • Configure Varnish to ignore certain specific cookies
  • Exclude your WordPress login pages and admin
  • Increase the reverse proxy cache duration

Open the VCL file using a text editor through the command line. You must find the file default.vcl, located here: /etc/varnish/default.vcl

You must open the file and add the following code from the official Varnish documentation.

sub vcl_rec{

..

set req.http.cookie = regsuball(req.http.cookie, "wp-settings-\d+=[^;]+(; )?", "");

set req.http.cookie = regsuball(req.http.cookie, "wp-settings-time-\d+=[^;]+(; )?", "");

set req.http.cookie = regsuball(req.http.cookie, "wordpress_test_cookie=[^;]+(; )?", "");

if (req.http.cookie == "") {

unset req.http.cookie;

}=

}

# exclude wordpress login and admin urls

if (req.url ~ "wp-admin|wp-login") {

return (pass);

}

# extending caching time

sub vcl_backend_response {

if (beresp.ttl == 120s) {

set beresp.ttl = 1h;

}

}

Once these changes are made, save them, and you have successfully configured Varnish to work with WordPress.

Summing Up

If you have access to your server, you can enhance your website’s performance by implementing a reverse proxy cache for your WordPress. Or you can also buy web hosting solutions with a built-in Varnish configuration. Many companies provide hosting in India with such easy integrations and configurations for Linux Hosting. Hope this blog helps you install Varnish Cache on your server with ease.