Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear complete cache in Varnish?

I'm looking for a way to clear the cache for all domains and all URLs in Varnish.

Currently, I would need to issue individual commands for each URLs, for example:

curl -X PURGE http://example.com/url1
curl -X PURGE http://example.com/url1
curl -X PURGE http://subdomain.example.com/
curl -X PURGE http://subdomain.example.com/url1
// etc.

While I'm looking for a way to do something like

curl -X PURGE http://example.com/*

And that would clear all URLs under example.com, but also all URLs in sub-domains of example.com, basically all the URLs managed by Varnish.

Any idea how to achieve this?

This is my current VCL file:

vcl 4.0;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    # Command to clear the cache
    # curl -X PURGE http://example.com
    if (req.method == "PURGE") {
        return (purge);
    }
}
like image 612
laurent Avatar asked Aug 11 '16 09:08

laurent


People also ask

How do I restart Varnish Cache?

Use "systemctl start/restart/stop varnish".

What is purge from Varnish?

According to Varnish documentation, “A purge is what happens when you pick out an object from the cache and discard it along with its variants.” A Varnish purge is very similar to a Magento cache clean command (or clicking Flush Magento Cache in the Admin).

What is Varnish ban?

Banning is a concept in Varnish that allows expression-based cache invalidation. This means that you can invalidate multiple objects from the cache without the need for individual purge calls. A ban is created by adding a ban expression to the ban list.


1 Answers

With Varnish 4.0 I ended up implementing it with the ban command:

sub vcl_recv {
    # ...

    # Command to clear complete cache for all URLs and all sub-domains
    # curl -X XCGFULLBAN http://example.com
    if (req.method == "XCGFULLBAN") {
        ban("req.http.host ~ .*");
        return (synth(200, "Full cache cleared"));
    }

    # ...
}
like image 153
laurent Avatar answered Sep 17 '22 16:09

laurent