centos/redhat
1、安装
#rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm
#yum install varnish
2、配置
default.vcl
# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition. Set this to point to your content
# server.
#
backend default {
.host = "192.168.75.122";
.port = "80";
.probe = {
.url = "/";
.interval = 5s;
.timeout = 1 s;
.window = 5;
.threshold = 3;
}
}
backend test {
.host = "192.168.0.83";
.port = "80";
#.probe = {
# .url = "/";
# .interval = 5s;
# .timeout = 1 s;
# .window = 5;
# .threshold = 3;
# }
}
#director ecae_director round-robin {
#{
# .backend = default;
#}
#{
# .backend = test;
#}
#}
acl purge {
"localhost";
"192.168.210.0"/24;
"192.168.51.0"/24;
}
#
# Below is a commented-out copy of the default VCL logic. If you
# redefine any of these subroutines, the built-in logic will be
# appended to your code.
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return(lookup);
}
if (req.http.host ~ "(?i)^(images.)?test.cn$") {
set req.http.host = "images.test.cn";
set req.backend = test;
} elsif (req.http.host ~ "(?i)^images2.test.cn$") {
set req.http.host = "images2.test.cn";
set req.backend = test;
} else {
error 404 "Unknown virtual host";
}
if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
unset req.http.cookie;
set req.url = regsub(req.url, "\?.*$", "");
}
if (req.http.cookie) {
unset req.http.cookie;
}
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
# No point in compressing these
remove req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
# unknown algorithm
remove req.http.Accept-Encoding;
}
}
return (lookup);
}
#
sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
}
#
sub vcl_miss {
if (req.request == "PURGE") {
error 404 "Not in cache.";
}
}
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_fetch {
if (req.request == "GET") {
unset beresp.http.set-cookie;
}
if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
unset beresp.http.set-cookie;
set beresp.ttl = 365d;
}
}
#
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
3、启动
varnishd -f /etc/varnish/default.vcl -s malloc,3G -w 8,500,300 -T 127.0.0.1:2000 -a 0.0.0.0:80
本文转自yuangang_love 51CTO博客,原文链接:http://blog.51cto.com/linux008/1195549,如需转载请自行联系原作者