Skip to content

Instantly share code, notes, and snippets.

@caquino
Last active December 16, 2015 20:29
Show Gist options
  • Save caquino/5493273 to your computer and use it in GitHub Desktop.
Save caquino/5493273 to your computer and use it in GitHub Desktop.
NGINX cache purge using cache_proxy_bypass
location / {
add_header X-Cached $upstream_cache_status;
....
proxy_cache_bypass $http_cache_purge;
....
proxy_pass ... ;
}
#!/usr/bin/env bash
echo "Before purging:"
exec 6<>/dev/tcp/127.0.0.1/80
echo -ne "GET $1 HTTP/1.0\r\n\r\n" >&6
while read -r -u 6
do
if [[ ${REPLY} =~ ^$ ]]; then
break
elif [[ ${REPLY} =~ ^(Last-Modified|X-Cached):(.*)$ ]]; then
case ${BASH_REMATCH[1]} in
Last-Modified)
echo -ne "\tFile date:\t\t${BASH_REMATCH[2]}\n"
;;
X-Cached)
echo -ne "\tStatus:\t\t${BASH_REMATCH[2]}\n"
;;
esac
fi
done
exec 6>&-
echo "Purging file."
exec 6<>/dev/tcp/127.0.0.1/80
echo -ne "GET $1 HTTP/1.0\r\nCache-Purge: 1\r\n\r\n" >&6
while read -r -u 6
do
if [[ ${REPLY} =~ ^$ ]]; then
break
elif [[ ${REPLY} =~ ^(Last-Modified|X-Cached):(.*)$ ]]; then
case ${BASH_REMATCH[1]} in
Last-Modified)
echo -ne "\tFile date:\t\t${BASH_REMATCH[2]}\n"
;;
X-Cached)
echo -ne "\tStatus:\t\t${BASH_REMATCH[2]}\n"
;;
esac
fi
done
exec 6>&-
echo "After purging:"
exec 6<>/dev/tcp/127.0.0.1/80
echo -ne "GET $1 HTTP/1.0\r\n\r\n" >&6
while read -r -u 6
do
if [[ ${REPLY} =~ ^$ ]]; then
break
elif [[ ${REPLY} =~ ^(Last-Modified|X-Cached):(.*)$ ]]; then
case ${BASH_REMATCH[1]} in
Last-Modified)
echo -ne "\tFile date:\t\t${BASH_REMATCH[2]}\n"
;;
X-Cached)
echo -ne "\tStatus:\t\t${BASH_REMATCH[2]}\n"
;;
esac
fi
done
exec 6>&-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment