2

I consider starting using Varnish on my websites. I just tried out Varnish and I am wondering how to cache pages even if I my websites uses cookies, for Google Analytics. I am trying to remove them but it seems like Varnish isn't caching. This is how my config looks like; http://pastie.org/1254664. If it matters I have one Debian server, and one server using Ubuntu Server. So, how do I cache the website even if I am using cookies?

Thank you in advance!

Addition: I don't get any X-Cache: HIT/MISS neither. What am I doing wrong?

Erik
  • 125
  • 1
  • 3

6 Answers6

2

I am fairly new to caching with Varnish, but here is what I've learned so far: There are several factors to consider when using Varnish for caching against an application.

In your case, know what cookies are being set and for what purpose. If varnish sees a cookie with your request, you will be passed to the backend, resulting in a cache miss.

Google Analytics Cookies

If you are using Google Analytics cookies, you can safely unset them in Varnish; don't worry, you will still the data in your GA reports. Use something like this in your vcl_recv

set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|__utma_a2a)=[^;]*", "");

You can try a couple more cleanup lines, also in the vcl_recv

Remove ";" prefixes from Cookies

set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");

Unset empty cookies

if (req.http.Cookie ~ "^\s*$") {
  unset req.http.Cookie;
}

Application Specific Cookies

If your application sets a cookie when a user logs in to perform a function, those requests should not be cached and sent to the backend directly. Otherwise, you could cached pages viewed by logged in users (bad).

Use something like this:

if (req.http.Authorization || req.http.Cookie) {
  return (pass);
}

HTH & good luck.

Edit

Use this to see what cookies varnish is seeing come through:

varnishtop -i RxHeader -I Cookie

If you're regex misses any, catch 'em here!

KM.
  • 1,746
  • 2
  • 18
  • 31
0

I don't know about Varnish. But according to my experience in server administration, try those stuffs in your LAN first, than move on to the Internet.

jack
  • 167
  • 1
  • 7
0

That's what the Vary-header in http is for: variations that should return the same resource. Varnish understands a Vary: cookies.

If you are not getting X-Cache headers you meight not be connecting to the cache at all; are you sure it's running on the right ip/port?

Joris
  • 5,939
  • 1
  • 15
  • 13
  • 1
    I added Cookies to the Vary-header and checked that Varnish is running. http://pastie.org/1254829 – Erik Oct 28 '10 at 07:23
  • Varnish does not set the X-Cache headers by default, it does set the X-Forwarded-For to the backends, and a varnish and Age header to the clients. – arjarj Oct 30 '11 at 07:52
0

You may pass some parts of you site to backend without caching.

Simplest example for vcl_recv and vcl_fetch blocks:

sub vcl_recv {
if (req.url ~ "^/somescript.php") {                       
return (pass);                                                          
}                                                                       
else {                                                                  
unset req.http.Cookie;                                                  
}
return(lookup);
}

sub vcl_fetch {
if (req.url ~ "^/somescript.php") {                       
return (pass);                                                          
}                                                                       
else {                                                                  
unset req.http.Cookie;                                                  
}
return(deliver);
}

Read - read this to understand why Varnish misses

ghloogh
  • 1,039
  • 5
  • 9
  • Not even the second example of code (in the linked page) worked. – Erik Oct 28 '10 at 08:43
  • what version of Varnish do you use? This code is for 2.1.x versions – ghloogh Oct 28 '10 at 09:18
  • Oh, it seems like I am using varnish-2.0.4. How do I update? apt-get update; apt-get upgrade didn't give me any newer. – Erik Oct 28 '10 at 20:04
  • Add a Varnish repo http://www.varnish-cache.org/installation/debian to your sources.list and upgrade it from there – ghloogh Oct 29 '10 at 10:12
0

run varnishlog and see if you are actually going through varnish

Mike
  • 21,910
  • 7
  • 55
  • 79
0

My recommendation is to check for which URLs you really need cookies (e.g. administration panel) and to remove cookies for all other URLs. See below an example:

sub vcl_recv {
  if (req.url ~ "^/admin/") {
    # administration panel
    set req.http.admin = 1;
  } else {
    # public web site, ignore client request for fresh content, remove cookies
    unset req.http.Cache-Control;
    unset req.http.Max-Age;
    unset req.http.Pragma;
    unset req.http.Cookie;
  }
  ...
}

sub vcl_fetch {
  if (req.http.admin == 1) {
    # administration panel
    return (hit_for_pass);
  } else {
    # public web site, not allowed to set cookies
    unset beresp.http.Set-Cookie;
    ...
  }
  ...
}
Jens Bradler
  • 6,133
  • 2
  • 16
  • 13