采用redhat,rpm安装方式。
rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm
yum install varnish
配置文件在
/etc/varnish/default.vcl 和 /etc/sysconfig/varnish
以下为我的配置文件。
# 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 webserver1 {
.host = “8.8.8.8”;
.port = “81”;
.probe = {
.url = “/”;
.interval = 5s;
.timeout = 5 s;
.window = 10;
.threshold = 8;
}
}
backend webserver2 {
.host = “8.8.8.8”;
.port = “83”;
.probe = {
.url = “/”;
.interval = 5s;
.timeout = 5 s;
.window = 10;
.threshold = 8;
}
}
#director webserver round-robin {
# { .backend = webserver1; }
# { .backend = webserver2; }
#}
director webserver random {
.retries = 6;
{
.backend = webserver1;
.weight = 2;
}
{
.backend = webserver2;
.weight = 2;
}
}
#director webserver random {
# { .backend = www; .weight = 2; }
# { .backend = { .host = “www2.example.com”; .port = “http”; } .weight = 1; }
#}
acl purge {
“localhost”;
“127.0.0.1”;
}
# 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 {
# Only cache the following site
# if (req.http.host ~ “(app.iamle.com)”) {
# set req.backend = webserver;
# } else {
# return (pass);
# }
if (req.http.host ~ “^(.*?)iamle.com$”) {
set req.backend = webserver;
}
else {
error 404 “Unknown HostName!”;
}
if (req.request == “PURGE”) {
if (!client.ip ~ purge) {
error 405 “Not allowed.”;
}
return (lookup);
}
if (req.request ==”GET”&&req.url ~ “(?i)\.php($|\?)”){
return (pass);
}
if (req.request == “GET” && req.url ~ “\.(js|css|jpg|png|gif|swf|flv|ico|jpeg|htm|html)$”) {
unset req.http.cookie;
}
# if (req.url ~ “^/[^?]+\.(jpeg|jpg|png|gif|ico|js|css|swf|flv|htm|html|shtm)(\?.*|)$”) {
# unset req.http.cookie;
# set req.url = regsub(req.url, “\?.*$”, “”);
# }
if (req.url ~ “^/$”) {
unset req.http.cookie;
}
# if (req.request ==”GET”&&req.url ~ “(?i)\.shtml($|\?)”){
# unset req.http.cookie;
# }
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.http.Authorization || req.http.Cookie ~ “wordpress_logged” || req.http.Cookie ~ “comment_”) {
# /* Not cacheable by default */
# return (pass);
# }
# nocache authorization
# if (req.http.Authorization) {
# return (pass);
# }
if (req.http.Authorization || req.http.Cookie) {
return (pass);
}
return (lookup);
}
sub vcl_pipe {
# Note that only the first request to the backend will have
# X-Forwarded-For set. If you use X-Forwarded-For and want to
# have it set for all requests, make sure to have:
# set bereq.http.connection = “close”;
# here. It is not set by default as it might break some broken web
# applications, like IIS with NTLM authentication.
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 ~ “^/$”) {
unset beresp.http.set-cookie;
set beresp.ttl = 10m;
set beresp.do_gzip = true;
unset beresp.http.Cache-Control;
unset beresp.http.Pragma;
set beresp.http.Cache-Control = “max-age=600”;
unset beresp.http.Expires;
}
# if (req.request ==”GET”&&req.url ~ “(?i)\.shtml($|\?)”){
# unset beresp.http.set-cookie;
# set beresp.ttl = 10m;
# set beresp.do_gzip = true;
# unset beresp.http.Cache-Control;
# unset beresp.http.Pragma;
# set beresp.http.Cache-Control = “max-age=600”;
# unset beresp.http.Expires;
# }
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);
}
# if (beresp.http.Pragma ~ “no-cache” ||
# beresp.http.Cache-Control ~ “no-cache” ||
# beresp.http.Cache-Control ~ “private”) {
# return(deliver);
# }
if(beresp.status == 404 || beresp.status == 300) {
error 404;
}
if (req.request == “GET” && req.url ~ “\.(jpg|png|gif|swf|flv|ico|jpeg)$”) {
set beresp.ttl = 3h;
}
# if (req.request == “GET” && req.url ~ “\.(htm|html|shtml)$”) {
# set beresp.ttl = 1h;
# }
set beresp.ttl = 1h;
return (deliver);
}
sub vcl_deliver {
set resp.http.x-hits = obj.hits;
if (obj.hits > 0) {
set resp.http.X-Cache = “cached”;
} else {
set resp.http.x-Cache = “uncached”;
}
# Remove some headers: PHP version
unset resp.http.X-Powered-By;
# Remove some headers: Apache version & OS
unset resp.http.Server;
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>iamle.com cache server”} + 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>www.iamle.com cache server</p>
</body>
</html>
“};
return (deliver);
}
sub vcl_init {
return (ok);
}
sub vcl_fini {
return (ok);
}