Amit
Amit

Reputation: 4000

varnish vcl purging cache selectively

So i am using varnish for the first time. i have spent quite some time reading on how it works, but i am unable to figure out how do i selectively purge the cache.

like say i have a url like this

/?account=123&url=google.com

and another like

/?account=123&url=stackoverflow.com

I need to purge the cache where account=123.

I can only figure out that issuing purge on

/?account=123&url=stackoverflow.com

will only purge the cache where the url matches the incoming url with PURGE method.

Any help is appreciated.

Upvotes: 4

Views: 7317

Answers (4)

Artur Barseghyan
Artur Barseghyan

Reputation: 14242

I'm no Varnish expert, but this is what worked for me:

acl purge {
  "localhost";
  "10.1.1.0";
}

sub vcl_recv {
  if (req.http.X-Purge-Regex-URL) {
    if (!client.ip ~ purge) {
      error 405 "Not allowed.";
    }
    ban_url(req.url);
    #error 200 "Banned URL";
  }
}

Note, that I commented out the error 200 "Banner URL" - which in my my case was causing the problem.

Upvotes: 0

Ketola
Ketola

Reputation: 2767

We faced a similar problem and ended up using a separate X-Purge-Regex-URL request header for purging content using HTTP requests.

Here's the VCL we are using to accomplish this:

acl purge {
  "localhost";
  "10.1.1.0"/28;
}

sub vcl_recv {
  if (req.http.X-Purge-Regex-URL) {
    if (!client.ip ~ purge) {
      error 405 "Not allowed.";
    }
    ban_url(req.url);
    error 200 "Banned URL";
  }
}

In your case we could purge the content using curl -H "X-Purge-Regex-URL: True" --head "http://my.host.addr/?account=123"

Do note that ban_url() doesn't care about HTTP host. It purges all the matching URLs from all the hosts. If this isn't something you want you should use ban() to ban the content by using req.http.host and req.url.

Upvotes: 3

Amit
Amit

Reputation: 4000

So this is what works. In varnish 3 selective purge is called ban. so you need to use

ban("obj.http.x-url ~ " + req.url);

Upvotes: 4

Łukasz Rysiak
Łukasz Rysiak

Reputation: 3078

HTTP PURGE methos allows you to purge specific url.

to purge using regexp you have to use telnet connection to varnish admin port or use command line tool varnishadm.

usually you can do so by

telnet localhost 6082

by using telnet, after connecting to varnish server you can do purges with purge command:

purge req.url ~ /?account=123

above will purge every url matching "/?account=123"

if you want to purge specific page like you do with HTTP PURGE request, you have to use double equal sign instead of tilde (~)

purge req.url == /?account=123

you can also purge whole domain with

purge req.http.host == yourdomain.com

or specific page on your domain:

purge req.http.host == yourdomain.com && req.url ~ /?account=123

Upvotes: 1

Related Questions