CentOS 5.5环境下安装配置Varnish

#!/bin/bash
# BY kerryhu
# MAIL:king_819@163.com
# BLOG:
# 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
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";
}

#判断host请求针对那个后端服务器
sub vcl_recv {
 if (req.http.host ~ "^()?linuxidc.com$") {  #泛域名的写法"^(.*.)?linuxidc.com$"
  set req.backend = kerry;
 }
 elsif (req.http.host ~ "^()?linuxidc.net$") {
  set req.backend = king;
 }
 else {
  error 404 "Unknown HostName!"; #如果都不匹配,返回404错误
 }
 #不充许非访问控制列表的IP进行varnish缓存清除
 if(req.request == "PURGE") {
  if (!client.ip ~ local) {
   error 405 "Not Allowed.";
   return (lookup);
   }
 }
 #清除url中有jpg|png|gif等文件的cookie
 if (req.request == "GET" && req.url ~ "\.(jpg|png|gif|swf|jpeg|ico)$") {
  unset req.http.cookie;
 }
 #取消服务器上images目录下所有文件的cookie
 if (req.url ~ "^/images") {
  unset req.http.cookie;
 }
 #判断req.http.X-Forwarded-For,如果前端有多重反向代理,这样可以获取客户端IP地址。
 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);
 }
 #针对请求和url地址判断,是否在varnish缓存里查找
 if (req.request != "GET" && req.request != "HEAD") {
  return (pass);
 } ## 对非GET|HEAD请求的直接转发给后端服务器
 if (req.http.Authorization || req.http.Cookie) {
  return (pass);
 }
 if (req.request == "GET" && req.url ~ "\.(php)($|\?)") {
  return (pass);
  } #对GET请求,且url里以.php和.php?结尾的,直接转发给后端服务器
      return (lookup);
 }  #除了以上的访问以外,都在varnish缓存里查找

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";
 }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wywfsy.html