Varnish HTTP Purge Warmer WordPress Plugin

August 8th, 2016 Leave a comment Go to comments

TL;DR - This little plugin should make it super quick and simple to recreate the cached URL when used in conjunction with the Varnish HTTP Purge plugin. Scroll to the bottom for the link.

WordPress is the bane of my existence. While it may be the darling with its ubiquity and ease-of-use (we'll call it) - under the hood it is a mess, the plugin and theme ecosystem is like the Wild West and in opinion it does more harm than good.

I've spent the larger portion of the past two days trying to get a site to perform well on a very cheap hosting setup. I have my normal desired setup (which I will explain at another time) but this one was on some very small budget constraints. I finally took the dive and setup Varnish, typically avoided because I can leverage CloudFlare to do the same, and I found it somewhat bleeding edge to actually adopt properly.

First off, there's lots of blog posts about Varnish + WordPress. But most are still referencing Varnish 3.x, and even the ones referencing 4.x are not very complete and actually have some wrong assumptions. Varnish 4.x no longer lets you PURGE by wildcard, they recommend using "Bans" - which is not accurately reflected in any of the example VCL files I could find. I did find a snippet that finally got me over that issue and teamed it up with the rest of my other stuff.

The Varnish HTTP Purge plugin out of the box will work properly with it (just make sure to define VHP_VARNISH_IP properly, if not running on localhost:80) however I wanted to also prewarm that cache - instead of generating it at runtime. I leveraged the "after_purge_url" hook to just send a short-lived HTTP request back out to each URL it was told to purge, hoping to trigger the work ahead of time so the URL is both purged and reloaded at roughly the same time.

Also, with the VCL file I've been maturing, the "purge entire site" capability actually works (without the "ban" support, it won't work and you'll have a false sense of security.)

Since someone else may benefit from all this headache, I'm posting both the VCL and the plugin (if you can even call it that at three lines of actual PHP code) up here for people to enjoy.

You can grab the plugin (or just the VCL if you want) on GitHub. Feel free to send PRs.