docker: varnish cache purge, and cache indicator
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nugraha 2022-12-05 19:46:59 +07:00
parent 44173e65f5
commit 1f9da8db15
Signed by: ii64
GPG key ID: E41C08AD390E7C49

View file

@ -12,8 +12,11 @@
# new 4.0 format. # new 4.0 format.
vcl 4.0; vcl 4.0;
import std;
import directors;
# Default backend definition. Set this to point to your content server. # Default backend definition. Set this to point to your content server.
backend default { backend obsb1 {
.host = "obs-access-signer"; .host = "obs-access-signer";
.port = "9002"; .port = "9002";
.connect_timeout = 300s; .connect_timeout = 300s;
@ -22,6 +25,16 @@ backend default {
.max_connections = 800; .max_connections = 800;
} }
acl purge {
"localhost";
"127.0.0.0"/8;
}
sub vcl_init {
new vdir = directors.round_robin();
vdir.add_backend(obsb1);
}
sub vcl_hash { sub vcl_hash {
hash_data(req.url); hash_data(req.url);
if (req.http.Host) { if (req.http.Host) {
@ -37,6 +50,16 @@ sub vcl_recv {
# Typically you clean up the request here, removing cookies you don't need, # Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc. # rewriting the request, etc.
// cache purge
if (req.method == "PURGE") {
if (!client.ip ~ purge) {
return (synth(405, "Not Allowed"));
}
return (purge);
}
set req.backend_hint = vdir.backend();
if (req.method == "PRI") { if (req.method == "PRI") {
/* We do not support SPDY or HTTP/2.0 */ /* We do not support SPDY or HTTP/2.0 */
return (synth(405)); return (synth(405));
@ -70,7 +93,7 @@ sub vcl_backend_response {
return (deliver); return (deliver);
} }
# keep last content in case backend goes down. # keep last stale content in case backend goes down.
set beresp.grace = 6h; set beresp.grace = 6h;
# cache timeout # cache timeout
@ -86,7 +109,7 @@ sub vcl_deliver {
# You can do accounting or modifying the final object here. # You can do accounting or modifying the final object here.
set resp.http.Via = regsuball(resp.http.Via, "\s\([a-zA-Z0-9\/.]+\)", ""); set resp.http.Via = regsuball(resp.http.Via, "\s\([a-zA-Z0-9\/.]+\)", "");
set resp.http.Server = "VOAS"; set resp.http.Server = "voasis";
# Debug header to see if it's a HIT/MISS and the number of hits # Debug header to see if it's a HIT/MISS and the number of hits
if (obj.hits > 0) { if (obj.hits > 0) {
@ -108,6 +131,22 @@ sub vcl_deliver {
return (deliver); return (deliver);
} }
sub vcl_hit {
// TTL still valid
if (obj.ttl >= 0s) {
return (deliver);
}
// Misbehaving backend:
// https://varnish-cache.org/docs/trunk/users-guide/vcl-grace.html
return (deliver);
}
sub vcl_miss {
}
sub vcl_backend_error { sub vcl_backend_error {
return (retry); return (retry);
} }