Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a purge request in varnish

Tags:

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,

like image 494
flukeflume Avatar asked Feb 09 '12 17:02

flukeflume


People also ask

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).

How do I purge Varnish cache using command line?

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.


1 Answers

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); 
like image 116
Jeremy Roman Avatar answered Oct 25 '22 11:10

Jeremy Roman