#################varnish#################php
主機環境: rhel6 selinux and iptables disabledhtml
實驗主機: 172.25.254.11 varnishlinux
172.25.254.12 apacheweb
172.25.254.13 apacheapache
處理過程大體分爲以下幾個步驟:vim
(1)Receive 狀態,也就是請求處理的入口狀態,根據 VCL 規則判斷該請求應該是 Pass 或Pipe,或者進入 Lookup(本地查詢)。後端
(2)Lookup 狀態,進入此狀態後,會在 hash 表中查找數據,若找到,則進入 Hit 狀態,不然進入 miss 狀態。緩存
(3)Pass 狀態,在此狀態下,會進入後端請求,即進入 fetch 狀態。服務器
(4)Fetch 狀態,在 Fetch 狀態下,對請求進行後端的獲取,發送請求,得到數據,並進行本地的存儲。負載均衡
(5)Deliver 狀態, 將獲取到的數據發送給客戶端,而後完成本次請求。
1. 安裝
yum install -y varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
2.配置
(1)配置一個後端服務器
準備工做:若是火牆沒有關閉,將其關閉,打開其它兩個服務器並裝上httpd並打開
[root@server1 ~]# vim /etc/varnish/default.vcl
在12主機/var/www/html下編寫index.html文件作爲測試網頁
[root@server1 ~]# vim /etc/sysconfig/varnish
66 VARNISH_LISTEN_PORT=80
[root@server1 ~]# /etc/init.d/varnish start
測試:
(2)查看緩存命中狀況
[root@server1 bansys]# /etc/init.d/varnish reload
(3)定義多個不一樣域名站點後端服務器
#當訪問 www.westos.org 域名時從 web1 上取數據,訪問 bbs.westos.org 域名時到 web2 取數據,訪問其餘頁面報錯。
[root@server1 ~]# /etc/init.d/varnish reload
測試:
(4)定義負載均衡
[root@server1 ~]# /etc/init.d/varnish reload
[root@server2 html]# mkdir /www/bbs -p
[root@server2 html]# mkdir /www/westos
mkdir: cannot create directory `/www/westos': File exists
[root@server2 html]# cd /www/bbs
[root@server2 bbs]# ls
Index.html
<h1>bbs.wesstoss.org</h1>
[root@server2 bbs]# vim *
[root@server2 bbs]# cd ..
[root@server2 www]# cd westos/
[root@server2 westos]# ls
Index.html
<h1>server2www.westos.org</h1>
[root@server2 westos]# vim *
[root@server2 westos]# /etc/init.d/httpd restart
[root@server3 ~]# cd /var/www/html
[root@server3 html]# ls
index.html
[root@server3 html]# vim *
<h1>server3www.westos.org</h1>
### 經過 varnishadm 手動清除緩存
# varnishadm ban.url .*$ #清除全部
# varnishadm ban.url /index.html #清除 index.html 頁面緩存
# varnishadm ban.url /admin/$ #清除 admin 目錄緩存
注:每次測試時清除上一次的varnish的緩存,若是不進行緩存的話在配置時加上return(pass);
測試:
(5)varnish cdn 推送平臺
[root@server1 ~]# yum reinstall -y php
[root@server1 ~]# unzip bansys.zip -d /var/www/html
[root@server1 ~]# cd /var/www/html
[root@server1 html]# ls
bansys class_socket.php config.php index.php purge_action.php static
[root@server1 html]# cd bansys/
[root@server1 bansys]# ls
class_socket.php config.php index.php purge_action.php static
[root@server1 bansys]# vim config.php
[root@server1 ~]# vim /etc/varnish/default.vcl
[root@server1 bansys]# /etc/init.d/varnish reload
測試: