Skip to content

Instantly share code, notes, and snippets.

@chluehr
Created December 3, 2013 10:04
Show Gist options
  • Save chluehr/7766871 to your computer and use it in GitHub Desktop.
Save chluehr/7766871 to your computer and use it in GitHub Desktop.
Bash script to clear all resources / files / path of a (sub-) domain in a varnish cache.
#/!bin/bash
cmd="sudo varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret"
site=$(echo $1 | sed -r 's|https?://||;s|/(.*)||;')
page=$(echo $1 | awk -F'/' -v OFS='/' '{sub(/https?:\/\//, ""); $1=""; print $0}')
if [ -z "$1" ]; then
echo "ERROR: please provide a URL to purge.."
echo "USAGE: "
echo " - clear all urls of a domain: $0 http://domain.foo "
echo " - clear single url: $0 http://domain.foo/path/file.suffix "
exit 1
fi
if [ -z "$site" ]; then
echo "ERROR: No valid URL"
exit 1
fi
if [ -z "$page" ]; then
echo "Purging SITE [$site] from cache"
$cmd "ban req.http.host == $site"
else
echo "Purging SITE [$site] PATH [$page] from cache"
$cmd "ban req.http.host == $site && req.url ~ ^$page\$"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment