简体   繁体   English

Varnish 3 - 如何在http标头中设置最大年龄

[英]Varnish 3 - how to set maximum age in http headers

I am using Varnish 3.0.3 and to use it to leverage browser caching by setting a maximum age in the HTTP headers for static resources. 我正在使用Varnish 3.0.3并通过在HTTP标头中为静态资源设置最大年龄来使用它来利用浏览器缓存。 I tried adding the following configuration to default.vcl: 我尝试将以下配置添加到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";
  }
}

This is copied from https://www.varnish-cache.org/trac/wiki/VCLExampleLongerCaching . 这是从https://www.varnish-cache.org/trac/wiki/VCLExampleLongerCaching复制的。 Maybe I just made a typo. 也许我刚写了一个错字。 On restart of Varnish, it no longer worked. 重新启动Varnish后,它不再有效。

I have two questions. 我有两个问题。 Is this the correct way to do it for Varnish 3? 这是为Varnish 3做正确的方法吗? If so, what am I doing wrong? 如果是这样,我做错了什么? Secondly, is there a way to test the Varnish configuration file, before a restart? 其次,有没有办法在重启之前测试Varnish配置文件? Something along the ways of what Apache has with "/sbin/service httpd configtest". Apache与“/ sbin / service httpd configtest”的方式有关。 That catches mistakes before going live. 在上线之前捕获错误。 Thank you. 谢谢。

Yes, in general this is the way of overriding the backend's TTL. 是的,一般来说这是覆盖后端TTL的方法。 Remove beresp.http.expires, set beresp.http.cache-control, set beresp.ttl. 删除beresp.http.expires,设置beresp.http.cache-control,设置beresp.ttl。 beresp.cacheable is a 2.[01]-ism. beresp.cacheable是2. [01] -ism。 The same test in 3.0 is to check that beresp.ttl > 0. 3.0中的相同测试是检查beresp.ttl> 0。

A small tip is to store your magic marker on req.http instead, then you don't have to clean it up before handing the object to the client. 一个小技巧是将您的魔术标记存储在req.http上,然后在将对象交给客户端之前不必清理它。

With regards to testing a configuration file, you can call the VCL compiler directly with "varnishd -C -f /etc/varnish/default.vcl" for example. 关于测试配置文件,您可以直接使用“varnishd -C -f /etc/varnish/default.vcl”调用VCL编译器。 If your VCL is faulty you get the error message, if the VCL is valid you get a few pages with generated C code. 如果您的VCL出现故障,则会收到错误消息,如果VCL有效,您将获得一些生成C代码的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM