Varnish VCL Configuration on NetBSD 10
Full example with load balancing and grace
vcl 4.1;
import directors;
backend web1 {
.host = "192.168.1.10"; .port = "8080";
.probe = { .url = "/health"; .timeout = 5s; .interval = 10s; .window = 5; .threshold = 3; }
}
backend web2 {
.host = "192.168.1.11"; .port = "8080";
.probe = { .url = "/health"; .timeout = 5s; .interval = 10s; }
}
sub vcl_init {
new lb = directors.round_robin();
lb.add_backend(web1);
lb.add_backend(web2);
}
sub vcl_recv {
set req.backend_hint = lb.backend();
if (req.method != "GET" && req.method != "HEAD") { return (pass); }
if (req.http.Authorization) { return (pass); }
if (req.url ~ "\.(css|js|png|jpg|gif|ico|woff2?)$") {
unset req.http.Cookie;
return (hash);
}
}
sub vcl_backend_response {
set beresp.grace = 30s;
if (bereq.url ~ "\.(css|js|png|jpg|gif|ico|woff2?)$") {
unset beresp.http.Set-Cookie;
set beresp.ttl = 7d;
}
if (beresp.status >= 500) {
set beresp.ttl = 0s; set beresp.uncacheable = true; return (deliver);
}
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
unset resp.http.X-Varnish;
unset resp.http.Via;
}
Purge by URL pattern
varnishadm "ban req.http.host == example.com && req.url ~ /blog"