Varnish is passing all requests to Apache

apache-2.2varnish

I'm setting up Varnish Cache 3.0.4 and Apache 2.22 on the same Xubuntu server, with Varnish accepting connections on port 80, and Apache listening on port 81. The intention is to cache .html files in Varnish, but at the moment, all requests are passed through to the server.

I set up /usr/local/etc/varnish/default.vcl as follows:

backend default {
.host = "127.0.0.1";
.port = "81";
}

sub vcl_recv {
  # Do not cache following pages (edit as needed for your configuration).
  if (!( req.url ~ "wp-(login|admin|comments-post)" )) {
      return (pass);  
  }

  # Drop the cookies for everything else
  unset req.http.cookie;

  # Try a cache-lookup
  return (hash);
}

sub vcl_backend_response {
    # Below will cache the page for one week.(1s = 1 sec, 1d = 1 day)
    set beresp.ttl = 1w;
}

I use this script to start Varnish:

#!/bin/sh
ulimit -n 10240
ulimit -l 16384
/usr/local/sbin/varnishd -f /usr/local/etc/varnish/default.vcl \
-a :80 \
-s malloc,16M -l 8m,1m,+ -u varnishd

I enabled mod headers and added this code to /etc/apache2.conf:

<FilesMatch "\.html$">
    Header set Cache-control "public, max-age=9200"
</FilesMatch>

The HTTP headers from a GET request are as follows:

HTTP/1.1 200 OK
Server: Apache/2.2.22 (Ubuntu)
Last-Modified: Thu, 10 Oct 2013 13:50:01 GMT
ETag: "e719-1884-4e8634739cce1"
Accept-Ranges: bytes
Vary: Accept-Encoding
Cache-control: public, max-age=9200
Content-Type: text/html
Date: Fri, 11 Oct 2013 17:54:29 GMT
X-Varnish: 21561442
Age: 0
Via: 1.1 varnish
Connection: close
Accept-Ranges: bytes

I ran a test using this command on another PC:

seige -d1 -c800 -t1 http://192.168.0.7/specs.html

I looked at the ouput of varnishstat which appears to show that all requests where passed to the backend. The values for MAIN.s_req and MAIN.s_pass where both 126003. Can anyone suggest why Varnish isn't caching any data?

Best Answer

Have a look at varnish example templates here: https://www.varnish-cache.org/trac/wiki/VCLExamples Your VCL is missing some key varnish functions.