How to Use Varnish With WordPress

Rifat WordPress Tutorials Sep 28, 2020

People don't like to use slow loading websites, having one can cost your users. No matter how informative and helpful a website you have, no excuse will be accepted for a lazy website considering how many ways there are to boost your website speed, including the caching procedure

There are a lot of ways to speed up your website and cache implementation. Also, there are a lot of tools that can help you to fix this issue. Today we will talk about how does reverse proxies work and will tell you about Varnish and teach you how to use it on WordPress.

What is a Reverse Proxy?

Proxies are also identified as 'mediator' servers. In a shot, a server that sits between the user and central web server and let user access all web content or block using some specific site with a condition of following some particular rules, that is called Proxy.

Let me give you an example, your Internet Service Provider (ISP) or office might use a proxy server to block you from reaching particular websites. You might also use a proxy server to defend your privacy, encrypt web requests, or detour restrictions on the web.

Why should we use Reverse Proxy?

Reverse proxies set between your web server and internet. They filter incoming HTTP requests. Here are some advantages of using a reverse proxy for your website.

  • Server loading balance - If you have many servers, the reverse Proxy can give direction to requests depending on the loading capacity of each computer.
  • Caching purpose - The static and dynamic contents can be cached by the reverse Proxy. So, it reduces loading time on your website.
  • Multiple SSL certificate management - It can handle incoming HTTP requests and receive information from un-decrypted data from your server.
  • Ensure better privacy - Using a reverse proxy server can hide your valuable information from the server. It reduces the vulnerability of your website and provides better security.

Ultimately, it's a great way to boost your WordPress websites performance. Speeding up is one of the best tools at your disposal doing so.

What is Varnish for WordPress?

There are a couple of ways to take advantages of caching. However, either you can depend on users browser or CDN - Content Delivery Network to control caching of your website. On the other hand, you can use a reverse proxy cache - and that is precisely what Varnish is.

The Varnish is a full-page caching tool and its completely open sourced that can work as a reverse proxy. Means, it can act as a medium between your website and users. Every time someone hits the website, Varnish checks its cache to check if it ha any copy of page or info users want to see. If it doesn't, Varnish will fetch that page and store it for further use.

Varnish can increase your servers performance because it's more convenient to display contents from cache than the scratch. By using Varnish, you can save copies of all assets that you don't usually change very often. Regularly, if you manage caches, Varnish will deliver a clean, perfect render outcome under any situation.

Varnish uses its own Varnish Configuration Language (VCL) To maintain proxy servers that follow specific rules during request handling. It gives you full control over your proxy servers when it comes to configuration. So, the better you are familiarised with it, the better result you can expect.

You can use extend the functionality of Varnish using 'VMODs', It enhanced the core functions. VMODs can be easily found in official Varnish website, feel free to check them.

You can install Varnish directly into your server. Means, you'll need to reach to the command line. If you are a VPS (Virtual Private Server) user, read the steps below to learn how to set up Varnish in WordPress.

How to Set Up Varnish for WordPress

During this lesson, we'll be making frequent changes at the server level. If you're nervous using the command line or tweaking your server, you may want to read through all of the instructions precisely before getting started. Besides, make a backup of your website first, just in case. Otherwise, if your site gets down, you won't be able to recover it.

Part 1: Install Varnish on Server

You have to access your server through the command line if you want or install Varnish. Install the tool, configure the VCL file and save it to make it work. Go to Varnish installation guidance For UNIX based server, follow that and you'll be able to install it. After installation, make sure you restart everything. Ths, ou are ready to fine-tune your website to work with your new tool.

Part 2: Set Up a Proxy Cache Purge Plugin

Ideally, you want to setup Varnish to purge its cache at planned times. That way, it won't help visitors old versions of your content.

The best way to do this in WordPress is to purge the reverse proxy cache each time you renew 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.

This is a well-liked plugin because it doesn't delete the entire cache when you publish or edit a page or post. Rather, it only targets the stocks for your front page, the edited content, and associated classifications. The only time when the plugin does purge the entire reverse proxy cache automatically is when you switch themes.

Proxy Cache Purge doesn't need any different arrangement, but you do have to allow custom permalinks for the plugin to work correctly. If you're not using a custom permalink structure that you should be, check out here.

Part 3: Configure Your Varnish Settings for WordPress

If you followed the original setup guidance for Varnish from the website, you'll get a configuration file that works for WordPress but isn't friendly with the platform.

To optimize Varnish for WordPress, we advise you to make 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 use any text editor to edit the command line of the VCL file. The changes will be happening in default.vcl and the file will be located here.

/etc/varnish/default.vcl

Open the VCL file and add the piece of code to it. Do not worry, and this was collected from Varnish official site.

unsetting wordpress cookiessub 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 urlsif (req.url ~ "wp-admin|wp-login") {return (pass);}extending caching timesub vcl_backend_response {if (beresp.ttl == 120s) {set beresp.ttl = 1h;}}

Save it, and it is done! Now you are good to go. The reverse proxy server is ready to work. Now Varnish will contact your site in every one hour, instead of every two minutes (default).

Summing-up

If you are empowered with the full access of your site, then planting a reverse proxy is a very effective way to enrich loading times for your visitors. It will help you to handle extensive traffic load and also will secure your data from unauthorized people.