[英]PURGE fails in Varnish cache
I am using the following code to PURGE the homepage of a site: 我使用以下代码来清理网站的主页:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.mysite.com:8080/");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PURGE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 1000);
$r = curl_exec($ch);
echo "<PRE>$r</PRE>";
curl_close($ch);
The response from Varnish is as expected: Varnish的回应如预期:
HTTP/1.1 200 Purged.
Server: Varnish
Content-Type: text/html; charset=utf-8
Retry-After: 5
Content-Length: 382
Accept-Ranges: bytes
Date: Fri, 10 Aug 2012 10:50:56 GMT
X-Varnish: 617777456
Age: 0
Via: 1.1 varnish
Connection: close
X-Cache: MISS
So now I think that it is purged, but a further call to the page to check the headers suggest that it is not purged. 所以现在我认为它已被清除,但是进一步调用页面检查标题表明它没有被清除。 As
Age: 15
and X-Cache: HIT
are both set, suggesting that the page is still cached and is 15 seconds old. 由于
Age: 15
和X-Cache: HIT
都已设置,表明该页面仍然被缓存并且是15秒。
The TTL is 120. TTL是120。
Am I missing something? 我错过了什么吗?
Thanks Jake 谢谢杰克
To eliminate all other php/curl issues I would start with the most basic low-level check. 为了消除所有其他php / curl问题,我将从最基本的低级检查开始。
This works for me: 这对我有用:
netcat -C varnish_hostname 80 <<EOF
PURGE /the/url
Host: hostname
EOF
Once you get this to work, you know that your VCL rules and ACLs are not an issue and you may proceed to the curl/php level. 一旦你开始工作,你知道你的VCL规则和ACL不是问题,你可以进入curl / php级别。
EDIT Two notes: 编辑两个说明:
netcat -C
option is there to convert newlines to CRLF char pairs per the HTTP protocol. netcat -C
选项用于根据HTTP协议将换行符转换为CRLF字符对。 I think you would not have checked for purging in the default.vcl The default.vcl should contain something like following: 我认为你不会检查default.vcl中的清除default.vcl应该包含如下内容:
acl purge {
"localhost";
"192.168.55.0"/24;
}
sub vcl_recv {
# allow PURGE from localhost and 192.168.55...
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return (lookup);
}
}
sub vcl_hit {
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
}
sub vcl_miss {
if (req.request == "PURGE") {
error 200 "OK but nothing to purge - URL was not in cache";
}
}
I wanted to comment that you should post your default.vcl file, but my reputation is too low. 我想评论你应该发布你的default.vcl文件,但我的声誉太低了。
PURGE is implemented in your vcl file (or at least it should if you expect it to work), and what it does and does not is there on code. PURGE是在您的vcl文件中实现的(或者至少应该如果您希望它能够工作),以及它在代码中的作用和不作用。 It could be blocking your PURGE based on the ip address from where you are connecting, or it could be not implemented.
它可能会根据您连接的IP地址阻止您的PURGE,或者可能无法实现。
Just to know, PURGE works on the beReq. 要知道,PURGE在beReq上工作。
If you modify the req.url (ie add prefix or a suffix to your URI), you should do the same in the vcl_recv that calls purge. 如果修改req.url(即为URI添加前缀或后缀),则应在调用purge的vcl_recv中执行相同操作。 Alternatively, you have to write the purge condition inside the main vcl_recv after the url manipulation.
或者,您必须在url操作后在主vcl_recv中写入清除条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.