Skip to main content
Home
badllama.com
  • Search
  • Log In

Varnish default.vcl

Fri, 04/22/2011 - 15:14 by bchavet

backend default {
        .host = "127.0.0.1";
        .port = "8080";
        .connect_timeout = 600s;
        .first_byte_timeout = 600s;
        .between_bytes_timeout = 600s;
	.probe = {
		.url = "/status.php";
		.interval = 5s;
		.timeout = 1s;
		.window = 5;
		.threshold = 3;
	}
}

// Define the privilged networks
acl privileged {
        "127.0.0.0"/8;
        "10.0.0.0"/8;
}

sub vcl_recv {
	// Allow stale content if backend server is slow or down.
	set req.grace = 6h;

	// Try to use anonymous pages if backend is down.
	if (!req.backend.healthy) {
		unset req.http.Cookie;
	}

	// Always cache the following file types for all users.
	if (req.url ~ "(?i)\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)(\?[a-z0-9]+)?$") {
		unset req.http.Cookie;
	}

        // Remove has_js and Google Analytics __* and Drupal.toolbar.collapsed cookies.
        set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js|Drupal.toolbar.collapsed)=[^;]*", "");

        // 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;
        }

        // Lock down access to cron.php or install.php.
        if (req.url ~ "^/(cron|install)\.php(\?.*)?$" && !client.ip ~ privileged) {
		// Have Varnish throw the error directly
		error 404 "Page not found.";
		// Use a custom error page
                # set req.url = "/404";
        }

	// Handle compression correctly to get more cache hits.
	if (req.http.Accept-Encoding) {
		if (req.http.Accept-Encoding ~ "gzip") {
			// If the browser supports it, we'll use gzip.
			set req.http.Accept-Encoding = "gzip";
		} else if (req.http.Accept-Encoding ~ "deflate") {
			// Next, try deflate if it is supported.
			set req.http.Accept-Encoding = "deflate";
  		} else {
			// Unknown algorithm. Remove it and send unencoded.
			unset req.http.Accept-Encoding;
		}
	}

	// Do not cache these URI's.
	if (req.url ~ "^/update\.php$" ||
	    req.url ~ "^/cron\.php$" ) {
		return(pass);
	}
}

sub vcl_fetch {
	// Allow stale content if backend server is slow or down.
	set beresp.grace = 6h;

	// Don't allow static files to set cookies.
	if (req.url ~ "(?i)\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)(\?[a-z0-9]+)?$") {
		unset beresp.http.set-cookie;
	}
}

sub vcl_error {

	set obj.http.Content-Type = "text/html; charset=utf-8";
	if (req.url !~ "^/?$") {

		// Try to redirect to the homepage, which is more likely to be cached.
		synthetic {"<?xml version="1.0" encoding="utf-8"?>
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			<html>
			<head>
			<title>"} obj.status " " obj.response {"
			</head>
			<body onload="setTimeout(function() { window.location = 'http://"} req.http.host {"/' }, 5000)">
			<h1>Error "} obj.status " " obj.response {"</h1>
			<p>"} obj.response {"</p>
			<p>XID: "} req.xid {"</p>
			<p>Redirecting to http://"} req.http.host {"/ in 5 seconds</p>
			<hr>
			<p>Varnish cache server</p>
			</body>
			</html>
		"};

	} else {

		synthetic {"<?xml version="1.0" encoding="utf-8"?>
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			<html>
			<head>
			<title>"} obj.status " " obj.response {"</title>
			</head>
			<body>
			<h1>Error "} obj.status " " obj.response {"</h1>
			<p>"} obj.response {"</p>
			<p>XID: "} req.xid {"</p>
			<hr>
			<p>Varnish cache server/p>
			</body>
			</html>
		"};

	}

	return(deliver);

}
Powered by Backdrop CMS