Varnish 4 不支持 Cache-Control:must-revalidate

Varnish 4 does not honor Cache-Control: must-revalidate

我正在尝试让 Varnish 与 last-modified headers 一起工作,但无论我做什么,我的页面都会在 120 秒内被缓存,并且 Varnish 永远不会与后端重新验证。

我的后端正在发送这些 headers :

Cache-Control: must-revalidate, proxy-revalidate, public, stale-while-revalidate=0
Last-Modified: Fri, 22 Jan 2016 03:32:33 GMT

当我记录 object 命中的 TTL 时,它的值始终设置为 120s。

我正在使用 Varnish 4 的默认 VCL 配置。

此致,


编辑:经过一番搜索,我发现120s是varnish的默认ttl值。但是他为什么无视last-modified?

设置 Cache-Control 的 "s-maxage" 或 "max-age" 属性 header:

beresp.ttl is initialized with the first value it finds among:

The s-maxage variable in the Cache-Control response header field
The max-age variable in the Cache-Control response header field
The Expires response header field
The default_ttl parameter.

参见:http://book.varnish-software.com/4.0/chapters/VCL_Built_in_Subroutines.html#the-initial-value-of-beresp-ttl

我从varnish邮件列表得到了答复,为了模拟"must-revalidate"header,必须添加这段VCL:

sub vcl_backend_response {
    if (beresp.http.cache-control ~ "must-revalidate") {
        set beresp.ttl = 1s;
        set beresp.grace = 0s;
        set beresp.keep = 1w;
    }
}

它只适用于 Varnish 4。

我引用 1s ttl 的原因:

This way, you'd only cache for 1 second (don't set it to 0, or all the requests for this object would be done sequentially), but will keep the object for a week, revalidating it each time it is requested and its ttl is expired.