Purging on PUT and DELETE

Dridi Boukelmoune dridi at varni.sh
Thu Mar 19 10:21:00 UTC 2020


On Thu, Mar 19, 2020 at 10:05 AM Martynas Jusevičius
<martynas at atomgraph.com> wrote:
>
> Hi,
>
> upon receiving a PUT or DELETE request, I'd like Varnish to invalidate
> the current object (and its variants) *and* to pass the request to the
> backend.
>
> Essentially the same question as here:
> https://serverfault.com/questions/399814/varnish-purge-on-post-or-put
> The answer seems outdated though.

I would do it like this:

> sub vcl_backend_response {
>     if (beresp.status == 200 && bereq.method ~ "PUT|DELETE") {
>         ban("req.url == " + bereq.url + " && req.http.host == " + bereq.http.host);
>     }
> }

Or at least, I would do it in vcl_backend_response, there's no point
in invalidating if the client wasn't allowed to change a resource for
example.

> I consider this a common use case for REST CRUD APIs, so I was
> surprised not to find a single VCL example mentioning it.

The problem is that so many things can go wrong. For example my
snippet doesn't allow the ban to be processed in the background, so
further adjustments are needed to make that happen. It also assumes
that bereq's URL and Host are identical to req's, and isn't subject to
client noise (spurious query parameters and whatnots).

So indeed, I wouldn't want to advertise that kind of snippet without a
heavy supply of red tape.


Dridi


More information about the varnish-misc mailing list