5

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.
}
  • 1
    Welcome to ServerFault. This question will likely be closed as a duplicate, since the same question already exists. I know that the current answers haven't helped you, so I suggest placing a bounty on the original question to bring more attention to it, and possibly receive more recent answers. Also, if you find the answer elsewhere, you can add that as an answer to the original question of course. – MDMoore313 Apr 7 '15 at 12:11
  • possible duplicate of Varnish Cache - default TTL? – MDMoore313 Apr 7 '15 at 12:11
  • Er, it's not a duplicate. I explicitly linked to that question in my original question and said the answer doesn't apply to 4.0. How can that be a duplicate? Or is SF now so keen on closing questions down that you can only ask a question once for one version of a tool, and never again get answers for any other version of a tool? :( – Richard Apr 7 '15 at 16:11
  • Also, how is this question not a clear problem statement? – Richard Apr 7 '15 at 16:11
  • 1
    There's no difference in the syntax, and the other answers appear to work just fine in Varnish 4.0. From the information you've given us, it appears that you haven't actually tried them -- or anything else! – Michael Hampton Apr 7 '15 at 17:00
2

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.

  • Note you can set this in your DAEMON_OPTS config (e.g., in /etc/varnish/varnish.params) which does the same as above, same format, e.g., DAEMON_OPTS="-p default_ttl=3600" – ldg May 7 '18 at 23:09
0

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;
}
  • This answer is at best incomplete: using this strategy will cache all response codes, including 5xx statuses, which is hardly desirable :( – T. Fabre Feb 5 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.