如何在PHP脚本中禁用Varnish Cache?

时间:2014-09-21 00:40:27

标签: php varnish

我分发了一个PHP脚本,最近很多人在共享主机帐户上遇到了清漆缓存问题。

这是PHP脚本顶部的代码。但是我仍然在响应头中获得“Varnish:HIT”(并且脚本无法正常工作)。

header('Pragma: no-cache');
header('Cache-Control: private, no-cache, no-store, max-age=0, must-revalidate, proxy-revalidate');
header('Expires: Tue, 04 Sep 2012 05:32:29 GMT');

一家托管服务提供商表示,即使通过设置上述缓存标头,也无法在PHP脚本中禁用清漆。这似乎......嗯..傻?但似乎符合我的经验。

那么有没有办法在PHP中禁用/跳过清漆?或者varnish(默认情况下)是否忽略PHP设置的这些缓存头?


谢谢Jens-AndréKoch - 我将包含清漆说明和PHP脚本,以使其忽略无缓存响应:

sub vcl_fetch {
        if (beresp.http.cache-control ~ "(no-cache|private)" ||
            beresp.http.pragma ~ "no-cache") {
                set beresp.ttl = 0s;
        }
}

4 个答案:

答案 0 :(得分:4)

您无法在PHP中禁用Varnish,但您可以使用一种技巧使Varnish忽略当前页面。 Varnish不会缓存您设置cookie的页面,所以每当您希望Varnish不缓存某个页面时,您可以编写这段代码:

setcookie('xx', microtime(true), time()+600, '/');

不是最理想的解决方案,但它的工作正常......

答案 1 :(得分:3)

您可以使用以下内容禁用Varnish缓存创建.htaccess文件:

Header set Cache-Control "max-age=0, private, no-cache, no-store, must-revalidate"

答案 2 :(得分:0)

答案 3 :(得分:0)

您必须配置Va​​rnish以避免缓存页面。 修改您的VCL以检测标题... 指定您自己的标头以关闭文件的缓存,或者将其静态添加为未缓存到配置中。

https://www.varnish-software.com/static/book/build/exercises/complete-avoid_caching_a_page.html?highlight=headers