系统为ubuntu 10.04
安装:
apt-get install varnish
配置:
主 配置文件为 /etc/varnish/default.vcl,启动脚本文件/etc/init.d/varnish,启动配置文件/etc/default/varnish
备份默认的主配置文件 mv /etc/varnish/default.vcl /etc/varnish/default.vcl_bak
新建主配置文件 vim /etc/varnish/default.vcl
一个简单的配置文件:
backend webserver {
.host = "10.1.1.223"; #后端服务器地址
.port = "80"; #端口
.pore = { #后端服务器健康状态检查
.url = "/";
.interval = 5s; #时间间隔
.timeout = 1 s; #
.window = 5;
.threshold = 3;
.initial = 3;
.expected_response = 200; #http返回的状态
}
}
backend webserver2 {
.host = "localhost"; #后端服务器地址10.1.1.198
.port = "8080"; #端口
.pore = { #后端服务器健康状态检查
.url = "/";
.interval = 5s;
.timeout = 1 s;
.window = 5;
.threshold = 3;
.initial = 3;
.expected_response = 200;
}
}
backend testweb1 {
.host = "10.1.1.222";
.port = "80";
.pore = {
.url = "/";
.interval = 5s;
.timeout = 1 s;
.threshold = 3;
.initial = 3;
.expected_response = 200;
}
}
backend testweb2 {
.host = "10.1.1.226";
.port = "80";
.pore = {
.url = "/";
.interval = 5s;
.timeout = 1 s;
.threshold = 3;
.initial = 3;
.expected_response = 200;
}
}
director testweb round-robin { #round-robin为循环模式,random为随机模式其中权重设置为 .weight = 1;数字表示权重值。
{ .backend = testweb1; }
{ .backend = testweb2; }
}
acl purgeallow { #允许清除缓存地址
"127.0.0.1";
"localhost";
}
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purgeallow) {
error 405 "Not Allow";
}
return (lookup);
}
if (req.http.host ~ "10.1.1.198") { #访问114.111.161.23时,指向后端服务webserver2
set req.backend = webserver2;
}
if (req.http.host ~ "www.mytest.cn") { #访问www.mytest.cn,指向后端testweb集群。
set req.backend = testweb;
}
if (req.http.host ~ "^(www.)?mywebserver.com.cn") { #访问这个域名时,指向webserver后端服务器
set req.backend = webserver;
if (req.request != "GET" && req.request != "HEAD") {
return (pipe);
}
else {
return (lookup);
}
}
if (req.restarts == 0) {
if (req.http.x-forwarded-for) { #配置后端服务器获得客户端访问的真实ip
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For + ", " + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
}
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") {
return (lookup);
}
if (req.http.Authorization || req.http.Cookie) {
return (pass);
}
#if ( !( req.url ~ ^/ezdemo_admin/) ) { #非 /ezdemo_admin/ 目录下访问删除cookie
# unset req.http.Cookie;
#}
#if (req.url ~ "\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz|js|css|html|htm|php)$") { #以这些结尾文件删除cookie
# unset req.http.cookie;
#}
if (req.url ~ "\.(html|php|js|css|mp3|jpg|png|gif|swf|flv|jpeg|ico)$") { #以这些为结尾的文件进行缓存
return (lookup);
}
if (req.http.Cache-Control ~ "no-cache" || req.http.Pragma ~ "no-cache") { #http头中cache-control或pragma 符合no-cache不缓存
return (pass);
}
return (lookup);
}
sub vcl_pipe {
return (pipe);
}
sub vcl_pass {
return (pass);
}
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (hash);
}
sub vcl_hit {
return (deliver);
}
sub vcl_miss {
return (fetch);
}
sub vcl_fetch {
if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Vary == "*") {
set beresp.ttl = 1200 s;
return (hit_for_pass);
}
if (beresp.http.Cache-Control ~ "no-cache" || beresp.http.Pragma ~ "no-cache") {
return (pass);
}
if (req.request == "GET" && req.url ~ "\.(html|php|js|css)$") {
#unset beresp.http.Set-Cookie;
set beresp.ttl = 1800s;
}
if (req.request == "GET" && req.url ~ "\.(mp3|jpg|png|gif|swf|jpeg|ico)$") {
#unset beresp.http.Set-Cookie;
set beresp.ttl = 7d;
}
return (deliver);
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT FROM CLOUDSTYLE";
}
else {
set resp.http.X-Cache = "MISS FROM CLOUDSTYLE";
}
return (deliver);
}
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
set obj.http.Retry-After = "5";
synthetic {"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Error "} + obj.status + " " + obj.response + {"
"} + obj.response + {"
Guru Meditation:
XID: "} + req.xid + {"
Varnish cache server
"};
return (deliver);
}
sub vcl_init {
return (ok);
}
sub vcl_fini {
return (ok);
}
修改启动配置文件/etc/default/varnish 将 DAEMON_OPTS值改为如下
DAEMON_OPTS="-a :80 \ #默认为6081
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,1G #默认为file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,1G"
启动varnish
/etc/init.d/varnish start
或是如下命令启动
varnishd -P /var/run/varnishd.pid -a 0.0.0.0:80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s file,/var/lib/varnish/ubuntu/varnish_storage.bin,1G #( or -s malloc,1G)
启动日志
varnishncsa -a -w /usr/local/varnish/logs/varnish.log &
清除缓存
varnishadm -S /etc/varnish/secret -T localhost:6082 purge.url(ban.url) ^.*$ #清除所有缓存
附录:
pass 当一个请求被pass后,这个请求将通过varnish转发到后端服务器,但是不会被缓存。
pass可以放在vcl_recv 和 vcl_fetch中
lookup 当一个请求在vcl_recv中被lookup后,varnish将从缓存中提取数据,如果缓存中没有数据,将被设置为pass,不能在vcl_fetch中设置lookup。
pipe pipe和pass相似,都要访问后端服务器,不过当进入pipe模式后,在此连接未关闭前,后续的所有请求都发到后端服务器(这句是我自己理解后简化的,有能力的朋友可以看看官方文档,给我提修改建议)。
deliver 请求的目标被缓存,然后发送给客户端
esi ESI-process the fetched document(我理解的就是vcl中包换一段html代码)
varnish 参考
https://www.varnish-cache.org/docs/3.0/reference/varnishd.html