How to Purge/Ban An Entire Domain in Varnish Cache 3.

Ban is a bit different from Purge. Banned objects remain in memory, but banning is faster than purging(which you do one by one).   It does not actually remove the object, just bans it from being used. Like sweeping it under the rug.  Here is the official Varnish 3 Documentation  https://www.varnish-cache.org/docs/3.0/tutorial/purging.html.

Purge may be more of use for your use-case; the examples I will give work, however they aren’t precisely the best way of doing it.

BAN:
First the purge method used to be just called ‘purge’ in Varnish 2.x, but they renamed it to ‘ban’  in Varnish 3.0.  Just to throw you off they made a new tool called ‘purge’ that is less sophisticated than ‘ban’ as it only purges exactly matched items.  More or less one thing at a time.

If you want to use the new purge, you need to have some specific VCL setup see here:
https://www.varnish-cache.org/docs/2.1/tutorial/purging.html#http-purges.

In Varnish 2.x I would have used a cURL request:
curl -X PURGE www.mydomain.com

However in Varnish 3.x this purges only the home page default document itself from that domain.
In order to purge the whole domain we need to use ‘ban’.

A common use case for this is if you have a high Varnish TTL and just uploaded new content.  Purging will give you that new content. However, it will not stop new content from being cached or being served. Just bans content already cached. Once the TTL expires on the banned cache, list is cleared.
Casual Warnings:
Don’t purge an entire site during high traffic, this could cause high load on the server. (‘ban’ lets you purge selectively as well).

You can’t flush public or user caches, this doesn’t negate the need to increment filenames on any assets with long expiry time. (and / or managed public cache-control headers carefully).

How to:
There are  several ways to get this done, the simplest way is to use varnishadm, which you can go into and contr5ol manually, or run on the command line by passing the required command to it.

Here is an example:

sudo varnishadm "ban req.http.host ~ www.mydomain.com" 
 
You can also go more complicated and tell it to for instance ban all .css files: 
sudo varnishadm "ban req.http.host ~ www.mydomain.com && req.url ~ .css" 


Afterwords we can test it out:

 varnishadm "ban.list"
Output:
Present bans:
1454538178.153226 0 req.http.host ~ example.comTips & Warnings

jim has written 83 articles

One thought on “How to Purge/Ban An Entire Domain in Varnish Cache 3.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>