【问题标题】:Varnish 3 - how to set maximum age in http headersVarnish 3 - 如何在 http 标头中设置最大年龄
【发布时间】:2012-10-03 13:47:37
【问题描述】:

我正在使用 Varnish 3.0.3,并通过在静态资源的 HTTP 标头中设置最大期限来利用浏览器缓存。我尝试将以下配置添加到 default.vcl:

sub vcl_fetch {
  if (beresp.cacheable) {
    /* Remove Expires from backend, it's not long enough */
    unset beresp.http.expires;

    /* Set the clients TTL on this object */
    set beresp.http.cache-control = "max-age=900";

    /* Set how long Varnish will keep it */
    set beresp.ttl = 1w;

    /* marker for vcl_deliver to reset Age: */
    set beresp.http.magicmarker = "1";
  }
}

sub vcl_deliver {
  if (resp.http.magicmarker) {
    /* Remove the magic marker */
    unset resp.http.magicmarker;

    /* By definition we have a fresh object */
    set resp.http.age = "0";
  }
}

这是从 https://www.varnish-cache.org/trac/wiki/VCLExampleLongerCaching 复制而来的。也许我只是打错了。重新启动 Varnish 后,它不再起作用。

我有两个问题。这是为 Varnish 3 做的正确方法吗?如果是这样,我做错了什么?其次,有没有办法在重启之前测试 Varnish 配置文件?与 Apache 的“/sbin/service httpd configtest”类似。在上线之前发现错误。谢谢。

【问题讨论】:

    标签: varnish varnish-vcl


    【解决方案1】:

    是的,通常这是覆盖后端 TTL 的方式。 删除 beresp.http.expires,设置 beresp.http.cache-control,设置 beresp.ttl。 beresp.cacheable 是一个 2.[01]-ism。 3.0 中的相同测试是检查 beresp.ttl > 0。

    一个小技巧是将你的魔法标记存储在 req.http 上,然后你不必在将对象交给客户端之前清理它。

    关于测试配置文件,你可以直接调用VCL编译器,例如“varnishd -C -f /etc/varnish/default.vcl”。如果您的 VCL 有故障,您会收到错误消息,如果 VCL 有效,您会收到几页生成的 C 代码。

    【讨论】:

      猜你喜欢
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      • 2016-08-04
      • 2016-08-08
      • 2016-05-11
      • 2011-10-13
      相关资源
      最近更新 更多