Decided to try varnish, the relatively new squid alternative out. Here’s what I have so far. I’m getting about 66% (very limited testing, YMMV) cache hits with this setup but am missing some things for caching Rails sessions and content. With appropriate tuning/configuration, I am sure the hit rate could be much higher. If you have any suggestions, let me know and I will update this.
# Varnish Config
# default backend config
backend default {
set backend.host = "127.0.0.1";
set backend.port = "80";
}
# only allow purging of cache from localhost
acl purge {
"localhost";
}
# this is the main config
sub vcl_recv {
# Host header check.
# * Check if we recognize the domain used
# * Normalize host headers to limit cache usage
# * Rewrite URL for the virtual host monster
# Return an error for other requests.
# cache multimedia
if (req.request == "GET" && req.url ~ "\.(jpg|jpeg|gif|ico)$") {
lookup;
}
# cache CSS and JS files
if (req.request == "GET" && req.url ~ "\.(css|js)$") {
lookup;
}
# cache static files
if (req.request == "GET" && req.url ~ "\.(pdf|xls|vsd|doc|ppt|iso)$") {
lookup;
}
# do not cache POST requests
if (req.request == "POST") {
pipe;
}
if (req.request != "GET" && req.request != "HEAD") {
# figure out if you are allowed to purge stuff from cache
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
lookup;
}
# else just pass it along
pipe;
}
if (req.http.Expect) {
pipe;
}
if (req.http.If-None-Match) {
pass;
}
# pass to backend if we have HTTP authentication
if (req.http.Authenticate || req.http.Authorization) {
pass;
}
# We only care about the "__vu_*" cookies, used for authentication
# if (req.http.Cookie && req.http.Cookie ~ "__vu_session=") {
# pass;
# }
# Remove the "Cookie:" header from the request.
# remove req.http.cookie;
# Do a "lookup" in the cache. This goes to "vcl_hit", or to
# "vcl_miss" and then "vcl_fetch"
lookup;
}
sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
}
sub vcl_miss {
if (req.http.If-Modified-Since) {
pass;
}
if (req.request == "PURGE") {
error 404 "Not in cache.";
}
}
sub vcl_fetch {
# Handle X-Accel-Redirect
if ( obj.http.x-accel-redirect ~ ".*" ) {
set req.url = obj.http.x-accel-redirect;
restart;
}
}