[Varnish 4] How to purge based on regex

Viktor Gunnarson viktor.gunnarson at ericsson.com
Tue Oct 28 12:18:25 CET 2014


Hi,

I wonder how I can make Varnish 4 purge based on a regex? I've found old examples on how to do this in previous versions of Varnish but no example for Varnish 4.

What I want to accomplish is basically this:

I send a PURGE request:

PURGE /api/news/ HTTP/1.1

This should then purge /api/news/ but also all url's with query parameters (such as /api/news/?lang=en). I have tried the following without any success:

sub vcl_recv {
    if (req.method == "PURGE") {
        return (purge);
    }
}

sub vcl_hash {
    if (req.method == "PURGE") {

        #Remove ? and everything after that
        set req.http.purge-url = regsub(req.url, "^([/a-zA-Z0-9]*)([\?=&a-zA-Z0-9]*)", "\1" );

        #Make sure the url ends with a /
        if(!req.http.purge-url ~ "[.]*/$"){
            set req.http.purge-url = regsub(req.http.purge-url, "$", "/");
        }

        #Make it a regex
        set req.http.purge-url = "^" + req.http.purge-url + ".*$";

        hash_data(req.http.purge-url);

        return (lookup);
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20141028/43e06d27/attachment.html>


More information about the varnish-misc mailing list