Set default TTL in Varnish 4.0

varnish

I am new to Varnish, and I am running v4.0 on Debian Wheezy.

I would like to set a default TTL across my cache of 4 weeks (very static content).

From reading the docs I think the answer is to set a default_ttl option somewhere in my VCL file. I have searched the docs but can only find one reference to it.

I've found this question but I think the answer must be out of date, because it doesn't work for me.

Could someone clarify how to do this in Varnish 4.0?

UPDATE: Here's my config file (the default that ships with Varnish 4.0, except that I've pointed the backend at localhost):

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.
}

Best Answer

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.

Related Topic