最近两天,对varnish缓存研究了一下其性能,在官方网页上中介绍varnish的高级特性在linux 2.6, FreeBSD 6/7 and Solaris 10上有着非常高效的性能。一些基本的特性包括有:
. A modern design
. VCL - a very flexible configuration language
. Load balancing with health checking of backends
. Partial support for ESI
. URL rewriting
. Graceful handling of "dead" backends
. etc
1、安装:
# cd varnish-version
# ./configure --prefix=/opt/varnish --enable-debugging-symbols --enable-developer-warnings --enable-dependency-tracking
# make
# make install
2、配置VCL:
例如:
backend b1 {
.host = "192.168.16.86";
.port = "8080";
}
sub vcl_recv {
if (req.url ~ "\.(png|gif|jpg|swf|css|jsp|txt|js|jpge)$") {
lookup;
unset req.http.authenticate;
}
if (req.http.Cache-Control ~ "no-cache"){
purge_url(req.url);
}
set req.backend = b1;
}
如果有多个虚拟主机,可以参考下面的方式:
backend gpw{
.host="www.xxxx1.com";
.port="8000";
}
backend gpw2{
.host="xxx2.com";
.port="8000";
}
backend gpw3{
.host="bbs.xxx3.com";
.port="8000";
}
sub vcl_recv{
if (req.url == "index.php") {
set req.url = "index.php";
}
if (req.url ~ "\.(gif|jpg|swf|css|jpeg|png|js)$") {
lookup;
unset req.http.authenticate;
}
if (req.http.host ~ "^(www.)?xxx1.com$"){
set req.backend=gpw;
}elsif(req.http.host ~ "^xxx2.com$") {
set req.backend=gpw2;
}else{
set req.backend=gpw3;
}
}
sub vcl_hash {
set req.hash += req.url;
if (req.http.host) {
set req.hash += req.http.host;
} else {
set req.hash += server.ip;
}
hash;
}
sub vcl_fetch {
if ( req.request == "GET" && req.url ~ "\.(jpg|png|jpge|swf|css|gif|jsp|txt|js)$"){
set obj.ttl = 3600s;
unset obj.http.set-cookie;
} else {
set obj.ttl = 1d;
}
}
sub vcl_hit {
if (!obj.cacheable) {
pass;
}
deliver;
}
sub vcl_miss {
if (req.http.user-agent ~ "spider") {
error 503 "Not presently in cache";
}
fetch;
}
sub vcl_deliver {
if (obj.hits > 0){
set resp.http.X-Cache="HIT";
}else{
set resp.http.X-Cache="MISS";
}
}
sub vcl_pass { pass;}
sub vcl_discard { discard;}
sub vcl_timeout { discard;}
3.启动:
# cd /opt/varnishd/sbin/,使用 varnishd启动.参数有:
-a address:port # HTTP listen address and port
-b address:port # backend address and port
# -b
# -b '
-d # debug
-f file # VCL script
-F # Run in foreground
-h kind[,hashoptions] # Hash specification
# -h simple_list
# -h classic [default]
# -h classic,
-l bytesize # Size of shared memory log
-n dir # varnishd working directory
-P file # PID file
-p param=value # set parameter
-s kind[,storageoptions] # Backend storage specification
# -s malloc
# -s file [default: use /tmp]
# -s file,
# -s file,
# -s file,
-t # Default TTL
-T address:port # Telnet listen address and port
-V # version
-w int[,int[,int]] # Number of worker threads
# -w
# -w min,max
# -w min,max,timeout [default: -w2,500,300]
-u user # Priviledge separation user id
服务器 param的设置
param有以下选项
user root (0)
group root (0)
default_ttl 14400 [seconds]
thread_pools 1 [pools]
thread_pool_max 12000 [threads]
thread_pool_min 4000 [threads]
thread_pool_timeout 10 [seconds]
overflow_max 100 [%]
http_workspace 8192 [bytes]
sess_timeout 5 [seconds]
pipe_timeout 60 [seconds]
send_timeout 20 [seconds]
auto_restart on [bool]
fetch_chunksize 128 [kilobytes]
sendfile_threshold unlimited [bytes]
vcl_trace off [bool]
listen_address 172.16.189.1:3128
listen_depth 1024 [connections]
srcaddr_hash 1049 [buckets]
srcaddr_ttl 720 [seconds]
backend_http11 on [bool]
client_http11 on [bool]
ping_interval 3 [seconds]
有关性能上的调节,可查看 http://varnish.projects.linpro.no/wiki/Performance
# ./varnishd -a 192.168.16.86:80 -d -f ../etc/varnish/qing.vcl.conf -s malloc -p thread_pool_max=4000 -h classic,500009 -p obj_workspace=4096
如果要要使用varnishadm,在启动行加上 -T ip:port
4.查看性能状态: varnishstat, varnishtop,官方有一个Varnish web GUI的查看模式,可以参考 http://varnish.projects.linpro.no/wiki/WebGui
5.查看日志: varnishlog

No comments:
Post a Comment