【问题标题】:Varnish 4 does not honor Cache-Control: must-revalidateVarnish 4 不支持 Cache-Control: must-revalidate
【发布时间】:2016-04-28 13:56:48
【问题描述】:

我正在尝试让 Varnish 使用最后修改的标头,但无论我做什么,我的页面都会在 120 秒内缓存,并且 Varnish 永远不会在后端重新验证。

我的后端正在发送这些标头:

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

当我记录命中对象的 TTL 时,它的值始终设置为 120 秒。

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

问候,


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

【问题讨论】:

    标签: varnish varnish-vcl


    【解决方案1】:

    设置Cache-Control标头的“s-maxage”或“max-age”属性:

    beresp.ttl 使用它在其中找到的第一个值进行初始化:

    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

    【讨论】:

      【解决方案2】:

      我从 Varnish 邮件列表中得到了答复,为了模拟“must-revalidate”标头,必须添加这段 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 的原因:

      这样,您只会缓存 1 秒(不要将其设置为 0,或者所有 对该对象的请求将按顺序完成),但将保持 对象一周,每次请求时重新验证它, 它的 ttl 已过期。

      【讨论】:

      • 这里有一个想法:将 'grace' 设置为 0 并将 'keep' 设置为 1 周,每个请求都会到达后端并且必须等待后端的响应。除此之外,如果后端服务器关闭,则不会提供任何内容。相反,如果您将宽限期设置为 1w,而不是在宽限期内,请求会立即得到响应,并且 Varnish 将从后端检索下一个请求的最新信息。另外,如果后端服务器关闭,缓存值仍然会被传递。
      • 源站的响应似乎表明每个请求都必须重新验证(“proxy-revalidate”和“stale-while-revalidate=0”),所以发送缓存的版本不允许。
      猜你喜欢
      • 2019-04-23
      • 2019-01-17
      • 2015-02-14
      • 2018-11-24
      • 2011-01-25
      • 2011-07-09
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多