This article was originally published on this site


Slow-loading websites are a pain to use, and they can cost you users. Plus, there’s no excuse for a sluggish website considering how many ways there are to speed up performance, including caching.

There are a lot of ways to implement caching, as well as tools that can help you get the job done. In this article, we’ll talk about how reverse proxies work, and we’ll teach you how to use Varnish in WordPress.

Let’s get to it!

What Is a Reverse Proxy (And Why Should You Use One)?

Proxies are also known as ‘intermediary’ servers. In most cases, those servers are placed between users and the rest of the web. The proxy filters user requests, following a specific set of rules.

To give you an example, your Internet Service Provider (ISP) or workplace might use a proxy to block you from accessing specific websites. You might also use a proxy server to protect your privacy, encrypt web requests, or bypass restrictions on the web.

‘Reverse’ proxies, on the other hand, are placed between the internet and your web server. Their job is to filter incoming requests. Here are some of the benefits of using a reverse proxy for your website:

  • Load balancing. If you have multiple web servers, the reverse proxy can route requests depending on each computer’s load.
  • Enhanced privacy. Using a reverse proxy hides your end server’s information. If someone is snooping around, they’ll only see as far as the reverse proxy.
  • Managing multiple Secure Sockets Layer (SSL) certificates. Your reverse proxy can handle incoming HTTP requests, and get the un-encrypted data required from your end server.
  • Caching purposes. A reverse proxy can cache your website’s static and dynamic content, thereby reducing the load on your server.

To sum it up, reverse proxies are pretty cool. If your primary concern is increasing your WordPress website’s performance, caching is one of the best tools at your disposal doing doing so.

There are several ways to take advantage of caching, however. You can rely on your user’s browsers to store data, or use a Content Delivery Network (CDN) to manage caching for you. Alternately, you can implement a reverse proxy cache. That’s precisely what Varnish can do for WordPress users.

An Introduction to Varnish for WordPress

The Varnish website.

Varnish is an open-source full-page caching tool, which can work as a reverse proxy. That means it can act as an intermediary between end users and your website. Every time someone visits the site, Varnish will check its own cache to see if it has a copy of the page they want to see. If it doesn’t, it will fetch that page and then store it for later use.

In practice, Varnish can drastically increase your server’s performance. That’s because it’s far easier to serve cached content than it is to render that content from scratch. By using Varnish for WordPress, you can save copies of all the assets on your website that don’t change very often. If you purge or renew that cache periodically, it should deliver a pixel-perfect rendering of your website under most circumstances.

As we mentioned earlier, proxy servers follow specific rules when they handle requests. Varnish uses its own language for internal rules, called Varnish Configuration Language (VLC). In practice, VLC gives you full control over your proxy server’s configuration, so you’ll need to become acquainted with it first.

You can also extend Varnish’s core functionality using ‘VMODs’, which are modules that enhance the tool. The official Varnish website maintains a repository of popular VMODS, so feel free to check it out as well.

For Varnish to work, you’ll have to install the tool directly on your server. That means you’ll need access to the command line, which rules out those on shared hosting plans. If you’re using a Virtual Private Server (VPS), a cloud environment, or even a dedicated server, however, read on to learn how to set up Varnish in WordPress.

How to Set Up Varnish for WordPress (In 3 Steps)

Throughout this tutorial, you’ll be making several changes at the server level. If you’re uncomfortable using the command line or modifying your server, you may want to read through all of the instructions carefully before getting started. In addition, it’s a good idea to make a backup of your website first, just in case.

Step 1: Install Varnish on Your Server

To install Varnish, you’ll need to access your server through the command line. Then you’ll have to install the tool, tweak its VLC file, and configure your server to work with it.

For this step, go ahead and check out Vanish’s official installation instructions for UNIX-based servers. That brief tutorial includes all the commands you’ll need to use in the correct order.

Once you install Varnish and configure your web server to work alongside it, remember to restart everything. Then you’re ready to fine-tune WordPress to work alongside your new tool.

Step 2: Set Up a Proxy Cache Purge Plugin

Ideally, you want to configure Varnish to purge its cache at predetermined times. That way, it won’t serve visitors outdated versions of your content.

The best way to do this in WordPress is to purge the reverse proxy cache every time you update a page or a post (or create a new one). As you might imagine, there’s a plugin that can do this for you, called Proxy Cache Purge:

The Proxy Cache Purge plugin.

We like this plugin because it doesn’t purge the entire cache when you publish or edit a page or post. Instead, it only targets the caches for your front page, the edited content, and associated taxonomies. The only time when the plugin does purge the entire reverse proxy cache automatically is when you change themes.

Proxy Cache Purge doesn’t need any additional configuration, but you do have to enable custom permalinks for the plugin to work properly. If you’re not already using a custom permalink structure (which you should be!), check our guide on how to set one up. Any structure will do, so feel free to choose your favorite.

Step 3: Configure Your Varnish Settings for WordPress

If you followed the official setup instructions for Varnish, you’ll end up with a configuration file that works for WordPress but isn’t optimized for the platform.

To optimize Varnish for WordPress, we recommend making three small changes to your VLC file:

  1. Configure Varnish to ignore specific cookies.
  2. Exclude your WordPress admin and login pages.
  3. Extend the reverse proxy cache duration.

You can do this by opening the Varnish configuration file using a text editor via the command line. The file you’re looking for is default.vcl, and it should be located here:

/etc/varnish/default.vcl

Open the file and add the following code snippet to it, which comes courtesy of the official Varnish documentation:

#unsetting wordpress cookies
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;
}
}

Save the changes to default.vlc, and you’re good to go. Your reverse proxy server is ready to work with WordPress. Now Varnish won’t contact your server every two minutes (which is the default), but once every hour instead.

Conclusion

If you have full access to your server, implementing a reverse proxy for WordPress is an effective way to improve loading times for users. Plus, using a reverse proxy can enable your server to handle much heavier traffic loads, since Varnish takes care of the brunt of the work for you.

You’ll need access to your server to set up Varnish for WordPress. After that, the installation and configuration only takes three steps:

  1. Install Varnish on your server.
  2. Set up a proxy cache purge plugin.
  3. Configure your Varnish settings for WordPress.

Do you have any questions about how to use Varnish with WordPress? Let’s talk about them in the comments section below!

Article thumbnail image by Studio_G / shutterstock.com