Varnish + Wordpress + Nginx - 防止 no-store no-cache must-revalidate headers

Varnish + Wordpress + Nginx - Prevent no-store no-cache must-revalidate headers

我们大约一周前启动了一个网络应用程序,经历了一个沉重的负载高峰并且停机了将近 2 个小时。我不会点名提及这家公司,但我们依靠他们的建议来防止这件事发生。

他们说因为我们使用了 Varnish,所以我们可以很轻松地应对流量的涌入。但是,我们没有验证缓存是否按预期工作。不是。


TLDR: 我们的网络应用正在发送 Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 headers 请求,但没有说明为什么会这样。

我在哪里可以防止这些 headers 被发送?

PHP: 5.6 Nginx:1.4.6 清漆:1.1 WordPress:4.6.12 木材:1.2.4

与我们合作的 linux 管理员说他们搜索了配置,但没有找到任何指定 headers 的东西,除了 AJAX 请求。

#dont cache ajax requests

if(req.http.X-Requested-With == "XMLHttpRequest" || req.url ~ "nocache" || req.url ~ "(control.php|wp-comments-post.php|wp-login.php|bb-login.php|bb-reset-password.php|register.php)")

当我们在网站上强制使用 HTTPS(force-https 插件)后将 Varnish 正确配置为缓存时,这是 Pre-launch 的卷曲:

$ curl -Ik -H'X-Forwarded-Proto: *************
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Content-Type: text/html; charset=UTF-8
Vary: Accept-Encoding
X-Server: *****
Date: Sat, 03 Nov 2018 22:36:43 GMT
X-Varnish: 53061104
Age: 0
Via: 1.1 varnish
Connection: keep-alive

从 post 开始:

curl -ILk ***********
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
X-Varnish: 691817320
Vary: Accept-Encoding
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Content-Type: text/html; charset=UTF-8
X-Server: ****
Date: Mon, 19 Nov 2018 19:17:02 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Transfer-Encoding: chunked
Accept-Ranges: bytes
Via: 1.1 varnish
Connection: Keep-Alive
Set-Cookie: X-Mapping-fjhppofk=33C486CB71216B67C5C5AB8F5E63769E; path=/
Age: 0

Force-https 插件:我们激活了它,更新了 Varnish 配置以避免重定向循环,并确认它在发布前一周工作。

插件:这些没有改变,除了 force-https。

Web 应用程序:它是之前应用程序的更新版本,完全重新设计,但据我所知,应用程序中没有任何内容指定要发送 no-store no-cache headers。

我应该从哪里开始?谢谢!

发送这些 headers 的是 PHP 引擎。

只要您启动 session,它就会这样做,这显然是基于 Set-Cookie 存在。

确保 PHP session 仅在绝对需要时启动。默认情况下,当响应包含 Set-Cookie 或 "negative" Cache-Control 时,Varnish 不会缓存,你有两者。

所以摆脱无关的 session_start() and/or setcookie() 调用是这里的关键。

您可以找到更多信息,了解何时可以 anti-caching headers 发送 here

您需要修复后端,但至少,您可以使用此 vcl 代码段去除恼人的 header、and/or 在 varnish 中绕过它;

sub vcl_backend_response {
    # kill the CC header so that application downstream don't see it
    unset req.http.Cache-Control;

    # alternatively, you can also override that header with
    # set req.http.Cache-Control = "whatever string you desire";

    # you can also force the TTL
    beresp.ttl;

    # also, if you return now, the builtin.vcl (https://github.com/varnishcache/varnish-cache/blob/master/bin/varnishd/builtin.vcl)
    # doesn't get executed; this is generally the one deciding content is uncacheable
    return (deliver);
}