Varnish是一款高性能的開源HTTP加速器,挪威最大的在線報紙 Verdens Gang (vg.no) 使用3臺Varnish代替了原來的12臺squid,性能比以前更好。
Varnish的作者Poul-Henning Kamp是FreeBSD的內核開發者之一,他認為現在的計算機比起1975年已經復雜許多。在1975年時,儲存媒介只有兩種:內存與硬盤。但現在計算 機系統的內存除了主存外,還包括了cpu內的L1、L2,甚至有L3快取。硬盤上也有自己的快取裝置,因此squid cache自行處理物件替換的架構不可能得知這些情況而做到最佳化,但操作系統可以得知這些情況,所以這部份的工作應該交給操作系統處理,這就是 Varnish cache設計架構。
Varnish可以在FreeBSD 6.0和Linux 2.6內核上運行。
1、編譯安裝varnish HTTP加速器:
tar zxvf varnish-1.1.1.tar.gz
cd varnish-1.1.1
./configure --prefix=/usr/local/varnish
make && make install
2、簡單啟動varnish守護進程,用本機80端口去反向代理加速127.0.0.1:81上的Apache服務器:
Varnish官方網站:
http://www.varnish-cache.org/
另有一份PDF文檔,說明Varnish原理的:
http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2163384
我測試了一下,在同等配置環境下,Varnish的性能確實要超過Squid,穩定性也不錯,值得繼續去深入研究。
今天寫的這篇關于Varnish的文章,已經是一篇可以完全替代Squid做網站緩存加速器的詳細解決方案了。網上關于Varnish的資料很少,中文資料更是微乎其微,希望本文能夠吸引更多的人研究、使用Varnish。
在我看來,使用Varnish代替Squid的理由有三點:
1、Varnish采用了“Visual Page Cache”技術,在內存的利用上,Varnish比Squid具有優勢,它避免了Squid頻繁在內存、磁盤中交換文件,性能要比Squid高。
2、Varnish的穩定性還不錯,我管理的一臺圖片服務器運行Varnish已經有一個月,沒有發生過故障,而進行相同工作的Squid服務器就倒過幾次。
3、通過Varnish管理端口,可以使用正則表達式快速、批量地清除部分緩存,這一點是Squid不能具備的。
下面來安裝Varnish網站緩存加速器(Linux系統):
1、創建www用戶和組,以及Varnish緩存文件存放目錄(/var/vcache):
/usr/sbin/useradd -u 48 -g www www
mkdir -p /var/vcache
chmod +w /var/vcache
chown -R www:www /var/vcache
2、創建Varnish日志目錄(/var/logs/):
chmod +w /var/logs
chown -R www:www /var/logs
3、編譯安裝varnish:
tar zxvf varnish-1.1.2.tar.gz
cd varnish-1.1.2
./configure --prefix=/usr/local/varnish
make && make install
4、創建Varnish配置文件:
輸入以下內容:
? ? ? ?set backend.host = "192.168.0.5";
? ? ? ?set backend.port = "80";
}
acl purge {
? ? ? ?"localhost";
? ? ? ?"127.0.0.1";
? ? ? ?"192.168.1.0"/24;
}
sub vcl_recv {
? ? ? ?if (req.request == "PURGE") {
? ? ? ? ? ? ? ?if (!client.ip ~ purge) {
? ? ? ? ? ? ? ? ? ? ? ?error 405 "Not allowed.";
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?lookup;
? ? ? ?}
? ? ? ?if (req.http.host ~ "^blog.s135.com") {
? ? ? ? ? ? ? ?set req.backend = myblogserver;
? ? ? ? ? ? ? ?if (req.request != "GET" && req.request != "HEAD") {
? ? ? ? ? ? ? ? ? ? ? ?pipe;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?else {
? ? ? ? ? ? ? ? ? ? ? ?lookup;
? ? ? ? ? ? ? ?}
? ? ? ?}
? ? ? ?else {
? ? ? ? ? ? ? ?error 404 "Zhang Yan Cache Server";
? ? ? ? ? ? ? ?lookup;
? ? ? ?}
}
sub vcl_hit {
? ? ? ?if (req.request == "PURGE") {
? ? ? ? ? ? ? ?set obj.ttl = 0s;
? ? ? ? ? ? ? ?error 200 "Purged.";
? ? ? ?}
}
sub vcl_miss {
? ? ? ?if (req.request == "PURGE") {
? ? ? ? ? ? ? ?error 404 "Not in cache.";
? ? ? ?}
}
sub vcl_fetch {
? ? ? ?if (req.request == "GET" && req.url ~ "\.(txt|js)$") {
? ? ? ? ? ? ? ?set obj.ttl = 3600s;
? ? ? ?}
? ? ? ?else {
? ? ? ? ? ? ? ?set obj.ttl = 30d;
? ? ? ?}
}
這里,我對這段配置文件解釋一下:
(1)、Varnish通過反向代理請求后端IP為192.168.0.5,端口為80的web服務器;
(2)、Varnish允許localhost、127.0.0.1、192.168.0.***三個來源IP通過PURGE方法清除緩存;
(3)、Varnish對域名為blog.s135.com的請求進行處理,非blog.s135.com域名的請求則返回“Zhang Yan Cache Server”;
(4)、Varnish對HTTP協議中的GET、HEAD請求進行緩存,對POST請求透過,讓其直接訪問后端Web服務器。之所以這樣配置,是因為POST請求一般是發送數據給服務器的,需要服務器接收、處理,所以不緩存;
(5)、Varnish對以.txt和.js結尾的URL緩存時間設置1小時,對其他的URL緩存時間設置為30天。
5、啟動Varnish
/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1G -g www -u www -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on
6、啟動varnishncsa用來將Varnish訪問日志寫入日志文件:
7、配置開機自動啟動Varnish
在末尾增加以下內容:
/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1G -g www -u www -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/youvideo.log &
8、優化Linux內核參數
在末尾增加以下內容:
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000 ? ?65000
再看看如何管理Varnish:
1、查看Varnish服務器連接數與命中率:
2、通過Varnish管理端口進行管理:
用help看看可以使用哪些Varnish命令:
ping [timestamp]
status
start
stop
stats
vcl.load
vcl.inline
vcl.use
vcl.discard
vcl.list
vcl.show
param.show [-l] []
param.set
help [command]
url.purge
dump.pool
3、通過Varnish管理端口,使用正則表達式批量清除緩存:
(1)、例:清除類似
http://blog.s135.com/a/zhangyan.html
的URL地址):
(2)、例:清除類似
http://blog.s135.com/tech
的URL地址:
(3)、例:清除所有緩存:
4、一個清除Squid緩存的PHP函數(清除Varnish緩存同樣可以使用該函數,無需作任何修改,十分方便):
- <?php??
- function ?purge( $ip ,? $url )??
- {??
- ???? $errstr ?=? '' ;??
- ???? $errno ?=? '' ;??
- ???? $fp ?=? fsockopen ?( $ip ,?80,? $errno ,? $errstr ,?2);??
- ???? if ?(! $fp )??
- ????{??
- ????????? return ?false;??
- ????}??
- ???? else ??
- ????{??
- ???????? $out ?=? "PURGE?$url?HTTP/1.1\r\n" ;??
- ???????? $out ?.=? "Host:blog.s135.com\r\n" ;??
- ???????? $out ?.=? "Connection:?close\r\n\r\n" ;??
- ???????? fputs ?( $fp ,? $out );??
- ???????? $out ?=? fgets ( $fp ?,?4096);??
- ????????fclose?( $fp );??
- ???????? return ?true;??
- ????}??
- }??
- ??
- purge( "192.168.0.4" ,? "/index.php" );??
- ?>??
附1:Varnish官方網站:
http://www.varnish-cache.org/
附2:2007年12月10日,我寫了一個每天0點運行,按天切割Varnish日志,生成一個壓縮文件,同時刪除上個月舊日志的腳本(/var/logs/cutlog.sh):
/var/logs/cutlog.sh文件內容如下:
# This file run at 00:00
date=$(date -d "yesterday" +"%Y-%m-%d")
pkill -9 varnishncsa
mv /var/logs/youvideo.log /var/logs/${date}.log
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/youvideo.log &
mkdir -p /var/logs/youvideo/
gzip -c /var/logs/${date}.log > /var/logs/youvideo/${date}.log.gz
rm -f /var/logs/${date}.log
rm -f /var/logs/youvideo/$(date -d "-1 month" +"%Y-%m*").log.gz
設置在每天00:00定時執行:
或者
輸入以下內容:
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
