WordPress Varnish Cache Config / VCL

Note: I am now providing VCLs in separate branches on github as the Varnish VCL syntax has changed.

Thanks to scoof for documenting the Varnish VCL changes.

What is this for non-technical folks?

WordPress sucks at delivering the same content over and over again, actually, I should rephrase that, wordpress rocks 99% of the time but if you serve a page over and over again it will quickly exhaust your servers resource which will mean wordpress will go slow.  Varnish Cache speeds up WordPress by serving pages from memory instead of doinmagicg a bunch of hard work.

Why should I use this VCL?

I have done a number of different VCLs that can be used with varnish and wordpress but this is the final revision for now. It is designed for Varnish 2 but should work on 3 with no to very little modifications, I have tweaked this VCL over a few years and I’m finally happy with it.

Features:

  • Supports multiple back-ends
  • Supports round-robin
  • Supports Purging using the Varnish WordPress plugin
  • Supports logged in users
  • Supports password protected pages
  • Supports Mobile devices
  • Provides long client side caching
  • Forwards the correct client IP to the web daemon.
  • Doesn’t cache 404s, 503, 500 etc.
  • Doesn’t cache wp-admin, login, preview, signup
  • Caches static objects such as images
  • Supports Multisite
  • Includes debug/log messages.
  • Really clean code, proper tabbing etc.

Requirements

Varnish, WordPress, The Varnish WordPress plugin installed and working, mod_rpaf installed in apache or an nginx equivalent.

Why have I made this?

I was going to make a VCL generator but then I remembered most people will use this VCL as a point of reference and I’m lazy, let’s face it, being lazy is the biggest factor in me backing out of making a generator. The varnish configs I have done before have been overly verbose for what they did and rewriting them and cleaning them up means this config is much easier to understand and modify. Adding logging means that you can easily use varnishlog to debug any problems you have.

What isn’t included?

I didn’t include Custom error messages in this VCL.  It’s not because I’m lazy, it’s because custom error messages put a lot of cruft into the VCL and if you want custom error messages you should see this article.

Let me at it!

Varnish v3
Varnish v3.03
Which version of Varnish am I running?

varnishd -V

What do I need to change?

Search and replace myFirstServer and mySecondServer with your server names adding new backends where required. Make sure new backends are added to the round-robin cluster and the purge list.

Once you are happy with your VCL save it in as /etc/varnish/default.vcl (remember to make a backup of your original file) and restart Varnish.  Any problems try to debug yourself but if you are stuck just give me a shout, I will be happy to help!

19 thoughts on “WordPress Varnish Cache Config / VCL

  1. Hello,

    I’ve got an error with this vcl file :

    Message from VCC-compiler:
    Variable ‘beresp.http.magicmarker’ not accessible in method ‘vcl_recv’.
    At: (input Line 98 Pos 9)
        set beresp.http.magicmarker = “1”;
    ——–#######################——-
    Running VCC-compiler failed, exit 1
    VCL compilation failed

    In varnish documentation : (https://www.varnish-cache.org/trac/wiki/VCLExampleLongerCaching)
    set beresp.http.magicmarker = “1”;appear in sub vcl_fetch and not in sub vcl_recvwhere is my error ?

  2. The post is full of unreadable iso chars. can you please repost with proper “” & etc. plz my vps is driving me nuts with nginx and varnish and the 503 port in use restart errors this might help

  3. I am currently on holiday away from work with no access to any internet enabled devices.

    Please forward any Technical support queries to adammc@primaryt.co.uk
    Please forward any installation/projector maintinance/av queries to dally@primaryt.co.uk
    Please forward any other service queries to josh@primaryt.co.uk

    For all urgent enquiries use contact@primaryt.co.uk
    This email and its attachments may be confidential and are intended solely for the use of the individual to whom it is addressed. Any views or opinions expressed are solely those of the author and do not necessarily represent those of the organisation from which this email originated. If you are not the intended recipient of this email and its attachments, you must take no action based upon them, nor must you copy or show them to anyone. Please contact the sender if you believe you have received this email in error. This email was sent by School Email – Safe Webmail and Hosted Email for Schools

  4. done..  have contacted the blog host provider to ask them how it got so broken..  It was not broken a few weeks ago

  5. Hello, I see this was an update from your previous configuration. It is now almost a year later, and I was wondering if you have made any adjustments to your VCL and if so if you wish to post it.

  6. Hi,
    I was actually going to try your VCL-configuration in place of my own template – as your VCL is quite shorter, but some parts looked more clean.
    But Varnish wouldn’t even start with your VCL. I just tested it quickly by replacing the servers/backends, et cetera.

  7. I think I wrote it for Varnish < v3.  I prolly need to rewrite it.  Will add another version when I can.

  8. Yea Varnish keep changing their API, it’s a bit of an issue for VCL maintainers.  What error do you get?

  9. John,
    Thanks for your reply. See, I’m new to Varnish – have only heard of it, and read some of it. But I absorb information quickly and evaluate it by trial & error.However, my VCL probably has some issues because nothing in WordPress admin is saved when I browse it via Varnish.
    However, when I do make changes directly using only the httpd, changes are made (i.e I can make a post, edit a comment, or change theme settings).
    I’d be a pretty happy chap if you’d be willing to have a look at my VCL: at the moment it’s functioning OK with the only issue I described above (that I know of, so far).
    Do you have an e-mail I can reach you at? If it’s easier for you, just drop me a line at i.am@k0nsl.org

  10. I rewrote it for 3.0.3 you nee to update your Varnish to the latest and it will work. 

    Just for the sake of clarity I’m in no way affiliated with any of your websites.

  11. Yes, I have latest version now. Although I’m currently not running it because of huge consumption of memory (despite ulimit, et cetera).

    I wrote my own VCL and got it working quite well. I’m however taking a pause from this now as I have other issues to deal with.
    Yes, yes Mr. Mclear, you’re not affiliated with any of my Web Pages and I don’t think anybody would make such a connection just from me commenting on your blog. I’m not affiliated with any of your venues either, see.
    I’m just interested in Varnish, that’s why I came to visit this post.
    Just for the sake of clarity, that is!

Leave a Reply

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