<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Equars People  - Varnish Changes</title>
  <id>tag:people.equars.com,2010:/2009/10/28/varnish/changes</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://people.equars.com/2009/10/28/varnish/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2009/10/28/varnish" rel="alternate" type="text/html"/>
  <updated>2009-10-28T10:28:10Z</updated>
  <entry xml:base="http://people.equars.com/">
    <author>
      <name>marco</name>
    </author>
    <id>tag:people.equars.com,2009-10-28:202:1</id>
    <updated>2009-10-28T10:28:10Z</updated>
    <link href="http://people.equars.com/2010/3/12/varnish" rel="alternate" type="text/html"/>
    <title>Varnish</title>
<content type="html">&lt;p&gt;&lt;a href=&quot;http://varnish.projects.linpro.no/&quot;&gt;Varnish&lt;/a&gt;  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.&lt;/p&gt;

&lt;p&gt;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 &lt;a href=&quot;http://blog.hamzahkhan.com/2009/09/30/nginx-varnish-haproxy-and-thinlighttpd/&quot;&gt;blog post&lt;/a&gt; and in the &lt;a href=&quot;http://varnish.projects.linpro.no/wiki/FAQ&quot;&gt;Varnish FAQ&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The only functions I modified with respect to the default configuration are &lt;pre&gt;vcl&lt;em&gt;recv&lt;/pre&gt;,  &lt;pre&gt;vcl&lt;/em&gt;pipe&lt;/pre&gt; and &lt;pre&gt;vcl_fetch&lt;/pre&gt;:&lt;/p&gt;

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

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

}

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

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

    if (req.http.Authorization &amp;amp;&amp;amp; !obj.http.Cache-Control ~ &quot;public&quot;) {
        # don't allow caching pages that are protected by basic authentication
        # unless when they explicitly set the cache-control to public.
        pass;
    }
}
&lt;/code&gt;&lt;/pre&gt;</content>  </entry>
</feed>
