Skip to content

Instantly share code, notes, and snippets.

@emiliorizzo
Last active August 15, 2022 12:02
Show Gist options
  • Save emiliorizzo/4951ee87442e8d424680c77de40a8a67 to your computer and use it in GitHub Desktop.
Save emiliorizzo/4951ee87442e8d424680c77de40a8a67 to your computer and use it in GitHub Desktop.
Varnish configuration, purge only for basic auth users
# Varnish configuration
# Purge only for basic auth users
## HOW TO PURGE
### with browser
# 1 - Set basic auth on /auth (or change the rule)
# 2 - Authenticate on /auth, and load url to refresh (purge) cache.
### with wget
# wget -r -l1 --http-user=foo --http-password=bar --auth-no-challenge http://url-to-purge
vcl 4.0;
# Default backend definition.
backend default {
.host = "127.0.0.1";
.port = "3005";
}
sub vcl_recv {
# Basic auth end point
if( req.url == "/auth" ){
return(pass);
}
if (req.http.Authorization){
# Basic auth user:pass in base64
# echo -n user:pass | base64
if (req.http.Authorization ~ "Basic Zm9vOmJhcg=="){ // foo:bar
unset req.http.Authorization;
return (purge);
}
unset req.http.Authorization;
return (hash);
}
}
sub vcl_backend_response {
set beresp.ttl = 1y;
}
sub vcl_purge {
set req.method = "GET";
return (restart);
}
sub vcl_hit {
if (obj.ttl >= 0s) {
// A pure unadultered hit, deliver it
return (deliver);
}
if (obj.ttl + obj.grace > 0s) {
// Object is in grace, deliver it
// Automatically triggers a background fetch
return (deliver);
}
// fetch & deliver once we get the result
return (fetch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment