如何从 PHP 脚本中禁用清漆缓存? [英] How to disable Varnish Cache from within a PHP script?

查看:16
本文介绍了如何从 PHP 脚本中禁用清漆缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I distribute a PHP script and recently a lot of people are having trouble with varnish cache on shared hosting accounts.

这是 PHP 脚本顶部的代码.但是我仍然在响应标头中收到Varnish:HIT"(并且脚本无法正常工作).

This is the code at the top of the PHP script. However I still get "Varnish: HIT" in response headers (and the script doesn't work correctly).

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.这似乎……好吧……愚蠢?但似乎符合我的经验.

One hosting provider said it was impossible to disable varnish from within a PHP script, even by setting the cache headers like above. This seems .. well .. silly? But seems to match my experience.

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

So is there a way to disable/skip varnish from within PHP? Or does varnish (by default) just ignore these cache headers set by PHP?

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

Thanks Jens-André Koch - I'll include varnish instructions along with the PHP script to make it ignore no-cache responses:

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

推荐答案

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

You have to configure Varnish to avoid caching the page. Modify your VCL to detect the headers... specify your own header to turn caching off for your file or add it statically as non-cached to the config.

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

这篇关于如何从 PHP 脚本中禁用清漆缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆