Varnish purge using HTTP and REGEX

MatthiasLaug picture MatthiasLaug · Jun 20, 2012 · Viewed 38.8k times · Source

I want to purge Elements of my varnish using HTTP. This http call is triggered from a backend server behind the varnish itself, so the backend server has not other access but HTTP.

I have implemented the following purging rules with the according ACL which work fine for

curl -X PURGE http://www.example.com/image/123/photo-100-150.jpg

but I want to be able to purge an URL via HTTP using Regex

curl -X PURGE http://www.example.com/image/123/*.jpg

That way I want to clear all scaled version of this image once a new has been uploaded. Is there a way?

Answer

mk_ picture mk_ · Jul 6, 2012

try this:

if varnish 3.0 and up.

vcl_recv {
    if (req.request == "PURGE") {
             if (!client.ip ~purge){
                     error 405 "Not allowed";
             }
     ban("req.http.host == " +req.http.host+" && req.url ~ "+req.url);
     error 200 "Ban added";

    }