5
votes

Varnish 2.1: I'm trying to use http PURGE to get Varnish to renew one cached URL.

While testing, I would get results that seemed strange until I read this bit in the documentation: "If there are several variants of the same URL in the cache however, only the matching variant will be purged. To purge a gzip variant of the same page the request would have to look like this:"

So now if I do these two commands:

curl -I http://example.com/my-url
curl -X PURGE http://example.com/my-url

I get back a 200 Purged response.

And if I do these two:

curl -I http://example.com/my-url -H "Accept-Encoding: gzip"
curl -X PURGE http://example.com/my-url -H "Accept-Encoding: gzip"

I also get a 200 Purged.

Now if I try:

curl -I http://example.com/my-url -H "Accept-Encoding: deflate"

I get back headers that indicate that the Content-Encoding: is "text/html;charset=utf-8", and it has age in the cache. That seems to indicate that "deflate" isn't being supported by my app, which is probably correct.

The page also has an Age: header indicating that it's being cached.

Now how do I purge that? (Am I asking a moot question?)

All of these attempts return "404 Not in cache":

curl -X PURGE http://example.com/my-url -H "Accept-Encoding: deflate"
curl -X PURGE http://example.com/my-url -H "Accept-Encoding: text/html;charset=utf-8"
curl -X PURGE http://example.com/my-url -H "Accept-Encoding: text/html"

Is there a magic way to purge all of the variants of a URL?

2
Does your VCL have entries for both vlc_hit and vlc_miss?favoretti
Umm, let me rephrase, pardon me asking an improper question. I meant to ask whether it has purge entries for both hit and miss.favoretti
Yes, and I can see them both executing correctly in the first and second examples. I've seen both Purged and "Not in cache" results from those routines on the PURGE call.Mojo
Out of curiosity.. What would you get if you try curl -I http://example.com/my-url -H "Accept-Encoding: gzip", followed by curl -X PURGE http://example.com/my-url? I'm not sure why it doesn't work in your example, other than response cached wasn't a page, but from what I understand, just purging a url should purge all instances of it. That being said, my varnish experience is rather non-extensive.favoretti
That test returns "404 not in cache". If I repeat the purge and add -H "Accept-Encoding: gzip" it returns "200 Purged." That's pretty much the gist of my problem -- how to purge all variations of a cached page.Mojo

2 Answers

7
votes

Aha, by applying some google-fu, I stumbled on a forum post, that suggests the following:

purge("req.url ~ ^" req.url "$"); 

i.e. using a regexp behind purge, to handle all Accept-Encoding headers.

More info here: http://www.gossamer-threads.com/lists/varnish/misc/15124

.. which means you need to modify your VCL though.

3
votes

I think this is what u need:

acl purge {
        "localhost";
        "192.168.55.0"/24;
}

sub vcl_recv {
        # allow PURGE from localhost and 192.168.55...

        if (req.request == "PURGE") {
                if (!client.ip ~ purge) {
                        error 405 "Not allowed.";
                }
                return (lookup);
        }
}

sub vcl_hit {
        if (req.request == "PURGE") {
                purge;
                error 200 "Purged.";
        }
}

sub vcl_miss {
        if (req.request == "PURGE") {
                purge;
                error 200 "Purged.";
        }
}