varnish 配置執行個體:
backend web1 {
.host = "*.*.*.*";
.port = "8080";
.connect_timeout = 1s;
.first_byte_timeout = 5s;
.between_bytes_timeout = 2s;
}
backend web2 {
.host = "127.0.0.1";
director server round-robin {
{.backend=web1;}
{.backend=web2;}
acl purge {
"localhost";
"127.0.0.1";
"10.0.0.0"/8;
sub vcl_recv {
set req.backend = server;
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.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|jpeg|flv)" ) {
remove req.http.Accept-Encoding;
remove req.http.Cookie;
} else if (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} else if (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
#移除一些特定格式的cookie
if (req.url ~ "^(.*)\.(jpg|png|gif|jpeg|flv|bmp|gz|tgz|bz2|tbz|js|css|html|htm)($|\?)" ) {
#移除cookie,以便能緩存到varnish
unset req.http.cookie;
}
}
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 {
#更新緩存的請求,提示緩存不存在 Not in cache
if (req.request == "PURGE") {
error 404 "Not in cache.";
#既然緩存不存在了,就去後端取吧
return (fetch);
sub vcl_fetch {
if (req.request == "GET" && req.url ~ "\.(gif|jpg|swf|css|png|jpg|jpeg|gif|png|tiff|tif|svg|swf|ico|doc|ppt|pps|xls|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw|txt|mp3|html|fnt|jsp)") {
remove beresp.http.Set-Cookie;
set beresp.ttl = 1 d;
} else if (req.request == "GET" && req.url ~ "\.(css|js|gzjs|json)") {
set beresp.ttl = 120 s;
if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Vary == "*") {
/*
* Mark as "Hit-For-Pass" for the next 2 minutes
*/
set beresp.ttl = 120 s;
return (hit_for_pass);
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT-h5-izhangxin";
#統計命中了多少次
set resp.http.X-Cache-Hits = obj.hits;
} else {
set resp.http.X-Cache = "MISS-h5-izhangxin";
#去掉 varnish 中的一些頭資訊(如果你不想保留的話,建議保留 Age,友善檢視)
remove resp.http.X-Varnish;
remove resp.http.Via;
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>
<p>Varnish cache server</p>
</body>
</html>
"};
sub vcl_init {
return (ok);
sub vcl_fini {

本文轉自 蔡小趙 51CTO部落格,原文連結:http://blog.51cto.com/zhaopeiyan/1830299