Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

6 Answers 6

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!

share|improve this answer

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.

share|improve this answer

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?

share|improve this answer
    
I added Cookies to the Vary-header and checked that Varnish is running. pastie.org/1254829 –  Erik Oct 28 '10 at 7: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 7:52

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

share|improve this answer
    
Not even the second example of code (in the linked page) worked. –  Erik Oct 28 '10 at 8:43
    
what version of Varnish do you use? This code is for 2.1.x versions –  ghloogh Oct 28 '10 at 9: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 varnish-cache.org/installation/debian to your sources.list and upgrade it from there –  ghloogh Oct 29 '10 at 10:12

run varnishlog and see if you are actually going through varnish

share|improve this answer

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;
    ...
  }
  ...
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.