Set default TTL in Varnish 4.0?

Solution 1:

default_ttl is a runtime parameter. You can set it when you start varnishd.

default_ttl

Units: seconds Default: 120.000 Minimum: 0.000 Flags: The TTL assigned to objects if neither the backend nor the VCL code assigns one.

You can set this parameter 2 different ways. Whichever way you choose will do the exact same thing.

You can use the shortcut -t

-t ttl Specifies a hard minimum time to live for cached documents. This is a shortcut for specifying the default_ttl run-time parameter.

or, you can use the -p param=value

So for example, you could start varnishd like this:

Using shortcut: varnishd -a 127.0.0.1:8081 -T 127.0.0.1 -t 2419200

Using longer form: varnishd -a 127.0.0.1:8081 -T 127.0.0.1 -p default_ttl=2419200

The number 2419200 is 4 weeks in seconds.

Solution 2:

The accepted answer is one way of achieving the goal. However, in fact you can perfectly set the default TTL in the /etc/varnish/default.vcl file like so:

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