Home > Varnish > Script to purge varnish cache

Script to purge varnish cache

August 15th, 2013 Leave a comment Go to comments

There is a easy way to allow developers to stop bugging us the system adminsitror, “clean the cache”.
Just setup this php page on a apache server and they can do it themselves.

Tested on Varnish 2.X using syntax:

curl -X PURGE http://localhost:81/site/bavidor/_img/*

It is also important to have the .vcl correclty configured as follows:


acl purge {
        "localhost";
        "10.51.34.99";
        "10.55.2.1";
}

sub vcl_recv {

        if (req.request == "PURGE") {
                if (!client.ip ~ purge) {
                        error 405 "Not allowed.";
                 } else {
                        purge_url(req.url);
                        error 200 "Purged";
                }
        }


here is the script:

Limpar Cache Varnish ASP


"; print "URL $URL
"; print "Examplos: /blogs/vidor/* ou /blogs/*
"; print "


"; $hostname = 'cache221'; purgeURL( $hostname, $port, $URL, $debug ); $hostname = 'cache222'; purgeURL( $hostname, $port, $URL, $debug ); function purgeURL( $hostname, $port, $purgeURL, $debug ) { $finalURL = sprintf( "http://%s:%d%s", $hostname, $port, $purgeURL ); print( "
Purging ${finalURL}

" ); $curlOptionList = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => 'PURGE', CURLOPT_HEADER => true , CURLOPT_NOBODY => true, CURLOPT_URL => $finalURL, CURLOPT_CONNECTTIMEOUT_MS => 2000 ); $fd = true; if( $debug == true ) { print "
---- Curl debug -----
"; $fd = fopen("php://output", 'w+'); $curlOptionList[CURLOPT_VERBOSE] = true; $curlOptionList[CURLOPT_STDERR] = $fd; } $curlHandler = curl_init(); curl_setopt_array( $curlHandler, $curlOptionList ); $return = curl_exec($curlHandler); if(curl_error($curlHandler)) { print "


CRITICAL - Error to connect to $hostname port $port - Error: curl_error($curl)
"; exit(2); } curl_close( $curlHandler ); if( $fd !== false ) { fclose( $fd ); } if( $debug == true ) { print "
Output:

$return


"; } } ?>
URL:

ref.
http://blog.feryn.eu/2010/11/varnish-cache-purging/

Categories: Varnish Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

VAMOVE *

*