0

I have not tried this before on Varnish. And I must admit that my last experience with Varnish was way back - version 2.1.5 days!

I have set varnish 6.x as a simple/basic static http-cache. I have achieved most of what I wish to from this varnish, and then I had an idea to display cache-ttl info as response header. Here, I hit a roadblock. I am surely doing it incorrectly. Possibly vcl_delivery may not get beresp.ttl. I am not sure. This is what I added to vcl_deliver

 set resp.http.X-TTL = "TTL (" + beresp.ttl +")";

I get compile error:

Mar 19 07:05:41  varnishd[55516]: Message from VCC-compiler:
Mar 19 07:05:41  varnishd[55516]: ('/etc/varnish/default.vcl' Line 97 Pos 40) -- (Pos 49)
Mar 19 07:05:41  varnishd[55516]:     set resp.http.X-TTL = "TTL (" + beresp.ttl +")";
Mar 19 07:05:41  varnishd[55516]: ---------------------------------------##########------
Mar 19 07:05:41  varnishd[55516]: Not available in method 'vcl_deliver'.

I tried assigning beresp.ttl as header variable in vcl_backend_response:

set beresp.http.x-ttl = beresp.ttl

That did not work either.

Makes me wonder, if this is possible? If so, how to achieve it?

anup
  • 717
  • 4
  • 9
  • 19

1 Answers1

1

Since Varnish 4, backend & client logic have been split up in separate threads. This split is also reflected in VCL.

At the client side of Varnish you have objects like req and resp to access request and response information. When you have to access the backend, the req object gets used to build the bereq object and the response is stored in the beresp object.

As you have noticed the beresp context is not available in vcl_deliver, because the delivery stage happens at the client side.

That's why it's better to set the X-TTL header at the backend side.

Here's the VCL code to do it:

sub vcl_backend_response {
    set beresp.http.X-TTL = "TTL (" + beresp.ttl +")";
}
Thijs Feryn
  • 1,166
  • 4
  • 5
  • I tried that in a more direct way, as mentioned in the Question. So, what I gather from your reply is that we cannot use 'beresp' in vcl_deliver. In other words, there is no way to expose a set TTL for a URL in its response header? – anup Mar 22 '21 at 10:47
  • Oh! I misunderstood it. I do not need to set it on vcl_deliver. I guess I missed seeing it in response when I set it up before. Thanks for pointing that out. – anup Mar 22 '21 at 10:52