7

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.
}
Richard
  • 253
  • 2
  • 5
  • 11
  • 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 07 '15 at 12:11
  • possible duplicate of [Varnish Cache - default TTL?](http://serverfault.com/questions/389202/varnish-cache-default-ttl) – MDMoore313 Apr 07 '15 at 12:11
  • 1
    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 07 '15 at 16:11
  • 1
    Also, how is this question not a clear problem statement? – Richard Apr 07 '15 at 16:11
  • your *question* is a duplicate, it doesn't matter if the answers helped you or not. Unfortunately your question or the former question wasn't tagged w/ the specific version, but that can be changed. – MDMoore313 Apr 07 '15 at 16:14
  • The question is not a duplicate. The question is about Varnish 4.0 and is explicitly flagged as such. The older question is about an older version of Varnish. The older answers do not apply to this question. – Richard Apr 07 '15 at 16:15
  • It's like saying "this question was answered for Chrome 1.0, so you cannot ask it for Chrome 32". It's just not helpful for users and it's not helpful for the site. – Richard Apr 07 '15 at 16:16
  • try adding your config file to this question and it can be voted on to reopen. – MDMoore313 Apr 07 '15 at 16:16
  • 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 07 '15 at 17:00

2 Answers2

3

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.

liquidity
  • 408
  • 1
  • 7
  • 22
  • 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 07 '18 at 23:09
1

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