Varnish緩存服務(wù)器在linux系統(tǒng)下怎么安裝
Varnish緩存服務(wù)器在linux系統(tǒng)下怎么安裝
Varnish經(jīng)常被用來(lái)作反向代理來(lái)加速HTTP傳輸,那么Varnish緩存服務(wù)器在linux系統(tǒng)下怎么安裝呢?下面跟著學(xué)習(xí)啦小編一起來(lái)了解一下吧。
Varnish緩存服務(wù)器在linux系統(tǒng)下安裝方法
Varnish是一款高性能且開源的反向代理服務(wù)器和http加速器。與傳統(tǒng)的Squid相比,Varnish具有性能更高、速度更快、管理更方便等諸多優(yōu)點(diǎn)。作者Poul-Henning Kamp是FreeBSD的內(nèi)核開發(fā)者之一。Varnish采用全新的軟件體系架構(gòu),和現(xiàn)在的硬件提交配合緊密。在1975年時(shí),儲(chǔ)存媒介只有兩種:內(nèi)存與硬盤。但現(xiàn)在計(jì)算 機(jī)系統(tǒng)的內(nèi)存除了主存外,還包括了cpu內(nèi)的L1、L2,甚至有L3快取。硬盤上也有自己的快取裝置,因此squid cache自行處理物件替換的架構(gòu)不可能得知這些情況而做到最佳化,但操作系統(tǒng)可以得知這些情況,所以這部份的工作應(yīng)該交給操作系統(tǒng)處理,這就是 Varnish cache設(shè)計(jì)架構(gòu)。
挪威最大的在線報(bào)紙 Verdens Gang (http://www.vg.no) 使用3臺(tái)Varnish代替了原來(lái)的12臺(tái)squid,性能居然比以前更好,這是Varnish最成功的應(yīng)用案例。
Varnish特點(diǎn):
1.基于內(nèi)存進(jìn)行緩存,重啟后數(shù)據(jù)將消失
2.利用虛擬內(nèi)存方式,I/O性能好
3.支持設(shè)置0~60秒的精確緩存時(shí)間
4.VCL配置管理比較靈活
5.32位機(jī)器上緩存文件大小最大為2G
6.具有強(qiáng)大的管理功能,例如top、stat、admin、list等
7.狀態(tài)機(jī)設(shè)置巧妙,結(jié)構(gòu)清晰
8.利用二叉堆管理緩存文件,可達(dá)到積極刪除目的
Varnish與Squid的對(duì)比
Squid是一個(gè)高性能的代理緩存服務(wù)器,它和varnish之間有諸多的異同點(diǎn),如下:
相同點(diǎn):
都是一個(gè)反向代理服務(wù)器
都是開源軟件
不同點(diǎn),也是Varnish的優(yōu)點(diǎn):
Varnish的穩(wěn)定性很高,兩者在完成相同負(fù)荷的工作時(shí),Squid服務(wù)器發(fā)生故障的幾率要高于Varnish,因?yàn)槭褂肧quid要經(jīng)常重啟。
Varnish訪問(wèn)速度更快,Varnish采用了“Visual Page Cache”技術(shù),所有緩存數(shù)據(jù)都直接從內(nèi)存讀取,而squid是從硬盤讀取,因而Varnish在訪問(wèn)速度方面會(huì)更快。
Varnish可以支持更多的并發(fā)連接,因?yàn)閂arnish的TCP連接釋放要比Squid快。因而在高并發(fā)連接情況下可以支持更多TCP連接。
Varnish可以通過(guò)管理端口,使用正則表達(dá)式批量的清除部分緩存,而Squid是做不到的。
squid屬于是單進(jìn)程使用單核CPU,但Varnish是通過(guò)fork形式打開多進(jìn)程來(lái)做處理,所以是合理的使用所有核來(lái)處理相應(yīng)的請(qǐng)求。
當(dāng)然,與傳統(tǒng)的Squid相比,Varnish也是有缺點(diǎn)的,如下:
varnish進(jìn)程一旦掛起、崩潰或者重啟,緩存數(shù)據(jù)都會(huì)從內(nèi)存中完全釋放,此時(shí)所有請(qǐng)求都會(huì)發(fā)送到后端服務(wù)器,在高并發(fā)情況下,會(huì)給后端服務(wù)器造成很大壓力。
在varnish使用中如果單個(gè)url的請(qǐng)求通過(guò)HA/F5(負(fù)載均衡)每次請(qǐng)求不同的varnish服務(wù)器中,被請(qǐng)求varnish服務(wù)器都會(huì)被穿透到后端,而且同樣的請(qǐng)求會(huì)在多臺(tái)服務(wù)器上緩存,也會(huì)造成varnish的緩存的資源浪費(fèi),也會(huì)造成性能下降。
解決方案:
綜上所述在訪問(wèn)量很大的情況下推薦使用varnish的內(nèi)存緩存方式啟動(dòng),而且后面需要跟多臺(tái)squid服務(wù)器。主要為了防止前面的varnish服務(wù)、服務(wù)器被重啟的情況下,前期肯定會(huì)有很多的穿透這樣squid可以擔(dān)當(dāng)?shù)诙觕ache,而且也彌補(bǔ)了varnish緩存在內(nèi)存中重啟都會(huì)釋放的問(wèn)題。
這樣的問(wèn)題可以在負(fù)載均衡上做url哈希,讓單個(gè)url請(qǐng)求固定請(qǐng)求到一臺(tái)varnish服務(wù)器上,可以解決該問(wèn)題。
varnish的工作流程
1、進(jìn)程之間通信
varnish啟動(dòng)或有2個(gè)進(jìn)程 master(management)進(jìn)程和child(worker)進(jìn)程。master讀入存儲(chǔ)配置命令,進(jìn)行初始化,然后fork,監(jiān)控child。child則分配線程進(jìn)行cache工作,child還會(huì)做管理線程和生成很多worker線程。
child進(jìn)程主線程初始化過(guò)程中,將存儲(chǔ)大文件整個(gè)加載到內(nèi)存中,如果該文件超出系統(tǒng)的虛擬內(nèi)存,則會(huì)減少原來(lái)配置mmap大小,然后繼續(xù)加載,這時(shí)候創(chuàng)建并初始化空閑存儲(chǔ)結(jié)構(gòu)體,放在存儲(chǔ)管理的struct中,等待分配。
接著varnish某個(gè)負(fù)責(zé)接口新http連接的線程開始等待用戶,如果有新的http連接,但是這個(gè)線程只負(fù)責(zé)接收,然后喚醒等待線程池中的work線程,進(jìn)行請(qǐng)求處理。
worker線程讀入uri后,將會(huì)查找已有的object,命中直接返回,沒(méi)有命中,則會(huì)從后端服務(wù)器中取出來(lái),放到緩存中。如果緩存已滿,會(huì)根據(jù)LRU算法,釋放舊的object。對(duì)于釋放緩存,有一個(gè)超時(shí)線程會(huì)檢測(cè)緩存中所有object的生命周期,如果緩存過(guò)期(ttl),則刪除,釋放相應(yīng)的存儲(chǔ)內(nèi)存。
2、配置文件各結(jié)構(gòu)之間通信
Varnish安裝
代碼如下:
wget http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-8.33.tar.gz
tar xzf pcre-8.33.tar.gz
cd pcre-8.33
./configure
make && make install
cd ../
varnish-3.0.4報(bào)錯(cuò)如下:
varnishadm.c:48:33: error: editline/readline.h: No such file or directory
varnishadm.c: In function 'cli_write':
varnishadm.c:76: warning: implicit declaration of function 'rl_callback_handler_remove'
varnishadm.c:76: warning: nested extern declaration of 'rl_callback_handler_remove'
varnishadm.c: In function 'send_line':
varnishadm.c:179: warning: implicit declaration of function 'add_history'
varnishadm.c:179: warning: nested extern declaration of 'add_history'
varnishadm.c: In function 'varnishadm_completion':
varnishadm.c:216: warning: implicit declaration of function 'rl_completion_matches'
varnishadm.c:216: warning: nested extern declaration of 'rl_completion_matches'
varnishadm.c:216: warning: assignment makes pointer from integer without a cast
varnishadm.c: In function 'pass':
varnishadm.c:233: error: 'rl_already_prompted' undeclared (first use in this function)
varnishadm.c:233: error: (Each undeclared identifier is reported only once
varnishadm.c:233: error: for each function it appears in.)
varnishadm.c:235: warning: implicit declaration of function 'rl_callback_handler_install'
varnishadm.c:235: warning: nested extern declaration of 'rl_callback_handler_install'
varnishadm.c:239: error: 'rl_attempted_completion_function' undeclared (first use in this function)
varnishadm.c:300: warning: implicit declaration of function 'rl_forced_update_display'
varnishadm.c:300: warning: nested extern declaration of 'rl_forced_update_display'
varnishadm.c:303: warning: implicit declaration of function 'rl_callback_read_char'
varnishadm.c:303: warning: nested extern declaration of 'rl_callback_read_char'
make[3]: *** [varnishadm-varnishadm.o] Error 1
make[3]: Leaving directory `/root/lnmp/src/varnish-3.0.4/bin/varnishadm'
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory `/root/lnmp/src/varnish-3.0.4/bin'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/root/lnmp/src/varnish-3.0.4'
make: *** [all] Error 2
報(bào)錯(cuò)沒(méi)找到解決方法,選varnish-3.0.3
代碼如下:
wget http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz
tar xzf varnish-3.0.3.tar.gz
cd varnish-3.0.3
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
./configure --prefix=/usr/local/varnish --enable-debugging-symbols --enable-developer-warnings --enable-dependency-tracking --with-jemalloc
make && make install
/usr/bin/install -m 755 ./redhat/varnish.initrc /etc/init.d/varnish
/usr/bin/install -m 644 ./redhat/varnish.sysconfig /etc/sysconfig/varnish
/usr/bin/install -m 755 ./redhat/varnish_reload_vcl /usr/local/varnish/bin
useradd -M -s /sbin/nologin varnish
代碼如下:
ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/
ln -s /usr/local/varnish/bin/varnish_reload_vcl /usr/bin/
ln -s /usr/local/varnish/bin/varnishadm /usr/bin/
代碼如下:
chkconfig --add varnish
chkconfig varnish on
生成varnish管理秘鑰:
代碼如下:
uuidgen > /usr/local/varnish/etc/varnish/secret
chmod 644 /usr/local/varnish/etc/varnish/secret
修改varnish啟動(dòng)配置:
代碼如下:
sed -i "s@^VARNISH_VCL_CONF=/etc/varnish/default.vcl@#VARNISH_VCL_CONF=/etc/varnish/default.vcl\nVARNISH_VCL_CONF=/usr/local/varnish/etc/varnish/linuxeye.vcl@" /etc/sysconfig/varnish
sed -i "s@^VARNISH_LISTEN_PORT=6081@#VARNISH_LISTEN_PORT=6081\nVARNISH_LISTEN_PORT=80@" /etc/sysconfig/varnish
sed -i "s@^VARNISH_SECRET_FILE=/etc/varnish/secret@#VARNISH_SECRET_FILE=/etc/varnish/secret\nVARNISH_SECRET_FILE=/usr/local/varnish/etc/varnish/secret@" /etc/sysconfig/varnish
sed -i "s@^VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin@#VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin\nVARNISH_STORAGE_FILE=/usr/local/varnish/var/varnish_storage.bin@" /etc/sysconfig/varnish
sed -i "s@^VARNISH_STORAGE_SIZE.*@VARNISH_STORAGE_SIZE=150M@" /etc/sysconfig/varnish
sed -i "s@^VARNISH_STORAGE=.*@VARNISH_STORAGE=\"malloc,${VARNISH_STORAGE_SIZE}\"@" /etc/sysconfig/varnish
假設(shè)你的服務(wù)器擁有多顆邏輯處理器,還可以做以下的設(shè)置:
/etc/sysconfig/varnish 里面還可以添加自定義的參數(shù),用”-p 參數(shù)“的方式添加,如:
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
-f ${VARNISH_VCL_CONF} \
-T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
-t ${VARNISH_TTL} \
-w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \
-u varnish -g varnish \
-S ${VARNISH_SECRET_FILE} \
-s ${VARNISH_STORAGE} \
-p thread_pools=2" #這里為添加項(xiàng)
Varnish啟動(dòng)后進(jìn)入后臺(tái)運(yùn)行,同時(shí)返回命令行狀態(tài)。需要注意的是,Varnish運(yùn)行時(shí)會(huì)同時(shí)啟動(dòng)兩個(gè)進(jìn)程,一個(gè)主進(jìn)程,一個(gè)是子進(jìn)程,如果子進(jìn)程出現(xiàn)問(wèn)題,主進(jìn)程將重新生成一個(gè)子進(jìn)程。
VCL配置
代碼如下:
/usr/local/varnish/etc/varnish/linuxeye.vcl
#通過(guò)backend定義了一個(gè)名稱為webserver的后端主機(jī),“.host”指定后端主機(jī)的IP地址或者域名,“.port”指定后端主機(jī)的服務(wù)端口。
backend webserver {
.host = "127.0.0.1";
.port = "8080";
}
#調(diào)用vcl_recv開始
sub vcl_recv {
if (req.restarts == 0) {
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;
}
}
#如果請(qǐng)求的類型不是GET、HEAD、PUT、POST、TRACE、OPTIONS、DELETE時(shí),進(jìn)入pipe模式。注意這里是“&&”的關(guān)系
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
return (pipe);
}
#如果請(qǐng)求的類型不是GET與HEAD,則進(jìn)入pass模式
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
return (pass);
} <strong></strong>#對(duì)linuxeye.com域名進(jìn)行緩存加速,這是個(gè)泛域名的概念,也就是所有以linuxeye.com結(jié)尾的域名都進(jìn)行緩存
if (req.http.host ~ "^(.*).linuxeye.com") {
set req.backend = webserver;
}
#對(duì)以.jsp、.do、php結(jié)尾以及帶有?的URL時(shí),直接從后端服務(wù)器讀取內(nèi)容
if (req.url ~ "\.(jsp|do|php)($|\?)") {
return (pass);
} else {
return (lookup);
}
}</p> <p>sub vcl_pipe {
return (pipe);
}</p> <p>sub vcl_pass {
return (pass);
}</p> <p>sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (hash);
}</p> <p>sub vcl_hit {
return (deliver);
}</p> <p>sub vcl_miss {
return (fetch);
}
代碼如下:
#對(duì)于請(qǐng)求類型是GET,并且請(qǐng)求的URL中包含upload,那么就進(jìn)行緩存,緩存的時(shí)間是300秒,即5分鐘
sub vcl_fetch {
if (req.request == "GET" && req.url ~ "^/upload(.*)$") {
set beresp.ttl = 300s;
}</p> <p> if (req.request == "GET" && req.url ~ "\.(png|gif|jpg|jpeg|bmp|swf|css|js|html|htm|xsl|xml|pdf|ppt|doc|docx|chm|rar|zip|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv|txt)$") {
unset beresp.http.set-cookie;
set beresp.ttl = 30d;
}
return (deliver);
}
代碼如下:
#下面是添加一個(gè)Header標(biāo)識(shí),以判斷緩存是否命中
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from demo.linuxeye.com";
} else {
set resp.http.X-Cache = "MISS from demo.linuxeye.com";
}
return (deliver);
}
代碼如下:
#使用vcl_error可以定制一個(gè)錯(cuò)誤頁(yè)面
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
set obj.http.Retry-After = "5";
synthetic {"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>"} + obj.status + " " + obj.response + {"</title>
</head>
<body>
<h1>Error "} + obj.status + " " + obj.response + {"</h1>
<p>"} + obj.response + {"</p>
<h3>Guru Meditation:</h3>
<p>XID: "} + req.xid + {"</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
"};
return (deliver);
}</p> <p>sub vcl_init {
return (ok);
}</p> <p>sub vcl_fini {
return (ok);
}
檢查VCL配置是否正確:
代碼如下:
service varnish configtest
或
代碼如下:
varnishd -C -f /usr/local/varnish/etc/varnish/linuxeye.vcl
啟動(dòng)varnish:
代碼如下:
service varnish start
查看varnish狀態(tài):
代碼如下:
service varnish status
動(dòng)態(tài)加載VCL配置:
代碼如下:
service varnish reload
停止varnish:
代碼如下:
service varnish stop
查看當(dāng)前varnish監(jiān)聽的80端口:
代碼如下:
# netstat -tpln | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 15249/varnishd
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 19468/nginx
tcp 0 0 :::80 :::* LISTEN 15249/varnishd
查看varnish進(jìn)程:
代碼如下:
# ps -ef | grep varnishd | grep -v grep
root 15248 1 0 11:47 ? 00:00:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -f /usr/local/varnish/etc/varnish/linuxeye.vcl -T 127.0.0.1:6082 -t 120 -w 50,1000,120 -u varnish -g varnish -S /usr/local/varnish/etc/varnish/secret -s malloc,150M
varnish 15249 15248 0 11:47 ? 00:00:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -f /usr/local/varnish/etc/varnish/linuxeye.vcl -T 127.0.0.1:6082 -t 120 -w 50,1000,120 -u varnish -g varnish -S /usr/local/varnish/etc/varnish/secret -s malloc,150M
Varnish訪問(wèn)日志
varnishncsa可以使用NCSA通用日志格式(NCSA Common Log Format)將HTTP請(qǐng)求記錄到日志文件.
代碼如下:
/usr/bin/install -m 755 ./redhat/varnishncsa.initrc /etc/init.d/varnishncsa
chmod +x /etc/init.d/varnishncsa
chkconfig varnishncsa on
mkdir -p /usr/local/varnish/logs
編輯varnishncsa啟動(dòng)配置
代碼如下:
ln -s /usr/local/varnish/bin/varnishncsa /usr/bin
sed -i 's@^logfile.*@logfile="/usr/local/varnish/logs/varnishncsa.log"@' /etc/init.d/varnishncsa
啟動(dòng)varnishncsa:
代碼如下:
service varnishncsa start
使用logrotate輪詢?nèi)罩疚募?每天輪詢):
代碼如下:
cat > /etc/logrotate.d/varnish << EOF
/usr/local/varnish/logs/varnishncsa.log {
daily
rotate 5
missingok
dateext
compress
notifempty
sharedscripts
postrotate
[ -e /var/run/varnishncsa.pid ] && kill -USR1 \`cat /var/run/varnishncsa.pid\`
endscript
}
EOF
日志輪詢debug測(cè)試:
代碼如下:
logrotate -df /etc/logrotate.d/varnish
看過(guò)“ Varnish緩存服務(wù)器在linux系統(tǒng)下怎么安裝 ”的人還看了:
2.Linux操作系統(tǒng)
4.初學(xué)者學(xué)習(xí)linux操作系統(tǒng),用哪個(gè)版本比較好
5.Linux操作系統(tǒng)基礎(chǔ)知識(shí)