groupadd www
groupadd www
useradd -g www www
mkdir /var/cache
mkdir /data/cache
chown -R www:www /data/cache/
mkdir -p /data/logs
chmod +w /data/logs/
chown -R www:www /data/logs/
tar -xzf varnish-2.0.4.tar.gz
cd varnish-2.0.4
./autogen.sh
autoconf
./configure --prefix=/usr/local/varnish
make
make install
cd /usr/local/varnish/etc/
cd varnish/
vi default.vcl
backend default {
.host = "127.0.0.1";
.port = "80";
}
sub vcl_recv {
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
return (pass);
}
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 (!obj.cacheable) {
return (pass);
}
return (deliver);
}
sub vcl_miss {
return (fetch);
}
sub vcl_fetch {
if (req.request == "GET" && req.url ~ "\.(jpg|gif|png|css|js)$") {
set obj.ttl = 3600s;
}
else {
set obj.ttl = 3d;
}
}
# if (!obj.cacheable) {
# return (pass);
# }
# if (obj.http.Set-Cookie) {
# return (pass);
# }
# set obj.prefetch = -30s;
# return (deliver);
sub vcl_deliver {
return (deliver);
}
sub vcl_discard {
return (discard);
}
sub vcl_prefetch {
return (fetch);
}
sub vcl_timeout {
return (discard);
}
启动:
/usr/local/varnish/sbin/varnishd -n /data/cache/ -f /usr/local/varnish/etc/varnish/default.vcl -a 0.0.0.0:80 -s file,/data/cache/varnish_cache.data,100M -g www -u www -w 300,512,10 -T 127.0.0.1:3500 -p client_http11=on
创建日志
/usr/local/varnish/bin/varnishncsa -n /data/cache/ -w /data/logs/varnish.log &
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、优化Linux内核参数
vi /etc/sysctl.conf
在末尾增加以下内容:
引用
net.ipv4.tcp_fin_timeout = 30
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服务器连接数与命中率:
/usr/local/varnish/bin/varnishstat
2、通过Varnish管理端口进行管理:
用help看看可以使用哪些Varnish命令:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 help
引用
Available commands:
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地址):
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /a/
(2)、例:清除类似
http://blog.s135.com/tech的URL地址:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge w*$
(3)、例:清除所有缓存:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge *$
4、一个清除Squid缓存的PHP函数(清除Varnish缓存同样可以使用该函数,无需作任何修改,十分方便):
- 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官方网站:
附2:2007年12月10日,我写了一个每天0点运行,按天切割Varnish日志,生成一个压缩文件,同时删除上个月旧日志的脚本(/var/logs/cutlog.sh):
/var/logs/cutlog.sh文件内容如下:
引用
#!/bin/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定时执行:
/usr/bin/crontab -e
或者
vi /var/spool/cron/root
输入以下内容:
引用
0 0 * * * /bin/sh /var/logs/cutlog.sh
参考:http://blog.s135.com/post/313/
阅读(707) | 评论(1) | 转发(0) |