使用cURL PHP与命令行cURL进行清漆缓存清除

时间:2014-01-07 22:28:37

标签: php caching curl varnish

我们目前在服务器上有多个虚拟主机。我试图通过PHP执行缓存清除,不涉及exec命令。我可以通过发出以下命令行命令清除Varnish Cache中的单个文件:

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

我正在尝试将此命令复制到PHP。我已经生成了以下内容:

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

我发布了/test.html

我收到以下回复:

Error 200 Purged.

Purged.

Guru Meditation:

XID: 1728013370

Varnish cache server

但是文件没有清除。有什么想法吗?

以下是请求的清漆日志:

    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

1 个答案:

答案 0 :(得分:0)

您没有发出正确的“Host:”HTTP标头,因此我认为以下某行存在问题:

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

curl_setopt($curl, CURLOPT_ENCODING, $varnishhost);