Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Varnish

shish edited this page Oct 1, 2012 · 10 revisions

This is the VCL that rule34.paheal.net is using, which copes with 2500 hits/second pretty easily

# This is a basic VCL configuration file for varnish.  See the vcl(7)
# man page for details on VCL syntax and semantics.
# 
# Default backend definition.  Set this to point to your content
# server.
# 
backend default {
    .host = "127.0.0.1";
    .port = "81";
}

sub vcl_recv {
	# normalise inputs
	if (req.http.host == "www.rule34.paheal.net") {
		set req.http.host = "rule34.paheal.net";
	}
	if (req.http.host ~ "rule34-data-\d+\.paheal\.net") {
		set req.http.host = "rule34-data-000.paheal.net";
	}
	if (req.url ~ "^/(_images|_thumbs)/") {
		# normalise filenames - "/_images/32452/old tags.jpg" and /_images/32452/new tags.jpg"
		# are really the same image
		set req.url = regsub(req.url, "^/(_images|_thumbs)/([0-9a-f]{32})/.*", "/\\1/\\2/thumb.jpg");
	}

	if(req.http.Cookie) {
		# ignore UI cookies
		set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(ui-[a-z-]+)=[^;]*", "");
		# ignore ad cookies
		set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(juicy-[a-z-]+)=[^;]*", "");
		# ignore google cookies
		set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", "");
		# Remove a ";" prefix, if present.
		set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
		# Remove empty cookies.
		if (req.http.Cookie ~ "^\s*$") {
			unset req.http.Cookie;
		}
	}

	if (req.http.Accept-Encoding) {
		if (req.url ~ "\.(jpg|jpeg|png|gif)$") {
			# 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" && req.http.user-agent !~ "Internet Explorer") {
			set req.http.Accept-Encoding = "deflate";
		} else {
			# unkown algorithm
			remove req.http.Accept-Encoding;
		}
	}

	# images and thumbs never change
	if (req.url ~ "/_images/.*") {
		return (lookup);
	}
	if (req.url ~ "/_thumbs/.*") {
		return (lookup);
	}
}

sub vcl_fetch {
	if (beresp.status >= 400) {
		set beresp.http.X-Cacheable = "NO: beresp.status";
		set beresp.http.X-Cacheable-status = beresp.status;
		set beresp.ttl = 0s;
		return (hit_for_pass);
	}

	if (req.http.Cookie ~ "shm_") {
        	set beresp.http.X-Cacheable = "NO: Got Shimmie Cookie";
		set beresp.ttl = 0s;
	        return (hit_for_pass);
	}

	if (beresp.http.Set-Cookie) {
		set beresp.http.X-Cacheable = "NO: Sets cookie";
		set beresp.ttl = 0s;
		return (hit_for_pass);
	}

	if (beresp.http.Vary == "*") {
		set beresp.http.X-Cacheable = "NO: Variable content";
		set beresp.ttl = 0s;
		return (hit_for_pass);
	}

	# override the default so cache isn't 100% full of thumbs (hopefully)
	if (req.url ~ "/_thumbs/.*") {
		set beresp.ttl = 1h;
	}

	# if the above rules deem something cacheable, and it doesn't have a TTL
        # set in the HTTP headers, it will have a default ttl of 120s; here we
	# override that
	if (beresp.ttl == 120s && req.http.host == "rule34.paheal.net") {
		set beresp.grace = 1h;

		# for users who don't take part in tagging or discussion,
		# view pages are essentially static
		if(req.url ~ "^/post/view/\d+") {
			set beresp.ttl = 3h;
		}

		# front page is most popular for anons, update it often
		elseif(req.url ~ "^/post/list$") {
			set beresp.ttl = 3m;
		}
		elseif(req.url ~ "^/comment/list$") {
			set beresp.ttl = 3m;
		}
		# first few pages are slightly less popular
		elseif(req.url ~ "^/post/list/[1-5]$") {
			set beresp.ttl = 5m;
		}
		# historical lists change rarely
		elseif(req.url ~ "^/post/list/\d+") {
			set beresp.ttl = 30m;
		}
		# search results change very rarely
		elseif(req.url ~ "^/post/list/.*/\d+") {
			set beresp.ttl = 3h;
		}

		# everything else, default to 1h
		else {
			set beresp.ttl = 1h;
		}
	}

	set beresp.http.X-Cache-TTL = beresp.ttl;

	if (beresp.ttl == 0s) {
		set beresp.http.X-Cacheable = "NO: yes, but TTL = 0s";
	}
	else {
		set beresp.http.X-Cacheable = "YES: all good";
	}

	return (deliver);
}

sub vcl_deliver {
	if (obj.hits > 0) {
		set resp.http.X-Cache = "HIT";
		set resp.http.X-Cache-Hits = obj.hits;
	}
	else {
		set resp.http.X-Cache = "MISS";
	}
}

sub vcl_error {
	if (obj.status == 502 && req.restarts < 5) {
		set obj.http.X-Restarts = req.restarts;
		return (restart);
	}
}
Clone this wiki locally