下载地址:http://pkgs.fedoraproject.org/repo/pkgs/varnish/varnish-2.1.3.tar.gz/357d99a760de173d841ac37bf2052be8/varnish-2.1.3.tar.gz

一、安装varnish

# tar zxvf varnish-2.1.3.tar.gz

#cd varnish-2.1.3

#/configure –prefix=/opt/varnish

# make && make install

二、配置实例

# vi /opt/varnish/etc/varnish/test.vcl

backend bbs {
        .host = "202.105.182.55";
        .port = "80"; }


acl local { "localhost";
           "192.168.2.4";
          }

 


sub vcl_recv {
        if (req.request == "PURGE") {
            if (!client.ip ~ local) {
                error 405 "Not Allowed.";
                return (lookup);
        }
  }

  if (req.request == "GET" && req.url ~ "\.(jpg|png|gif|swf|jpeg|ico)$") {
   unset req.http.cookie;
   }
  if (req.http.x-forwarded-for) {
  set req.http.X-Forwarded-For =
  req.http.X-Forwarded-For ", " client.ip;
  } else {
    set req.http.X-Forwarded-For = client.ip;
    }
    if (req.request != "GET" &&
        req.request != "HEAD" &&
        req.request != "PUT" &&
        req.request != "POST" &&
        req.request != "TRACE" &&
        req.request != "OPTIONS" &&
        req.request != "DELETE") {

       return (pipe);
    }
    if (req.request != "GET" && req.request != "HEAD") {
       return (pass);
    }
    if (req.http.Authorization || req.http.Cookie) {
        return (pass);
     }
    if (req.request == "GET" && req.url ~ "\.(php)($|\?)") {
       return (pass);
     }
       return (lookup);
     }
     sub vcl_pipe { return (pipe);
      }
     sub vcl_pass { return (pass);
      }
     sub vcl_hash {
     set req.hash += req.url;
     if (req.http.host) {
         set req.hash += req.http.host;
      } else {
             set req.hash += server.ip;
      }
       return (hash);
       }
     sub vcl_hit {
      if (!obj.cacheable) {
      return (pass);
      }
     return (deliver);
      }
    sub vcl_miss {
        return (fetch);
      }

    sub vcl_fetch {
        if (!beresp.cacheable) {
             return (pass);
            }
        if (beresp.http.Set-Cookie) {
             return (pass);

}
        if (beresp.http.Set-Cookie) {
             return (pass);
            }
        if (beresp.http.Pragma ~ "no-cache" ||
     beresp.http.Cache-Control ~ "no-cache" ||
     beresp.http.Cache-Control ~ "private") {
       return (pass);
      }
    if (req.request == "GET" && req.url ~ "\.(js|css|mp3|jpg|png|gif|swf|jpeg|ico)$")
    {
      set beresp.ttl = 7d;
    }
     return (deliver);
   }
 sub vcl_deliver { set resp.http.x-hits = obj.hits ;
                  if (obj.hits > 0) {
                                   set resp.http.X-Cache = "HIT cqtel-bbs";
                 } else {
                       set resp.http.X-Cache = "MISS cqtel-bbs";
    }
 }


sub vcl_error {
    set obj.http.Content-Type = "text/html; charset=utf-8";
    synthetic {"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
   <head>
       <title>"} obj.status " " obj.response {"</title>
  </head>
<body>
  <h1>Error "} obj.status " " obj.response {"</h1>
  <p>"} obj.response {"</p>
  <h3>Guru Meditation:</h3>
  <p>XID: "} req.xid {"</p>
  <hr>
  <address>
  <a href="http://www.zhnews.net/">bbs cache server</a>
  </address>
</body>
</html>

"};
return (deliver);
}

启动varnish:

#ulimit -SHn 51200

#useradd -s /sbin/nologin www

## /opt/varnish/sbin/varnishd -u www -g www -f /opt/varnish/etc/varnish/test.vcl -a 192.168.2.4:80 -s file,/data/varnish_cache/varnish_cache.data,1G -w 1024,5100,10 -t 3600 -T 192.168.2.4:3500

参数: -u 以什么用运行

           -g 以什么组运行

           -f varnish配置文件

            -a 绑定IP和端口

           -s varnish缓存文件位置与大小

           -w 最小,最大线程和超时时间

           -T varnish管理端口,主要用来清除缓存

-s file,/data/varnish_cache/varnish_cache.data,1G   缓存目录,这个32位系统不能超过2G的,建议使用64位系统,还有想要更快的速度,把这个目录改成内存缓存会更快,内存越大越好

停止varnish:

#pkill varnishd