Varnish need 2 know

Some of the stuff I ran into fine-tuning my varnish configuration. Pay attention, Im getting my hands dirty here.

Prerequisites Varnish server Web server with pressflow(!!)

site Varnish module enabled varnish key entered in the drupal varnish settings

This is achieved by copy paste-ing the key from [varnish]


to the drupal settings.

varnish console open to webserver

this is achieved by editing [varnish] /etc/default/varnish and change the -T from -T localhost:6082 \ to -T :6082 \

This way the varnish admin console is open to the network

Carefull however, if you're in the dmz you would want to allow only webserver access to this port.

Debugging Is best done by keeping one eye open via varnishlog -b It will show you only the request to the backend, only the stuff that takes time. Starting with a VCL For starters, try someone's vcl.

Improving the VCL

Some improvements to increase cache hits: Reduce caches for different browser header versions.

if (req.http.user-agent ~ "MSIE") {

set req.http.user-agent = "MSIE";

} else {

set req.http.user-agent = "Mozilla";


Add the ip address so the site knows the users IP address.
This is not always needed and does not improve performance

set req.http.X-Forwarded-For = client.ip;

This kind of rewrites the request so only the non prefixed request will be cached.

if( ~ "^(www)?"){

set = "";


if ( ~ "$") {

set req.backend = mysite;



Cookies for google analitics

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

clean up cookie code.

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

Remove empty cookies.

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

Other cookie stuff.

I needed to add the following cookies to my varnish configuration.
I did not find them anywhere else, but if you're using varnish module and admin toolbar, this is the way to go.
The following will pass the request (cause the request to go to backend) when the following cookies are in the request.

if (req.http.Cookie ~ "(VARNISH|DRUPAL_UID|LOGGED_IN|NO_CACHE|DrupalAdminToolbar|CUSTOMCOOKIE)") {

return (pass);


Cache invalidation

I need the cache to be invalidated on some specific form submit (for my front page). This is how it's done


if (module_exists('varnish')) {

module_load_include('module', 'varnish');

_varnish_terminal_run('url.purge ' . $base_url);



It is of course required if your front page will be altered by backend settings.

Remarks, improvements, questions? Let me know via twitter : drupal_sensei