简体   繁体   中英

Varnish Cache Purging with cURL PHP vs. Command Line cURL

We currently have multiple virtual hosts on a server. I am trying to do a cache purge through PHP that does not involve an exec command. I am able to Purge Individual Files in the Varnish Cache by issuing the following command line command:

curl -X PURGE -H "Host: domain.com" http://127.0.0.1/test.html

I am trying to replicate this command to PHP. I have generated the following:

if (isset($_POST['PurgeURL']))
{
    $purgeurl = $_POST["PurgeURL"];
    $varnishurl = "http://127.0.0.1" . $purgeurl ;
    $varnishhost = 'Host: ' . $_SERVER['SERVER_NAME'];
    $varnishcommand = "PURGE";
    $curl = curl_init($varnishurl);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $varnishcommand);
    curl_setopt($curl, CURLOPT_ENCODING, $varnishhost);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
    $result = curl_exec($curl);
    curl_close($curl);

I am posting /test.html .

I receive the following response:

Error 200 Purged.

Purged.

Guru Meditation:

XID: 1728013370

Varnish cache server

But then the file does not purge. Any ideas?

Here is the Varnish Log for the request:

    0 Debug        - "VCL_error(200, Purged.)"
   28 SessionOpen  c 127.0.0.1 54437 :80
   28 ReqStart     c 127.0.0.1 54437 1728228985
   28 RxRequest    c PURGE
   28 RxURL        c /test.html
   28 RxProtocol   c HTTP/1.1
   28 RxHeader     c Host: 127.0.0.1
   28 RxHeader     c Accept: */*
   28 RxHeader     c Accept-Encoding: Host: domain.com
   28 VCL_call     c recv
   28 VCL_acl      c MATCH purge localhost
   28 VCL_return   c lookup
   28 VCL_call     c hash
   28 Hash         c /test.html
   28 Hash         c 127.0.0.1
   28 VCL_return   c hash
   28 VCL_call     c miss error
   28 VCL_call     c error deliver
   28 VCL_call     c deliver deliver
   28 TxProtocol   c HTTP/1.1
   28 TxStatus     c 200
   28 TxResponse   c Purged.
   28 TxHeader     c Server: Varnish
   28 TxHeader     c Content-Type: text/html; charset=utf-8
   28 TxHeader     c Retry-After: 5
   28 TxHeader     c Content-Length: 383
   28 TxHeader     c Accept-Ranges: bytes
   28 TxHeader     c Date: Wed, 08 Jan 2014 14:42:33 GMT
   28 TxHeader     c X-Varnish: 1728228985
   28 TxHeader     c Age: 0
   28 TxHeader     c Via: 1.1 varnish
   28 TxHeader     c Connection: close
   28 Length       c 383
   28 ReqEnd       c 1728228985 1389192153.441481352 1389192153.441616535 0.000058413 0.000074863 0.000060320
   28 SessionClose c error
   28 StatSess     c 127.0.0.1 54437 0 1 1 0 0 0 245 383

You're not issuing the correct "Host: " HTTP header, so I assume there is a problem with one of the following lines:

$varnishhost = 'Host: ' . $_SERVER['SERVER_NAME'];

curl_setopt($curl, CURLOPT_ENCODING, $varnishhost);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM