Varnish simple troubleshooting for default.vcl

Here is some simple troubleshooting for Varnish

If you want to have simple solution, cache with varnish for all anonymous users and for logged in users you disable varnish use this in your vcl

sub vcl_recv {
#check if there is session cookie from logged in user
if (!req.http.Cookie ~ "SESS|SSESS|NO_CACHE") {
         unset req.http.Cookie;
    }
# Cache things with these extensions whether we are logged in or not, we don't care
if (req.url ~ "\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$") {
    return (lookup);
}
}

This will remove cookies from all pages where there is no session cookie (cookie for logged in users) and when there are no cookies, varnish runs the page from its cache. Be aware, that this does remove all cookies from request, so if you need some working cookies for anonymous users, you can't use this and will have to do reverse process of sanitanization of req.http.Cookie.

Also a note for Drupal 6 users, session cookie is different there, and you can't use varnish without pressflow, so adding to above is a must SESS|SSESS|NO_CACHE|VARNISH|DRUPAL_UID|LOGGED_IN not sure if all o them are needed, for DRUPAL_UID I am sure, but found somewhere that others could also be needed, so just add them for safety.

If you have error in your default.vcl file

you will probably get a line like
Reloading HTTP accelerator: varnishdCommand failed with error code 106 use this line to point where is the problem varnishd -Cf /etc/varnish/default.vcl and you will get detailed description of the problem, cool.

If you are suspecting you have a cookie that is making a problem

for varnish and that i the reason you are not getting your page cached. Then backup your default.vcl and add this code to it to strip all cookies and see if Varnish is working or not and then you can pin point your problem to cookies.

#this is a backend setting, add yours here
backend default {
    .host = "127.0.0.1";
    .port = "8082";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
}
sub vcl_recv {
  # Do not cache these paths.
 if (req.url ~ "^/status\.php$" ||
      req.url ~ "^/update\.php$" ||
      req.url ~ "^/admin$" ||
      req.url ~ "^/admin/.*$" ||
      req.url ~ "^/flag/.*$" ||
      req.url ~ "^.*/ajax/.*$" ||
      req.url ~ "^.*/ahah/.*$") {
      return (pass);
  }

# Drop any cookies sent to Drupal.
sub vcl_recv {
if (!(req.url ~ "install\.php|update\.php|cron\.php|admin\.php")) {
		unset req.http.cookie;
	}
}

# Drop any cookies Drupal tries to send back to the client. This will make it unpossible to login, unless you filter it a bit better that it is made below
sub vcl_fetch {
if (!(req.url ~ "install\.php|update\.php|cron\.php|admin\.php")) {
		unset beresp.http.set-cookie;
	}
}

If you want to get in headers information if your varnish made a hit or a miss

, add this code to default.vcl

# Set a header to track a cache HIT/MISS.
sub vcl_deliver {
  if (obj.hits > 0) {
    set resp.http.X-Varnish-Cache = "HIT";
  }
  else {
    set resp.http.X-Varnish-Cache = "MISS";
  }
}

You can check if it is hit or miss in Chrome or FF, just open Page Inspector, and go to Network tab and inspect html headers, you should be able to find it, there you will see
X-Varnish-Cache HIT information if Varnish is caching that HTML file. This is only for HTML file for images, css, js you need to check them, but most probably this type of files will not make a problem as usual default.vcl file just strips cookies from them. HTML file is different as you need to be careful there if you need to have some user actions going on and page not to be cached (with images, css, js you will probably not have any user interaction and custom settings per user).

To get Most important statistics

you should write varnishstat and you will have whole bunch of them, but you want to see Cache hits and Client requests received this is basicly how many requests were sent (html, jpg, css, js ...) and how many of them got hit by varnish. The more the merrier would people say. For all other data and advancned things check doc here
https://www.varnish-cache.org/trac/wiki/StatsExplained

Testing Varnish with curl

Open up a terminal and type in the following command. Using the -I (uppercase i) flag that prints out only the HTTP headers.

Using curl you can then verify that you have all the correct headers displaying for your site.
$ curl -I http://drupaldump.com/

Finding out what cookies are present

You could write some module and output it through JS console or to screen, but there is no need. Just open page inspector and go to network or just find Cookies Tab and you will have a full list of Cookies that are present. So then you need to inspect your cookie sanitization if you have it in .vcl file and see if some cookie is passing it and you have to add some more cleenup.