Varnish cache setup
Installation on Debian
echo "deb http://repo.varnish-cache.org/debian/ squeeze varnish-3.0" >> /etc/apt/sources.list
apt-get update
apt-get install varnish
配置默认的端口:
nano /etc/default/varnish
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"
配置default.vcl文件
nano /etc/varnish/default.vcl
配置文件代码如下,唯一需要修改的是www.yyphs.com改为你的博客域名,如果有多个,可以使用|分隔。
backend default {
.host = "服务器的ip";
.port = "80";
}
acl purge {
"localhost";
"127.0.0.1";
}sub vcl_recv {
# Only cache the following site
if (req.http.host ~ "(www.yyphs.com)") {
set req.backend = default;
} else {
return (pass);
}
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return (lookup);
}
if (req.restarts == 0) {
if (req.http.x-forwarded-for) {
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") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.url ~ "^/wp-content") {
unset req.http.cookie;
}
if (req.http.Authorization || req.http.Cookie ~ "wordpress_logged" || req.http.Cookie ~ "comment_") {
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 {
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
return (deliver);
}sub vcl_miss {
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
return (fetch);
}sub vcl_fetch {
if (req.url ~ "^/wp-content") {
unset beresp.http.set-cookie;
}
if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Vary == "*") {
set beresp.ttl = 120 s;
return (hit_for_pass);
}
set beresp.ttl = 7d;
return (deliver);
}sub vcl_deliver {
return (deliver);
}sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
set obj.http.Retry-After = "5";
synthetic {"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>"} + obj.status + " " + obj.response + {"</title>
</head>
<body>
<h1>Error "} + obj.status + " " + obj.response + {"</h1>
<p>"} + obj.response + {"</p>
<h3>Guru Meditation:</h3>
<p>XID: "} + req.xid + {"</p>
<hr>
&nbs
p; <p>Varnish cache server</p>
</body>
</html>
"};
return (deliver);
}sub vcl_init {
return (ok);
}sub vcl_fini {
return (ok);
}
配置完成之后,启动varnish。
service varnish start
使用命令 varnishlog -c 查看varnish和客户端之间的通信
使用命令 varnishlog -b 查看varnish和nginx,apache,lighttpd或其他后端web服务器之间的通信
一次前后端事务的处理过程日志
查看Varnish的命中和丢失情况
varnishhist
竖线代表命中,#代表丢失
查看varnish的实时统计
varnishstat