|
vcl 4.0;
import directors;
# probe
probe www_probe {
.url = "/";
}
# Default backend
backend default {
.host = "192.168.0.31";
.port = "80";
}
# backend server
backend web1 {
.host = "192.168.0.31";
.port = "80";
.connect_timeout = 2s;
.first_byte_timeout = 10s;
.between_bytes_timeout = 1s;
.probe = www_probe;
}
backend web2 {
.host = "192.168.0.32";
.port = "80";
.connect_timeout = 2s;
.first_byte_timeout = 10s;
.between_bytes_timeout = 1s;
.probe = www_probe;
}
backend img1 {
.host = "192.168.0.33";
.port = "80";
.connect_timeout = 2s;
.first_byte_timeout = 10s;
.between_bytes_timeout = 1s;
.probe = www_probe;
}
backend img2 {
.host = "192.168.0.34";
.port = "80";
.connect_timeout = 2s;
.first_byte_timeout = 10s;
.between_bytes_timeout = 1s;
.probe = www_probe;
}
# init
sub vcl_init {
new web_cluster = directors.round_robin();
web_cluster.add_backend(web1);
web_cluster.add_backend(web2);
new img_cluster = directors.random();
img_cluster.add_backend(img1, 10); # 2/3 to backend one
img_cluster.add_backend(img2, 5); # 1/3 to backend two
}
# acl purgers
acl purgers {
"127.0.0.1";
"192.168.0.0"/24;
}
# recv
sub vcl_recv {
if (req.url ~ "\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)") {
set req.backend_hint = web2;
}
if (req.url ~ "\.(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$") {
set req.backend_hint = img_cluster.backend();
}
if (req.url ~ "\.(html|htm|css|js)") {
set req.backend_hint = web1;
}
# allow PURGE from localhost and 192.168.0...
if (req.method == "PURGE") {
if (!client.ip ~ purgers) {
return (synth(405, "Purging not allowed for " + client.ip));
}
return (purge);
}
if (req.method != "GET" &&
req.method != "HEAD" &&
req.method != "PUT" &&
req.method != "POST" &&
req.method != "TRACE" &&
req.method != "OPTIONS" &&
req.method != "PATCH" &&
req.method != "DELETE") {
return (pipe);
}
if (req.method != "GET" && req.method != "HEAD") {
return (pass);
}
if (req.url ~ "\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)") {
return (pass);
}
if (req.http.Authorization) {
return (pass);
}
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$") {
unset req.http.Accept-Encoding;
} elseif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elseif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
unset req.http.Accept-Encoding;
}
}
if (req.url ~ "\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)") {
unset req.http.cookie;
return (hash);
if (req.restarts == 0) {
if (req.http.X-Fowarded-For) {
set req.http.X-Forwarded-For = req.http.X-Forwarded-For + "," + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
}
}
}
# pass
sub vcl_pass {
return (fetch);
}
# backen fetch
sub vcl_backend_fetch {
return (fetch);
}
# hash
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (lookup);
}
# hit
sub vcl_hit {
if (req.method == "PURGE") {
return (synth(200, "Purged."));
}
return (deliver);
}
# miss
sub vcl_miss {
if (req.method == "PURGE") {
return (synth(404, "Purged."));
}
return (fetch);
}
# backend response
sub vcl_backend_response {
set beresp.grace = 5m;
if (beresp.status == 499 || beresp.status == 404 || beresp.status == 502) {
set beresp.uncacheable = true;
}
if (beresp.http.content-type ~ "text") {
set beresp.do_gzip = true;
}
if (bereq.url ~ "\.(php|jsp)(\?|$)") {
set beresp.uncacheable = true;
} else { //自定义缓存文件的缓存时长,即TTL值
if (bereq.url ~ "\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico)($|\?)") {
set beresp.ttl = 15m;
unset beresp.http.Set-Cookie;
} elseif (bereq.url ~ "\.(gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)") {
set beresp.ttl = 30m;
unset beresp.http.Set-Cookie;
} else {
set beresp.ttl = 10m;
unset beresp.http.Set-Cookie;
}
return (deliver);
}
}
# deliver
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
set resp.http.X-Cache-Hits = obj.hits;
} else {
set resp.http.X-Cache = "MISS";
}
unset resp.http.X-Powered-By;
unset resp.http.Server;
unset resp.http.X-Drupal-Cache;
unset resp.http.Via;
unset resp.http.Link;
unset resp.http.X-Varnish;
set resp.http.xx_restarts_count = req.restarts;
set resp.http.xx_Age = resp.http.Age;
set resp.http.hit_count = obj.hits;
unset resp.http.Age;
return (deliver);
}
sub vcl_purge {
return (synth(200,"success"));
}
/* sub vcl_backend_error {
if (beresp.status == 500 ||
beresp.status == 501 ||
beresp.status == 502 ||
beresp.status == 503 ||
beresp.status == 504) {
return (retry);
}
}
*/
sub vcl_fini {
return (ok);
}
|
|
|