1、創建www用戶和組,以及Varnish緩存文件存放目錄(/var/vcache):
/usr/sbin/groupadd www -g 48
/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/):
mkdir -p /var/logs
chmod +w /var/logs
chown -R www:www /var/logs
3、編譯安裝varnish:
wget http://blog.s135.com/soft/linux/varnish/varnish-1.1.2.tar.gz
tar zxvf varnish-1.1.2.tar.gz
cd varnish-1.1.2
./configure --prefix=/usr/local/varnish
make && make install
4、創建Varnish配置文件:
vi /usr/local/varnish/vcl.conf
backend myblogserver {
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 "Alvin.zeng 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域名的請求則返回“Alvin.zeng Cache Server”;
(4)、Varnish對HTTP協議中的GET、HEAD請求進行緩存,對POST請求透過,讓其直接訪問後端Web服務器。之所以這樣配置,是因爲POST請求一般是發送數據給服務器的,需要服務器接收、處理,所以不緩存;
(5)、Varnish對以.txt和.js結尾的URL緩存時間設置1小時,對其他的URL緩存時間設置爲30天。
5、啓動Varnish
ulimit -SHn 51200
/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
報錯方式二:
varnishd -n /usr/local/services/varnish/var/varnish/test-1/ -f /usr/local/services/varnish/vcl.conf -a 0.0.0.0:80 -s file,/opt/vcahce/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訪問日誌寫入日誌文件:
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/varnish.log &
7、配置開機自動啓動Varnish
vi /etc/rc.local
ulimit -SHn 51200
/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、查看Varnish服務器連接數與命中率:
/usr/local/varnish/bin/varnishstat
9、通過Varnish管理端口進行管理:
用help看看可以使用哪些Varnish命令:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 help
說明其它:
TCP連接數Varnish要比Squid少,因爲Varnish的TCP連接釋放要比Squid快。
但同時處理的請求數Varnish要比Squid高一些,,Varnish實時處理的請求數比Squid多1倍,平均處理的請求數也比Squid多100餘個:
/usr/local/webserver/varnish/bin/varnishstat
-----------------------------------------------------------
70979868 580.97 356.55 Client requests received
70897998 580.97 356.14 Cache hits
/usr/local/squid/bin/squidclient -p 80 mgr:5min
-----------------------------------------------------------
client_http.requests = 248.425264/sec
client_http.hits = 245.135282/sec
如果正常的話,vcache這個目錄裏只有一個大小爲1G的文件:varnish_cache.data