Posted on

Varnish Monitoring and Configuration

Varnish Configuration

Graphical representation of the varnish VCL state machine:
State Machine

Set the caching time of Varnish different than for clients

VCL Caching

sub vcl_fetch {
   if (beresp.ttl > 0s) {
      /* Remove Expires from backend, it's not long enough */
      unset beresp.http.expires;

      /* Set the clients TTL on this object */
      set beresp.http.cache-control = "max-age=900";

      /* Set how long Varnish will keep it */
      set beresp.ttl = 1w;

      /* marker for vcl_deliver to reset Age: */
      set beresp.http.magicmarker = "1";
   }
}

sub vcl_deliver {
   if (resp.http.magicmarker) {
      /* Remove the magic marker */
      unset resp.http.magicmarker;

      /* By definition we have a fresh object */
      set resp.http.age = "0";
   }
}

Removing all except some cookies

sub vcl_recv {
if (req.http.Cookie) {
    set req.http.Cookie = ";" + req.http.Cookie;
    set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
    set req.http.Cookie = regsuball(req.http.Cookie, ";(COOKIE1|COOKIE2)=", "; \1=");
    set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

    if (req.http.Cookie == "") {
        remove req.http.Cookie;
    }
}

Creating a varnish hash which ignores subdomains

sub vcl_hash {
   hash_data( req.url );
   # changing hash so subdomains are ignored
   # only if subdomains run the same site
   if ( req.http.host ) {
      hash_data( regsub( req.http.host, "^([^\.]+\.)+([a-z]+)$", "\1\2" ) );
   } else {
      hash_data( server.ip );
   }
   return (hash);
}

Monitoring Varnish

Using “varnishtop”

# Display request cookies.
varnishtop -i RxHeader -I Cookie

# Display varnish hash data ('search' is text to filter within hash).
varnishtop -i "Hash" -I search

# Display 404s.
varnishlog -b -m "RxStatus:404"

# Get the age
varnishlog -i TxHeader -I ^Age

# Display a ranking of all cache missed (passing to the backend)
varnishtop -b -i TxURL

# top IP addresses  
varnishtop -i TxHeader -I '^X-Forwarded-For:'  

# which pages the IP address 120.130.140.150 is hitting  
varnishncsa |grep 120.130.140.150 |awk '{print $7}'

Using “varnishhist”

The history shows the response times for hits(|) and misses(#) in varnish. The x-axis is the time in seconds it takes for varnish to deliver the reply (1e0 = 1s, 1e-3 = 1ms). More can be found What does varnishhist tells us?