Search code examples
cachingcookiesvarnishvarnish-vcl

Varnish caching for some cookies?


I've started using Varnish a few months ago and my vcl files has started to get complicated. I'm serving 5 different sites with the same varnish instance (using different backends) and this part is working.

Now I have some cases that for some sites I need to keep some cookies and cache different versions based on them. Basic scenario is to show the user a message only the first time he sees the page. Without varnish if the cookie is not there I set it and show the message. Next time the cookie is there and no message is shown.

With varnish my logic is to include the cookie in hash and cache two different versions. The challenge is to make sure cookies are set correctly and here I'm kind of stack. I've added some debug messages in my vcl and for some reason I'm not seeing the cookies I set in my server in vcl_backend_response. Inside the routine, I have the following code in the beginning:

   # AT: DEBUG
   if(bereq.http.x-at-debug == "1") {
     std.log("AT DEBUG - bereq URL: " + bereq.url + ". Cookies: '" + bereq.http.Cookie + "'");
     std.log("AT DEBUG - beresp set-cookie: '" +  beresp.http.set-cookie + "'");
   }

I set x-at-debug header in vcl_recv based on my IP, so I can track only my requests. In the server side I see the cookie "__IKUB_AL_Myikub_PopUp" being set, but in vcl_backend_response is not.

Is there anything in between that can strip the cookie?


Solution

  • The core of the problem was that beresp.http.set-cookie is returning only the first set-cookie header. If you have more you need header module from varnish modules (https://github.com/varnish/varnish-modules). Be careful, it only works against varnish 4.1.