Varnish cache responese with “Set-Cookie” header

cookieshttpvarnish

I have a page which sends a "Set-Cookie" header for something like the language depending on the URL. The thing is this page will get a fairly high hit rate but it's not an option right now to strip the "Set-Cookie" header so that Varnish 4 will cache it.

The documentation only shows how to unset beresp.http.set-cookie or that I can add the cookie to the hash with hash_data(req.http.cookie). As far as I see, adding the cookie to the hash only apply to request Cookies not to the cookies set by the backend.

I am pretty sure vmod_header could be part of the solution but how would I use it to cache a cookie that I match with beresp.http.Set-Cookie == "language=en_us; path=/; domain=mydomain.tld" and cache this response?

Best Answer

The cache key (aka the hash computed in hash_data) is created when receiving the request. The response from the server is not yet received. You cannot change it afterwards.

The two options I can see is :

  • either disabling the cache of response coming with Set-Cookie header (what I think is maybe done by defautl by varnish)
  • or list the parameters in the incoming request you use in your Cookie computation (url, host, accept-language...) and add them to the request hash using hash_data