Last active
December 16, 2015 20:29
-
-
Save caquino/5493273 to your computer and use it in GitHub Desktop.
NGINX cache purge using cache_proxy_bypass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
location / { | |
add_header X-Cached $upstream_cache_status; | |
.... | |
proxy_cache_bypass $http_cache_purge; | |
.... | |
proxy_pass ... ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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