作者:小丹巛丹布莱妮 | 来源:互联网 | 2014-05-28 12:03
配置环境:Centos6.3x86编译安装varnish:yuminstall-yautomakeautoconflibtoolncurses-devellibxsltgroffpcre-develpkgconfigyuminstall-yautomakeautoconflibtoolncurses-devellibx
配置环境 : Centos 6.3
x86
编译安装varnish:
yum install -y automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig
wget
http://repo.varnish-cache.org/source/varnish-3.0.4.tar.gz
tar
zxvf varnish-3.0.4.tar.gz
cd varnish-3.0.4
./configure --prefix=/usr/local/varnish
make && make install
配置缓存存放+日志地址
mkdir -p /data/varnish/
chmod +w /data/varnish/
chown -R www:www /data/varnish/ ### 以网站权限运行
编辑配置文件:
[root@server110 ~]# vim /usr/local/varnish/default.vcl
backend web1 {
.host = "192.168.101.112";
.port = "80";
.connect_timeout = 1s; #连接超时时间
.first_byte_timeout = 8s;
.between_bytes_timeout = 5s;
}
#backend web2 {
# .host = "xx.xx.xx.xx";
# .port = "80";
#}
acl purge {
"localhost";
"127.0.0.1";
"192.168.101.0"/24;
}
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purge)
{
error 405 "Not allowed.";
}
return(lookup);
}
if (req.http.host ~ "^www.server110.com") { #web1对应的域名
set req.backend = web1;
}
#elseif (req.http.host ~ "^(www)|(my).baidu.com") { #web2对应的域名
# set req.backend = web2;
#}
else {
error 404 "Caesar's cache-server ! Email:root@server110.com"; #如果域名不在以上范围的出错提示
#set req.backend = web1;
}
if (req.request != "GET" && req.request != "HEAD") {
return(pipe);
}
elseif (req.url ~ "\.(php|cgi)($|\?)") #动态页面直接通过,不缓存
{
return(pass);
}
return(lookup);
}
sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
}
sub vcl_miss
{
return (fetch);
}
##让varnish服务器缓存的类型,从后端取得数据后调用
sub vcl_fetch
{
##对访问中get有包含jpg,png等格式的文件进行缓存,缓存时间为7天,s为秒
if (req.request == "GET" && req.url ~ "\.(js|css|mp3|jpg|png|gif|swf|jpeg|ico)$")
{
set beresp.ttl = 7d;
}
##对访问get中包含htm等静态页面,缓存300秒
if (req.request == "GET" && req.url ~ "\/[0-9]\.htm$")
{
set beresp.ttl = 300s;
}
return (deliver);
}
####添加在页面head头信息中查看缓存命中情况########
sub vcl_deliver
{
set resp.http.x-hits = obj.hits ;
if (obj.hits > 0)
{
set resp.http.X-Cache = "HIT cqtel-bbs";
}
else
{
set resp.http.X-Cache = "MISS cqtel-bbs";
}
}
参数:
-u 以什么用运行
-g 以什么组运行
-f varnish配置文件
-a 绑定IP和端口
-s varnish缓存文件位置与大小
-w 最小,最大线程和超时时间
-t 缓存时间s
-T varnish管理端口,主要用来清除缓存
-p client_http11=on 支持http1.1协议
-P(大P) /usr/local/varnish/var/varnish.pid 指定其进程码文件的位置,实现管理
停止varnish
pkill varnishd ###结束varnishd进程
启动 varnish
/usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/default.vcl -a 192.168.101.111:80 -s file,/data/varnish/cache/varnish_cache.data,1G -w 1024,51200,10 -t 3600 -T 192.168.101.111:3000
追加 varnish 启动项到开机启动
echo "/usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/default.vcl -a 192.168.101.111:80 -s file,/data/varnish/cache/varnish_cache.data,1G -w 1024,51200,10 -t 3600 -T 192.168.101.111:3000" >> /etc/rc.local
启动日志,方便分析网站访问情况
/usr/local/varnish/bin/varnishncsa -w /data/varnish/logs/varnish.log & echo "/usr/local/varnish/bin/varnishncsa -w /data/varnish/logs/varnish.log &" >> /etc/rc.local
参数: -w 指定varnish访问日志要写入的目录与文件
varnish日志切割
|
[root@server110 ~]# vim /data/shell/cut_varnish_log.sh
#!/bin/sh
logs_path=/data/varnish/logs
vlog=${logs_path}/varnish.log
date=$(date -d "yesterday" +"%Y-%m-%d")
pkill -9 varnishncsa
mkdir -p ${logs_path}/$(date -d "yesterday" +"%Y")/$(date -d
"yesterday" +"%m")/
mv /data/varnish/logs/varnish.log ${logs_path}/$(date -d
"yesterday" +"%Y")/$(date -d "yesterday"
+"%m")/varnish-${date}.log
/usr/local/varnish/bin/varnishncsa -w
/data/varnish/logs/varnish.log &
使用计划任务,每天晚上凌晨00点运行日志切割脚本
echo "0 0 * * * /data/shell/cut_varnish_log.sh" >>
/etc/crontab
|
其他设置 {清除缓存,命中率等 }
/usr/local/varnish/bin/varnishadm -T 192.168.101.111:3000 purge "req.http.host ~ www.server110.com$ && req.url ~ /static/image/tp.php"
详细介绍:
192.168.101.111:3000 ###为被清除缓存服务器地址
www.server110.com ###为被清除的域名
/static/image/tp.php ###为被清除的url地址列表
清除所有缓存
/usr/local/varnish/bin/varnishadm -T 192.168.9.201:3000 url.purge *$
清除image目录下所有缓存
/usr/local/varnish/bin/varnishadm -T 192.168.9.201:3000 url.purge /image/
查看Varnish服务器连接数与命中率
/usr/local/varnish/bin/varnishstat ?n /data/varnish/cache/varnish_cache.data