Make WordPress Faster with Varnish Cache

WordPress, Plugins and Varnish

Varnish to WP is like Christmas all year round
Varnish to WP is like Christmas all year round

My self, I don’t want to wait for website to load over 2 seconds. Standard installation of WordPress is, well, sluggish. Installing couple of plugins, nice theme and banner pictures makes it even more slower. It does not matter if your server is latest and the greatest. You can install plugin to minify some CSS and JS with Better WordPress Minify. Or you can optimize your images with EWWW Image Optimizer. Still there’s just too much going on with Worpress. All of this affect your readers – do they want to come back? Also it affects how high Google scores your site.

Varnish is an HTTP accelerator which is designed to boost up content-heavy dynamic we sites. It sits in front of your HTTP Server and caches your content automatically, almost. You can make wordpress faster and lower loads on your server. Win-Win.

Varnish has Domain Specific Language which compiles to C. You write set of rules how your content is cached and let Varnish do the heavy lifting. Current major version is Varnish 4 and VCL 4.0. If you still have 3.0, see this upgrade guide for changes to the DSL. This tutorial is using VCL 4.0.

Installing Varnish

I’m using Debian, and it’s stable release “Wheezy” has old Varnish 3. So I’m not going to use that. Instead go ahead and download latest version from the website for your OS whatever it might be.

Installing Varnish 4 to Debian:

Configuring Varnish

Go ahead and edit standard port and change it to 80. Don’t go changing your Apache port just yet. Let it run until you can make sure everything compiles ok.

If you’re not sure about your configs, you can configure Varnish to be in port 81, and test side-by-side without loosing any readers.

After that make a copy of the original VCL file:


Adding VCL Caching Policies

Add new file with the name default.vcl. See comments of default.vlc for more details how rules are working. This is bit and pieces taken from internet and combined. See definite quite from Varnish Documentation. There’s also lot’s of basic examples included in the documentation.

Default Policy


Device detection

Then device detection. This will ensure that mobile/desktop versions of your site will look as they should be. Code checks what kind of user agent browser has and sets device identification string to the headers. Then based on that string, in default.vcl, vcl_hash function generates separate cache for each device type.

This code does not include changing from mobile to desktop. That will be topic of a next blog post.

Add file called devicedetect.vcl to the same /etc/varnish directory. Original version of this can be found in GitHub, and it’s author is Lasse Karstensen <>. This is same code, but migrated to VCL 4.0.

Testing that Policies Work

If you’re doing this for the first time, you can just restart varnish to see if syntax is correct.

If you’re already running a version and you want to see if it compiles. This will output an error or C source code. After this, you can restart the server.

Configuring Apache

Edit your Apache, or favourite HTTP Server to use another port. (8080) Remember to check virtual host also configs.

Now go ahead and restart HTTP Server and Varnish. Varnish will be now serving at port 80, and Apache in 8080.  How Varnish knows where to look for HTTP Server? Well, look at the top of default.vcl.

Proof that it works

Well, you site should be blazingly fast! First load might take longer, but after that it should be rendering almost instantly. You can also investigate two headers: Age and X-Varnish. Age should be above 0 and X-Varnish should have two request id’s.


Purging Content from WordPress

Install Varnish HTTP Purge extension to your WordPress. You can purge manually or automatically when saving posts.

All you need to do is add this line to your wp-config.php file

Make WordPress Faster with Varnish Cache

Varnish Policies are in Github.

If you know how to install WordPress, you know how to install and configure Varnish Cache. You only need hour of free time and your readers will thank you.

Leave comments if you’re planning to install Varnish. Or if you have additional policies you want to share?

5 thoughts on “Make WordPress Faster with Varnish Cache

  1. Hi

    I have add some off your code butt when i try whit: cache cookies for admin site my wordpress toolbar disappear and i get it back when i delete this code:

    /* Don’t cache cookies for admin site */
    if (!(req.url ~ “wp-(login|admin)”) &&
    !(req.url ~ “&preview=true” ) ) {
    unset req.http.cookie;

    Have you notes the same thing?

    1. Hi,

      Now when you mention it. Yes, your correct. To fix this problem you need to replace that part with:

      /* Don’t cache cookies for admin site */
      if ((req.url ~ “wp-(login|admin)”) &&
      (req.url ~ “&preview=true” ) ) {

      Otherwise everything stays the same. I updated the post and Github project. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

four − 3 =