Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 73 lines (63 sloc) 2.02 KB
#!/bin/bash -e
. ../../bootstrap
PORT=$(basename $(pwd)|awk -F- '{print $2}')
BACKEND_PORT=$(basename $(pwd)|awk -F- '{print $3}')
TEMP_BASE_DIR=/var/tmp/${USER}-varnish-${PORT}
VARNISH_NAME=$TEMP_BASE_DIR
if [ ! -d $TEMP_BASE_DIR ]; then
mkdir -p $TEMP_BASE_DIR;
chmod og-rwx $TEMP_BASE_DIR;
fi
cat <<EOF > varnish.conf
backend default {
.host = "127.0.0.1";
.port = "$BACKEND_PORT";
}
# only allow purging of cache from localhost
acl purge {
"127.0.0.1";
}
sub vcl_recv {
# Normalize accept-encoding headers: http://www.varnish-cache.org/docs/trunk/tutorial/vary.html#tutorial-vary
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
# No point in compressing these
remove req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
# unkown algorithm
remove req.http.Accept-Encoding;
}
}
# Strip non-application cookies from the requests, e.g. cookies set by Google Analytics
if (req.http.cookie) {
set req.http.cookie = regsuball(req.http.Cookie, "\b__[^=]*?=[^;]+(; )?", "");
if (req.http.cookie ~ "^ *$") {
remove req.http.cookie;
}
}
# The regex used here matches the standard rails cache buster urls
# e.g. /images/an-image.png?1234567
if (req.request == "GET" && req.url ~ "\.(css|js|jpg|jpeg|gif|ico|png)\??\d*$") {
unset req.http.cookie;
}
if (req.request != "GET" && req.request != "HEAD") {
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
}
}
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
EOF
exec varnishd -a 127.0.0.1:$PORT -F -f varnish.conf -s file,$TEMP_BASE_DIR/data,200MB -n $VARNISH_NAME || echo "do you need to create the symlink '$BASE/varnishd?'"
Something went wrong with that request. Please try again.