How to Use Varnish With WordPress

Posted on July 5, 2020 by in WordPress | 12 comments

How to Use Varnish With WordPress

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 (VCL). In practice, VCL 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 VCL 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 VCL 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.vcl, 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

Premade Layouts

Check Out These Related Posts

Best Practices to Optimize Your Anchor Text

Best Practices to Optimize Your Anchor Text

Posted on August 3, 2020 by in WordPress

Even if you aren’t familiar with the phrase anchor text, you have almost certainly used it. Anchor text is the actual text you click on for a link, whether it is click here, check this out, or a keyword that indicates that you’re navigating to a list of the best code editors...

View Full Post
Unsplash WordPress Plugin Overview and Review

Unsplash WordPress Plugin Overview and Review

Posted on July 28, 2020 by in WordPress

Unsplash is one of the most popular sources for free images on the net. Unsplash has millions of professional quality images that are free to use in your commercial and non-commercial projects with or without attribution. This makes Unsplash popular with WordPress blogs and websites. Unsplash has...

View Full Post

12 Comments

  1. I love elegant theme. really this post helps me a lot. Thanks.

    • Happy to hear it, Sandeep!

  2. Nice one, am going to give this a try on my website and see how the performance will be

    • Thanks, Jude! Good luck.

  3. What if you’re already using a CDN like cloudflare? Can u still benefit from using a reverse proxy or would that defeat the purpose?

    • Hi Joseph! Cloudflare already acts like a reverse proxy in many ways, including caching copies of your website to lessen your server’s workload. If you’re happy with the CDN and your website’s performance, we’d recommend against tinkering with your server’s configuration for now.

  4. Well detailed article.
    I want to know, can we install varnish only with plugins.
    (Is there also a separate plugins to use Varnish) please reply.

    • Hi Sachin! I’m glad you enjoyed the article. To answer your question, it depends on your web host – if it comes with Varnish pre-installed, you can use a plugin to configure its settings. However, most web hosts don’t offer Varnish out of the box, so you’ll have to set up its packages manually using the command line. I’d check with your hosting provider, but for most shared plans, you can assume there’s no Varnish support.

  5. We’ve used several WordPress cache plugins before. But later we realized that using multiply cache plugins causes a lot of use of resources and in general reduces the page speed of our site. That’s why I recommend using a good comprehensive cache plugin like Varnish.

    • Thanks for your comment! Using multiple caching plugins can definitely lead to some performance issues – glad to hear Varnish has been a good solution for you.

  6. You mention Varnish Configuration Language (VCL) a few times, but then call it VLC every time. Just thought you’d like to know 😉

Leave A Reply

Comments are reviewed and must adhere to our comments policy.

Join To Download Today

Pin It on Pinterest