简体   繁体   English

PURGE在Varnish缓存中失败

[英]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: 15X-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
  • Replace varnish_hostname by your actual varnish hostname. 用您的实际varnish主机名替换varnish_hostname。
  • Replace 80 by the actual port varnish listens on. 用实际的端口清漆监听替换80
  • Replace /the/url by the path part of the url to purge url的路径部分替换为/ url以进行清除
  • Replace hostname by the hostname part in the URL you want to purge. 主机名替换为要清除的URL中的主机名部分。

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: 编辑两个说明:

  1. you may have to type the above fast to prevent the connection from closing prematurely because many http stacks these days don't allow a long delay between the connection establishment and the HTTP request to prevent half-open DoS attacks. 您可能必须快速键入以上内容以防止连接过早关闭,因为现在许多http堆栈不允许在连接建立和HTTP请求之间存在长时间延迟以防止半开DoS攻击。 You can achieve this by preparing the whole input ahead of time, copy it to your paste buffer, and pasting the whole blurb with one click of the mouse. 您可以通过提前准备整个输入,将其复制到粘贴缓冲区,并通过单击鼠标粘贴整个blurb来实现此目的。
  2. The HTTP request header has to end with a double new line as shown above. HTTP请求标头必须以双新行结束,如上所示。 The 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.

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