Varnish ban query string

varnish

I'm trying to setup ban in Varnish 4.
I have this in vcl_recv :

    ban("req.http.host == " +req.http.host+" && req.url ~ "+req.url);
    return(synth(200, "Ban added"));

When I make a request, it does say ban added, and I do see it un the ban.list with varnishadm :

1499676469.672070     0    req.http.host == something.com && req.url ~ /some/path?q=*

Except it doesn't work, nothing gets invalidated.
If I try to ban path* it seems to apply to "regular" files like path.css for example, but it looks like it never invalidates URLs based on query string.
Is there something else I need to do to get it to consider the query string ?
The query strings are pretty unreadable, full of % codes, if that matters.

Thanks

Best Answer

I think you're looking for PURGE instead. It will be more effective, and will invalidate whatever URL you request in PURGE request. In vcl_recv, add:

if (req.method == "PURGE") {
    # If not allowed then a error 405 is returned
    if (!client.ip ~ purge) {
        return(synth(405, "This IP is not allowed to send PURGE requests."));
    }
    return (purge);
}

Next, invalidate a page from cache:

curl -X PURGE http://127.0.0.1/your-super-request-with-params -H "Host: example.com"

If you really want BANs instead (do you really have to?), then don't use REGEX matching (~) to invalidate specific URLs, since it will more than likely interpret ? and other query parameters as engine flags. So:

ban("req.http.host == " + req.http.host + " && req.url = " + req.url);
return(synth(200, "Ban added"));
Related Topic