CentOS 5.5环境下安装配置Varnish

87530399 2011-05-07

#!/bin/bash
# BY kerryhu
# MAIL:[email protected]
# BLOG:http://kerry.blog.51cto.com
# Please manual operation yum of before Operation.....
#============================ 更新系统时间 ============================
yum install -y ntp
ntpdate time.nist.gov
echo "00 01 * * * ntpdate time.nist.gov" >> /etc/crontab

#============================ Varnish安装 =============================

CentOS 5.5环境下安装配置Varnish

如果是RedHat/CentOS系统,在安装varnish的时候首先要安装以下软件包
automake
autoconf
libtool
ncurses-devel
libxslt
groff
pcre-devel
pkgconfig

groupadd www
useradd www -g www -s /sbin/nologin
mkdir -p /data/varnish/{cache,logs}
chmod +w /data/varnish/{cache,logs}
chown -R www:www /data/varnish/{cache,logs}
cd /opt
yum install -y automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig
wget http://sourceforge.net/projects/varnish/files/varnish/2.1.3/varnish-2.1.3.tar.gz/download
tar -zxvf varnish-2.1.3.tar.gz
cd varnish-2.1.3
./configure --prefix=/usr/local/varnish
make;make install

#============================ varnish配置 ===========================

vi /usr/local/varnish/etc/varnish/kerry.vcl

backend kerry {              #定义后端服务器名
 .host = "192.168.9.203";    #定义后端服务器IP
 .port = "80";      #定义后端服务器端口
}

backend king {
 .host = "192.168.9.204";
 .port = "80";
}

#定义访问控制列表,充许那些IP清除varnish 缓存
acl local {
 "localhost";
 "127.0.0.1";
}

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);
 }
 if (req.request == "PURGE") {
         set obj.ttl = 0s;
         error 200 "Purged.";
      }
 return (deliver);
}

sub vcl_miss {
 return (fetch);
}

sub vcl_fetch {
 if (!beresp.cacheable) {
  return (pass);
 }
 if (beresp.http.Set-Cookie) {
  return (pass);
 }
 #WEB服务器指明不缓存的内容,varnish服务器不缓存
 if (beresp.http.Pragma ~ "no-cache" ||
     beresp.http.Cache-Control ~ "no-cache" ||
            beresp.http.Cache-Control ~ "private") {
  return (pass);
      }
      #对.txt .js .shtml结尾的URL缓存时间设置1小时,对其他的URL缓存时间设置为10天
 if (req.request == "GET" && req.url ~ "\.(txt|js|css|shtml|html|htm)$") {
               set beresp.ttl = 3600s;
 }
 else {
               set beresp.ttl = 10d;
 }
 return (deliver);
}
#添加在页面head头信息中查看缓存命中情况
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.6688.cc/">bbs cache server</a>
      </address>
   </body>
</html>
"};
 return (deliver);
}


注意:在2.1后的版本里,原"obj.*"的变量全部变为"beresp.*"了,需要留意一下

相关推荐