如何在PHP脚本中禁用Varnish缓存


How to disable Varnish Cache from within a PHP script?

我分发了一个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脚本中禁用varnish。这似乎。。好愚蠢的但似乎符合我的经验。

那么,有没有一种方法可以从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;
        }
}

您不能在PHP中禁用Varnish,但有一个技巧可以让Varnish忽略当前页面。Varnish不会缓存您设置cookie的页面,所以无论何时您希望Varnish不缓存某个页面,都可以编写以下代码:

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

不是最理想的解决方案,但效果很好。。。

您可以禁用Varnish缓存,使用以下命令创建.htaccess文件:

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

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

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

据我所知,您只能清除缓存:

https://www.varnish-software.com/static/book/Cache_invalidation.html