Varnish

Share on FriendFeed
Posted by marco
Wed, 28 Oct 2009 11:31:00 GMT

Varnish is a reverse proxy, and is a very good one. You can reduce effectively the load on your web servers, or deploy a load-balancer, and it has a very sophisticated configuration.

However, with the default configuration you could have a small cache hits/misses ratio. With a few modifications, you can improve the cache hit. I think that these modifications are general enough, so I share them here. Mostly are based on what you find in this blog post and in the Varnish FAQ

The only functions I modified with respect to the default configuration are vclrecv, vclpipe and vcl_fetch:

backend lighttpd {
.host = "::1";
.port = "80";
}
sub vcl_recv {
    set req.backend = lighttpd;
    if (req.http.Accept-Encoding) {
        if (req.url ~ ".(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|pdf)$") {
                    # No compression fo these
                remove req.http.Accept-Encoding;
            } elsif (req.http.Accept-Encoding ~ "gzip") {
                set req.http.Accept-Encoding = "gzip";
        } elsif (req.http.Accept-Encoding ~ "deflate") {
                    set req.http.Accept-Encoding = "deflate";
        } else {
                    # unkown
                    remove req.http.Accept-Encoding;
        }
    }
    set req.grace = 30s;
    # Allow purging of cache using shift + reload
    if (req.http.Cache-Control ~ "no-cache") {
        purge_url(req.url);
    }
    # Unset any cookies and autorization data for static links and icons, and fetch from cache
    if (req.request == "GET" && req.url ~ "^/files/") {
        unset req.http.cookie;
        unset req.http.Authorization;
        lookup;
     }

     # Look for images in the cache
     if (req.url ~ ".(svg|png|gif|jpg|ico|jpeg|swf|css|js)$") {
        unset req.http.cookie;
        lookup;
     }

}

sub vcl_pipe {
    # Warning, default is disabled, it may break some web applications 
    set req.http.connection = "close";
}

sub vcl_fetch {
    set obj.grace = 30s;
    if (obj.ttl < 180s) {
        set obj.ttl = 180s;
    }

    if (req.http.Authorization && !obj.http.Cache-Control ~ "public") {
        # don't allow caching pages that are protected by basic authentication
        # unless when they explicitly set the cache-control to public.
        pass;
    }
}
Comments

Leave a response

Comment