主機環境: rhel6 selinux and iptables disabled
實驗主機: 192.168.2.36 varnish
192.168.2.46 apache
192.168.2.38 apache
框架圖:
VCL 處理流程圖:
處理過程大致分爲如下幾個步驟:
(1)Receive 狀態,也就是請求處理的入口狀態,根據 VCL 規則判斷該請求應該是 Pass 或
Pipe,或者進入 Lookup(本地查詢)。
(2)Lookup 狀態,進入此狀態後,會在 hash 表中查找數據,若找到,則進入 Hit 狀態,否則進
入 miss 狀態。
(3)Pass 狀態,在此狀態下,會進入後端請求,即進入 fetch 狀態。
(4)Fetch 狀態,在 Fetch 狀態下,對請求進行後端的獲取,發送請求,獲得數據,並進行本地
的存儲。
(5)Deliver 狀態, 將獲取到的數據發送給客戶端,然後完成本次請求。
1. 安裝
http://repo.varnish-cache.org/redhat/varnish-3.0/el6/x86_64/
rpm -ivh varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
2. 配置
# vi /etc/varnish/default.vcl
###配置一個後端服務器
backend web1 {
.host = "192.168.0.188";
.port = "80";
}
###配置 varnish 服務端口
# vi /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80
###查看緩存命中情況
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
# service varnish start
在desktop46上安裝httpd 並啓動
#curl -I 192.168.2.36
#curl -I 192.168.2.36
###通過 varnishadm 手動清除緩存
# varnishadm ban.url .*$ #清除所有
# varnishadm ban.url /index.html #清除 index.html 頁面緩存
# varnishadm ban.url /admin/$ #清除 admin 目錄緩存
###定義多個不同域名站點的後端服務器
#當訪問 www.westos.org 域名時從 web1 上取數據,訪問 bbs.westos.org 域名時到 web2 取數據,
訪問其他頁面報錯。
#service varnish reload
#/etc/init.d/varnish reload
在desktop38上配置虛擬主機名vim /etc/httpd/conf/httpd.conf
在desktop38的/var/www/下創建web2目錄在裏面寫一個測試文件
#/etc/init.d/httpd restart
然後測試:
###varnish cdn 推送平臺
http://code.google.com/p/varnish-php-bansys/
把這個解壓到 desktop36 的/var/www/html/
然後編輯config.php
#編輯desktop36的httpd配置文件
#vim /etc/init.d/httpd/conf/httpd.conf
把監聽端口改爲 8080 不然就和varnish的端口起衝突
推送模式是http的話對varnish做如下配置:
#/etc/init.d/varnish reload
然後在firefox上測試:
OK!!