Varnish: Clearing your ESI cache

After setting up Varnish ESI caching, we can clear the cache when required. The example below written in Python shows you how to purge the ESI fragments.

In the VCL file, you'll have to add this somewhere at the top of the file. I put it under the backend declarations.

# Allow PURGE requests from the following web servers
acl purge_acl {
"yourhost.com.au";
"anotherserver.com.au";
}

And under "sub vcl_recv", add:

# Allow PURGE requests from our web servers
if (req.request == "PURGE") {
if (!client.ip ~ purge_acl) {
error 405 "Not allowed";
}

return(lookup);
}

Lastly, add in:

sub vcl_hit {
# Clear the cache if a PURGE has been requested
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
}

Now you can send PURGE requests to Varnish. When varnish detects a purge command, it'll clear the ESI cache for the given fragment.

For the following code, you'll have to send the full URL to the site's URL. It can definitely be improved, but this should be enough for you to get started.

from urlparse import urlparse
from httplib import HTTPConnection

def esi_purge_url(url):
"""
Clears the request for the given URL.
Uses a HTTP PURGE to perform this action.
The URL is run through urlparse and must point to the
varnish instance, not the varnishadm

@param url: Complete with http://domain.com/absolute/path/
"""
url = urlparse(url)
connection = HTTPConnection(url.hostname, url.port or 80)

path_bits = []
path_bits.append(url.path or '/')

if url.query:
path_bits.append('?')
path_bits.append(url.query)

connection.request('PURGE', ''.join(path_bits), '', {'Host': url.hostname})

response = connection.getresponse()

if response.status != 200:
print 'Purge failed with status: %s' % response.status

return response

Varnish also allows some sort of regex/wildcard purging, but I haven't implemented this yet. If you need mass purging, this post point you in the right direction.

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog