本人对varnish也是新手,这里记录一下安装步骤!

环境:centos6.6

varnish安装包下载:wget https://repo.varnish-cache.org/source/varnish-3.0.7.tar.gz

 

先上我的拓扑图 (基础拓扑为LVSDR环境)  LVS不在这里讲解

 

安装

yum -y install gcc gcc-c++ make autoconf automake
yum -y install automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig readline-devel
tar -zxf varnish-3.0.7.tar.gz
cd varnish-3.0.7
./configure --prefix=/usr/local/varnish
make;make install

 拷贝文件到系统

cp redhat/varnish.initrc /etc/init.d/varnish
cp redhat/varnish.sysconfig /etc/sysconfig/varnish
cp redhat/varnish_reload_vcl /usr/local/varnish/bin

 

修改varnish配置文件

 

vim /etc/sysconfig/varnish

修改为如下内容 (里面有许多varnish的性能配置)

VARNISH_VCL_CONF=/usr/local/varnish/etc/varnish/default.vcl

VARNISH_LISTEN_PORT=80

VARNISH_SECRET_FILE=/usr/local/varnish/etc/varnish/secret

 

vim /etc/init.d/varnish

pidfile=/var/run/varnish.pid
exec="/usr/local/varnish/sbin/varnishd"
reload_exec="/usr/local/varnish/bin/varnish_reload_vcl"

 

# Done parsing, set up command
VARNISHADM="/usr/local/varnish/bin/varnishadm $secret -T $VARNISH_ADMIN_LISTEN_ADDRESS:$VARNISH_ADMIN_LISTEN_PORT"

 注:也可以将/usr/local/varnish/bin添加到系统的PATH中,这样就不需要编辑varnish_reload_vcl

 

生成varnish管理秘钥:

/usr/bin/uuidgen > /usr/local/varnish/etc/varnish/secret
chmod 644 /usr/local/varnish/etc/varnish/secret

 

VCL配置(对应拓扑图)

vim /usr/local/varnish/etc/varnish/default.vcl

backend web1 {
.host = "192.168.0.38";
.port = "80";
.connect_timeout = 1s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 2s;
}

backend web2 {
.host = "192.168.0.162";
.port = "80";
.connect_timeout = 1s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 2s;
}

director syw_web random {
        { .backend = web1; .weight =2; }
        { .backend = web2; .weight =2; }

}

#clean cache
acl purge {
        "localhost";
        "127.0.0.1";
        "192.168.0.162";
        "192.168.0.167";
}

sub vcl_recv {
        if (req.http.host ~ "^192.168.0.193"){
                set req.backend = syw_web;}


if (req.request == "PURGE") {
      if (!client.ip ~purge) {
        error 405 "Not Allowed";
    }
        return(lookup); #find localhost cache
}

if (req.request == "GET" && req.url ~ "\.(png|swf|txt|png|gif|jpg|css|js|htm|html)$") {
    unset req.http.cookie;
 }

if (req.url ~ "^/images") {
  unset req.http.cookie;
 }

#if (req.http.Cache-Control ~ "(no-cache|max-age=0)"){
#       purge_url(req.url);
#}
#       return (lookup);


#获取客户端ip
#     if (req.restarts == 0) {
  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;
            }
  #   }
}

 

检查VCL配置是否正确: 
/etc/init.d/varnish configtest
或 
/usr/local/varnish/sbin/varnishd -C -f /usr/local/varnish/etc/varnish/fdfs.vcl    #启动varnish: 

service varnish start    #查看varnish状态: 

/etc/init.d/varnish status   # 动态加载VCL配置: 

/etc/init.d/varnish reload   # 停止varnish: 

/etc/init.d/varnish stop

 

先就说这么多吧

更多精华请参考

http://www.it165.net/admin/html/201405/2999.html

http://www.51itstudy.com/30132.html