Centos 5.6 安裝varnish

Centos 5.6 安裝varnish
環境
varnish 192.168.58.166
nginx1 192.168.58.130
nginx1 192.168.58.168
 
linux同步北京時間
ntpdate stdtime.sinica.edu.tw
建立運行用戶和目錄
useradd -s /sbin/nologin varnish
mkdir /data
mkdir /data/varnish
mkdir /data/varnish/cache
mkdir /data/varnish/log
chown -R varnish:varnish /data/varnish/cache/ /data/varnish/log/
安裝pcre
yum -y install pcre*
安裝varnish
wget http://pkgs.fedoraproject.org/repo/pkgs/varnish/varnish-2.1.2.tar.gz/8b0d80e47acf4946671c381af55518b9/varnish-2.1.2.tar.gz
tar zxvf varnish-2.1.2.tar.gz -C /usr/src/
cd /usr/src/varnish-2.1.2/
export PKG_CONFIG_PATH=/usr/lib/pkgconfig
./configure --prefix=/usr/local/varnish --enable-dependency-tracking --enable-debugging-symbols --enable-developer-warnings
make && make install
新建配置文件
cd /usr/local/varnish/etc
vi vcl.conf
 
backend webserver1 { 
 .host = "192.168.58.130"; 
 .port = "80"; 
} 
backend webserver2 { 
 .host = "192.168.58.168"; 
 .port = "80"; 
} 
#backend webserver3 {
# .host = "192.168.12.14";
# .port = "80";
#}
#backend webserver4 {
# .host = "192.168.12.15";
# .port = "80";
#}
#定義一個名爲webserverdirector,也就是由webserver1webserver2兩臺後端服務器隨機分擔 
#請求。「.weight」用來指定兩臺後端服務器的權值。權值高的處理請求的概率就高些 
director webserver random { 
 {.backend = webserver1; .weight = 5;  } 
 {.backend = webserver2; .weight = 8;  } 
} 
#這裏設定清理緩存的規則,Varnish容許localhost127.0.0.1192.168.58.***三個來源IP經過 
# PURGE方法清除緩存 
acl purge { 
 "localhost"; 
 "127.0.0.1"; 
 "192.168.58.0"/26; 
} 
sub vcl_recv { 
 
#這裏設定,當發送PURGE請求的客戶端不是在acl中設定的地址時,將返回405狀態代碼,提示 
#Not allowed」。當請求的URL是以.php.cgi結尾時,則交給後端服務器去處理 
 if (req.request == "PURGE") { 
     if (!client.ip ~ purge) { 
     error 405 "Not allowed."; 
   } 
     elseif(req.url ~ "\.(php|cgi)($|\?)") { 
     return (pass); 
   } 
     else { 
     return (lookup); 
   } 
 } 
 
#下面設定域名訪問策略,其實也是設定對後端主機健康狀態檢測的一個機制。若是訪問www.ixdba.net 
#或者bbs.ixdba.net,而且請求重啓次數爲0,則將請求交給webserver來處理。若是請求重啓次數 
#1,則將請求交給webserver1處理。若是訪問img.ixdba.net或者p_w_picpaths.ixdba.net,則將 
#請求交給webserver2來處理 
# if((req.http.host ~"^(www.|bbs.)?ixdba.net")&&(req.restarts == 0))  {
#     set req.backend = webserver;
# } elseif(req.restarts == 1) {
#     set req.backend = webserver1;
# }
# if(req.http.host ~"^(img.|p_w_picpaths.)?ixdba.net") {
#     set req.backend = webserver2;
# }
#上面註釋部分是基於域名的訪問策略,有域名的話,能夠採用上面的。由於沒有域名,因此下面是去除了域名的部分。
if (req.restarts == 0) {
set req.backend = webserver;
} else if(req.restarts == 1) {
set req.backend = webserver1;
} else if(req.restarts == 2) {
set req.backend = webserver2;
}
 
#下面定義緩存的策略。當請求以.cgi.php結尾及帶有?URL時,不進行緩存,直接從後端服務器 
#讀取內容。其餘請求都進入lookup模式,也就是進入cache中經過hash表尋找被請求的數據 
 if (req.request != "GET" && req.request != "HEAD") 
 { 
   return (pipe); 
 } 
 elseif (req.url ~ "\.(cgi|php)($|\?)") 
 { 
  return (pass); 
 } 
 elseif (req.http.Authenticate || req.http.Authorization) { 
   return (pass); 
 } 
 return (lookup); 
} 
 
#若是請求的類型是PURGE方法,Varnishd會將此請求的緩存週期設置爲0,也就是使這個URL的緩存失效, 
#從而達到刷新Varnish緩存的目的 
sub vcl_hit 
{ 
 if (req.request == "PURGE") { 
   set obj.ttl = 0s; 
   error 200 "Purged."; 
 } 
 
 if (!obj.cacheable) 
 { 
   return (pass); 
 } 
 
 if (obj.http.Vary) 
 { 
   unset obj.http.Vary; 
 } 
} 
 
sub vcl_miss 
{ 
 if (req.request == "PURGE") { 
   error 404 "Not in cache."; 
 } 
 
} 
#定義hash的值,而且處理壓縮內容 
sub vcl_hash { 
 set req.hash += req.url; 
 if (req.http.host) { 
   set req.hash += req.http.host; 
 } else { 
   set req.hash += server.ip; 
 } 
 if ( req.http.Accept-Encoding ){ 
   if (req.url ~ "\.(jpg|jpeg|png|gif|rar|zip|gz|tgz|bz2|tbz|mp3|ogg|swf|exe|flv|avi|rmvb|rm|mpg|mpeg|pdf)$") { 
   } else { 
       set req.hash += req.http.Accept-Encoding; 
   } 
 } 
 return (hash); 
} 
 
sub vcl_fetch 
{ 
    if (!beresp.cacheable) { 
          return (pass); 
    } 
 
    if (beresp.http.Set-Cookie) { 
            return (pass); 
    } 
 
#定義在什麼狀態下進入restart模式 
    if (beresp.status == 500 || beresp.status == 501 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504 || beresp.status == 404) 
    { 
         return (restart); 
       } 
 
#下面定義不緩存含有哪些HTTP頭的請求 
     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 ~ "\.(css|js|html|htm)$") { 
              set beresp.ttl = 300s; 
   } 
  if (req.request == "GET" && req.url ~ "\.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|img|bmp|wmf)$") { 
       set beresp.ttl = 3600s; 
       } 
  if (req.request == "GET" && req.url ~ "\.(svg|swf|ico|mp3|mp4|m4a|wav|rmvb|avi|wmv)$") { 
       set beresp.ttl = 10d; 
       } 
return (deliver); 
 
} 
sub vcl_deliver { 
   if (obj.hits > 0) { 
      set resp.http.X-Cache = "HIT from www.xx.com"; 
   } else { 
     set resp.http.X-Cache = "MISS from www.xx.com"; 
   } 
   return (deliver); 
}
 
啓動varnish
/usr/local/varnish/sbin/varnishd -u varnish -g varnish -f /usr/local/varnish/etc/vcl.conf -a 192.168.58.166:80 -s file,/data/varnish/cache/varnish_cache.data,256MB -w 256,2560,10 -t 3600 -T 192.168.58.166:3000
查看端口
[root@ns1 etc]# netstat -napt | grep varnish
tcp        0      0 127.0.0.1:80                0.0.0.0:*                   LISTEN      22907/varnishd
tcp        0      0 127.0.0.1:3000              0.0.0.0:*                   LISTEN      22906/varnishd

192.168.58.130上面修改網頁內容
vi /usr/local/nginx/html/index.html
 
 
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx1!</h1></center>
</body>
</html>
 
192.168.58.168修改成Welcome to nginx2!
 
重啓nginx
/usr/local/nginx/sbin/nginx -s reload
 
 
客戶端訪問varnish
http://192.168.58.166/
 
關閉nginx1服務
/usr/local/nginx/sbin/nginx -s stop
等待一段時間
清楚緩存,再次訪問頁面,就看到nginx2
相關文章
相關標籤/搜索