最近配置Varnish,经常发现把内存资源和磁盘IO资源占满的情况,后面找原因,发现Varnish把一些大文件缓存了,例如一些压缩档。记得以前用Squid时是有相应设置项的,可以不缓存文件大小超过1M的。后面经过Google搜索了一番,发现有篇文章的方法不错,用变通的方法(vcl_fetch 不支持 pass 语法,但支持 restart)解决了我的困扰,遂写日志以记之。

sub vcl_recv {
  /** ... vcl_recv example from above ... */
  /* Bypass cache for large files.  The x-pipe header is
     set in vcl_fetch when a too large file is detected. */
  if (req.http.x-pipe && req.restarts > 0) {
    remove req.http.x-pipe;
    return (pipe);
  }
  /** ... vcl_recv example from above ... */
}
sub vcl_fetch {
  /* Don't try to cache too large files.  It appears
      just crashes if we don't filter them. */
  if (beresp.http.Content-Length ~ "[0-9]{7,}") {
    set req.http.x-pipe = "1";
    return (restart);
  }
}
sub vcl_pipe {
  set bereq.http.connection = "close";
}

地址:http://devblog.seomoz.org/2011/05/how-to-cache-http-range-requests/
https://www.varnish-cache.org/lists/pipermail/varnish-misc/2010-November/004968.html