Varnish是一款強大的反向 Proxy加速軟體,關於其工作原理可以參考上圖,其具體流程及VCL文法我這裡就不做說明,網上資料多,大家還可以對照參考其官方網站和《Varnish中文權威指南》。
一、安裝CentOS5.8系統內容下的依耐關係
yum install gcc gcc-c++
yum install automake autoconflibtool ncurses-devel libxslt groff pcre-devel pkgconfig libtool -y
二、下載varnish-2.1.5源碼包,並進行編譯安裝。
cd /usr/local/src
wget http://repo.varnish-cache.org/source/varnish-2.1.5.tar.gz
tar zxvf varnish-2.1.5.tar.gz
cd varnish-2.1.5.
./autogen.sh
#autogen.sh命令是用來檢查軟體的依耐關係是否滿足,如果報錯的話, 則應該如下
正常所示:
+ aclocal
+ libtoolize --copy --force
+ autoheader
+ automake --add-missing --copy --foreign
+ autoconf
繼續編譯安裝:
./configure --prefix=/usr/local/varnish --enable-dependency-tracking --enable-debugging-symbols --enable-developer-warnings -enable-extra-warnings
make && make install && cd ../
三、建立varnish使用者和組,以及varnish快取檔案和日誌存放目錄:
/usr/sbin/groupadd varnish
/usr/sbin/useradd -s /sbin/nologin -g varnish varnish
mkdir -p /data/varnish/{cache,log}
chown -R varnish:varnish /data/varnish/{cache,log}
四、我的測試環境是兩台Web機器,IP為192.168.1.103(網域名稱為http://www.yuhongchun027.net)的varnish機器對後端IP為192.168.1.104和192.168.1.105的機器進行反向 Proxy加速,其設定檔/usr/local/varnish/etc/varnish/better.vcl如下所示:
backend rserver1{.host ="192.168.1.104";.port = "80";.probe = {.timeout = 5s; #等待多長時間逾時.interval = 2s; #檢查時間間隔.window = 10; #varnish將維持10個sliding windows的結果.threshold = 8; #如果是8次.windows檢查是成功的,就宣告後端的Web機器是健康的}}backend rserver2{.host ="192.168.1.105";.port = "80";.probe = {.timeout = 5s; .interval = 2s; .window = 10; .threshold = 8;}}#指定一個名為realserver組,使用random機制,權重越大,分配的訪問越多,可根據伺服器效能來設定;而round-robin(輪詢)機制是不能指定weight的director realserver random {{.backend = rserver1;.weight = 5;}{.backend = rserver2;.weight = 6;}}#定義能清理緩衝的機器,這裡只允許本機能用purge的方式清理acl purge {"localhost";"127.0.0.1";}sub vcl_recv{ if (req.http.host ~"^(.*).yuhongchun027.net") { set req.backend =realserver; } else { error 200 "Nocahce for this domain"; } if (req.request =="PURGE") { if (!client.ip ~purge) { error 405"Not allowed."; } else { return (pipe); }}#擷取用戶端真實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; }#對HTTP協議中的GET、HEAD請求進行緩衝,對POST請求透過,讓其直接存取後端Web伺服器。之所以這樣配置,是因為POST請求一般是發送資料給伺服器的,需要伺服器接收、處理,所以不緩衝;if (req.request !="GET" && req.request != "HEAD"){ return (pipe);}if (req.http.Expect){ return (pipe);}if (req.http.Authenticate|| req.http.Cookie){ return (pass);}if (req.http.Cache-Control~ "no-cache"){ return (pass);}#對JSP或者PHP檔案不緩衝if(req.url ~"\.jsp" || req.url ~ "\.php" ){ return (pass);}else{return (lookup);}}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 (req.request =="PURGE"){ set obj.ttl = 0s; error 200"Purged.";}if (!obj.cacheable){ return (pass);}return (deliver);}sub vcl_miss{if (req.request =="PURGE"){ error 404 "Not incache.";}if (req.http.user-agent ~"spider"){ error 503 "Notpresently in cache";} return (fetch);}sub vcl_fetch{if (req.request =="GET" && req.url ~ "\.(txt|js)$"){ set beresp.ttl = 3600s;}else{ set beresp.ttl = 30d;}if (!beresp.cacheable){ return (pass);}if (beresp.http.Set-Cookie){ return (pass);}return (deliver);}sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache= "HIT FROM www.yuhongchun027.net"; } else { set resp.http.X-Cache= "MISS FROM www.yuhongchun027.net"; }return (deliver);}
查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/