在Varnish 4.0中设置默认TTL?

我是Varnish的新手,我在Debian Wheezy上运行v4.0。

我想设置一个默认的TTL跨4周的caching(非常静态的内容)。

从阅读文档,我认为答案是在我的VCL文件的某处设置一个default_ttl选项。 我已经search了文档,但只能find一个参考 。

我发现这个问题,但我认为答案必须是过时的,因为它不适合我。

有人可以澄清如何在Varnish 4.0中做到这一点?

更新:这是我的configuration文件(除了我指出在本地主机的后端除了Varnish 4.0的默认):

 backend default { .host = "127.0.0.1"; .port = "8080"; } sub vcl_backend_fetch { set obj.ttl = 4w; } sub vcl_recv { # Happens before we check if we have this in cache already. # # Typically you clean up the request here, removing cookies you don't need, # rewriting the request, etc. } sub vcl_backend_response { # Happens after we have read the response headers from the backend. # # Here you clean the response headers, removing silly Set-Cookie headers # and other mistakes your backend does. } sub vcl_deliver { # Happens when we have all the pieces we need, and are about to send the # response to the client. # # You can do accounting or modifying the final object here. } 

default_ttl是一个运行时参数 。 你可以在启动varnishd时进行设置。

default_ttl

单位:秒默认值:120.000最小值:0.000标志:如果后端和VCL代码都不分配,则分配给对象的TTL。

你可以用2种不同的方式设置这个参数。 无论你select哪种方式,都会做同样的事情。

你可以使用快捷键-t

-t ttl指定caching文档的最短生存时间。 这是指定default_ttl运行时参数的快捷方式。

或者 ,您可以使用-p param=value

例如,你可以像这样开始清漆:

使用快捷键: varnishd -a 127.0.0.1:8081 -T 127.0.0.1 -t 2419200

varnishd -a 127.0.0.1:8081 -T 127.0.0.1 -p default_ttl=2419200

2419200的数字是4个星期。

被接受的答案是实现目标的一种方式。 但是,事实上,您可以在/etc/varnish/default.vcl文件中完美设置默认TTL,如下所示:

 sub vcl_backend_response { set beresp.ttl = 4w; }