1

I want to specify an expiration date when setting cookies in VCL. I currently have something like this:

add resp.http.Set-Cookie = "language=" + req.http.X-Language + "; path=/";

I know that I will have to add something like this:

Expires=Thu, 01 Jan 1970 00:00:00 GMT

Is there a built-in function in Varnish that will allow me to dynamically set the expiration date to anything in the future? I've been looking at their docs but no luck so far.

Thank you very much in advance.

-Angel

angelcool.net
  • 2,505
  • 1
  • 24
  • 26

2 Answers2

2

UPDATE - Working solution:

Not sure if this syntax is specific to Fastly but I got it working using : time.add(now,1d) :

add resp.http.Set-Cookie = "language=" + req.http.X-Language + ";expires="+ time.add(now,1d) +"; path=/";
angelcool.net
  • 2,505
  • 1
  • 24
  • 26
0

If you use Varnish 4, you should use the Cookie VMOD. From the doc : https://github.com/varnish/varnish-modules/blob/master/docs/vmod_cookie.rst

format_rfc1123

STRING format_rfc1123(TIME now, DURATION timedelta)
Description
Get a RFC1123 formatted date string suitable for inclusion in a Set-Cookie response header.

Care should be taken if the response has multiple Set-Cookie headers. In that case the header vmod should be used.

Example
sub vcl_deliver {
        # Set a userid cookie on the client that lives for 5 minutes.
        set resp.http.Set-Cookie = "userid=" + req.http.userid + "; Expires=" + cookie.format_rfc1123(now, 5m) + "; httpOnly";
}
Benjamin Baumann
  • 4,035
  • 2
  • 25
  • 35