varnish purge hack

Do you have the problem that you want to ban/purge elements on your site using both absolute and relative matching?

Let say your vcl contains ban("req.url ~ (?i)" + req.url);, what happens if you purge the root / is that it will purge
your whole site since you are matching with ~. But if you would match with == instead you would have to use absolute urls for every single element you want to purge. Cumbersome, right?

But if you add the following to vcl_recv:

if (req.request == "BAN") {
    ban("req.url ~ (?i)" + req.url + "$");
    error 200 "Banned.";

The magic here is the "$" at the end. Its simple but you can now purge / and ONLY purge the root.
But this opens up for the possibility to purge /.*css and invalidate all your stylesheets.
It's simple regex, but sometimes we fail to see the simple solutions.

Nice huh?