23

I can't see a similar question, but apologies if I'm duping.

We're running a varnish cache on our system, but want to install a system where we can purge individual pages when they are edited (fairly normal). We've been trying to get it to work by using an HTTP header. So, our VCL is set up like:

acl purge {
      "localhost";
#### Our server IP #####
}

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

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

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

However, I'm stuck on how to actually SEND the http purge request. We're using PHP for the website, so I've tried using:

header("PL: PURGE / HTTP/1.0");
header("Host: url to purge");

But this doesn't seem to do anything (and varnishlog doesn't seem to show anything purging).

I've also experimented with cURL but, again, it doesn't seem to be working. Am I missing something really basic here, or is the basis sound, meaning my implementation is bugged?

Many thanks,

nish
  • 6,952
  • 18
  • 74
  • 128
flukeflume
  • 707
  • 1
  • 6
  • 14

4 Answers4

36

You need to go and make an HTTP request.

Untested, but should be along the lines of (if you want to use curl as you mentioned):

$curl = curl_init("http://your.varnish.cache/url-to-purge");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PURGE");
curl_exec($curl);
Jeremy Roman
  • 16,137
  • 1
  • 43
  • 44
  • 2
    Yup, PURGE is an HTTP request just like GET or POST -- though it postdates RFC 2616, but it is used by content caches and proxies such as Squid, Varnish and Apache Traffic Server. A quick google of "PURGE curl_setopt" threw up [this example](http://www.rwahyudi.com/linux/purge-squid-cache-using-curl-without-squidclient/). Enjoy :) – TerryE Feb 09 '12 at 18:17
  • 1
    Or for more ad-hoc or script-driven approaches, you could also use `curl -XPURGE "http://your.varnish.cache/url-to-purge"` – Cameron Kerr Jun 10 '15 at 19:21
5

Quick, dirty and effective way to send a PURGE request:

curl -v -k -X PURGE URL

You can actually make a little script with that statement, as follows:

1) Open an editor, in example VI

 vi varnish_purge_url.sh

2) Enter the following text:

#!/bin/sh

curl -v -k -X PURGE $1

(remember to leave the blank line between the first and last lines).

3) Save the file and exit. Then set the appropriate attributes to execute it from the shell:

chmod 750 varnish_purge_url.sh

4) You want to be root when creating and using the above script. If using Ubuntu, feel free to add sudo in front of commands when required.

5) Usage is simple:

./varnish_purge_url.sh URL

Where URL is the URL to purge.

Dario Fumagalli
  • 1,064
  • 1
  • 16
  • 23
4

You can also purge using command line. Use the command sudo varnishadm. This will open the Varnish Command-line Interface. where you can type in your command to purge the pages as per your need. E.g to purge your home page, do this:

root@staging:/etc/varnish# sudo varnishadm
200        
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux,3.5.0-28-generic,x86_64,-sfile,-smalloc,-hcritbit

Type 'help' for command list.
Type 'quit' to close CLI session.

varnish> ban.url ^/$
200  
nish
  • 6,952
  • 18
  • 74
  • 128
  • 1
    No, you can not purge from varnishadm - only ban as in your example. Ban and purge are not the same functionality even if the end result is similar. – Clarence Feb 06 '15 at 11:33
0

Maybe I'm on a newer version but what's above didn't work for me. This did:

sub vcl_hit {
  if (req.request == "PURGE") {
    ban("req.url ~ "+req.url);
    error 200 "Purged.";
  }
}
JaseC
  • 3,103
  • 2
  • 21
  • 22