Varnish

Dieser Artikel dokumentiert die Konfiguration meines Varnish Proxy Servers, den ich zur Performancesteigerung für mein Blog verwende. Siehe auch diesen Artikel im Blog: http://nodomain.cc/2010/02/02/wordpress-varnish-konfiguration.html

# sources:
# http://developer.mindtouch.com/User:PeteE/Varnish_Installation
# http://d9t.de/know-how/varnish-konfiguration-vcl-fuer-plone-3
# http://omninoggin.com/web-development/block-unwanted-spam-bots-using-varnish-vcl/

backend default {
    .host = "127.0.0.1";
    .port = "1234";
# probing deactivated
#    .probe = { 
#	.url = "/servertest.php";
#	.timeout = 34 ms; 
#	.interval = 1s; 
#	.window = 10;
#	.threshold = 8;
#    }
}

acl purge {
    "localhost";
    "12.34.56.78";
}

## Called when a client request is received
#
sub vcl_recv {
    # post requests are not cached
    if (req.request == "POST") {
        pipe;
    }

    # live writer is not cached
    if (req.http.user-agent ~ "Windows Live Writer") {
	pipe;
    }
    
    # no caching for admin backend
    if (req.url ~ "wp-(login|admin)") {
        pipe;
    }

    # feeds are not cached
    if (req.url == "/feed" || req.url == "/comments/feed") {
	pipe;
    }
    
    # some purging stuff
    if (req.request != "GET" && req.request != "HEAD") {
        if (req.request == "PURGE") {
            if(!client.ip ~ purge) {
                error 405 "Not Allowed";
            }
	    purge("req.url ~ " req.url);
	}
        pipe;
    }

    if (req.http.Expect) {
        pipe;
    }
    
    # Add a unique header containing the client address
    remove req.http.X-Forwarded-For;
    set    req.http.X-Forwarded-For = req.http.rlnclientipaddr;

    # normalize the Accept-Encoding header
    if (req.http.Accept-Encoding) {
	if (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;
	}
    }

# only needed if probing is active, if backend dies, set grace period to 5 minutes    
#    if (req.backend.healthy) {
#	set req.grace = 30s;
#    } else {
#	set req.grace = 5m;
#    }

    # no cookies at all for frontend - drawback: ajax edit comments and remembering users does not work
    if (!(req.url ~ "wp-(login|admin)")) {
	unset req.http.cookie;
    }

    # pass through invalid if-none-match requests (see http://developer.mindtouch.com/User:PeteE/Varnish_Installation)
    if (req.http.If-None-Match && !req.http.If-Modified-Since) {
        pass;
    }
    lookup;
}

#
## Called when entering pipe mode
#
sub vcl_pipe {
    pipe;
}

#
## Called when entering pass mode
#
sub vcl_pass {
    pass;
}

#
## Called when entering an object into the cache
#
#sub vcl_hash {
#    set req.hash += req.url;
#    if (req.http.host) {
#	set req.hash += req.http.host;
#    } else {
#	set req.hash += server.ip;
#    }
#    hash;
#}

#
## Called when the requested object was found in the cache
#
sub vcl_hit {
    # pass through if object is not cacheable
    if (!obj.cacheable) {
	pass;
    }
    deliver;
}

#
## Called when the requested object was not found in the cache
#
sub vcl_miss {
    if (req.request == "PURGE") {
        error 404 "Not in cache.";
    }
    fetch;
}

#
## Called when the requested object has been retrieved from the
## backend, or the request to the backend has failed
#
sub vcl_fetch {
    # throw away cookies from backend if we are not in the backend
    if (!(req.url ~ "wp-(login|admin)")) {
	unset obj.http.set-cookie;
    }
    
    # completely ignore backend's caching headers since Wordpress does not help us here (except the W3 Total Cache plugin is installed)
    if (obj.cacheable) {
	/* Remove Expires from backend, it's not long enough */
	unset obj.http.expires;

	/* Set the clients TTL on this object */
	set obj.http.cache-control = "max-age = 900";

	/* Set how long Varnish will keep it */
	set obj.ttl = 5m;

	/* marker for vcl_deliver to reset Age: */
	set obj.http.magicmarker = "1";
    }
    
    # if object is not cacheable, pass it
    if (!obj.cacheable) {
	pass;
    }
}


#
## Called before a cached object is delivered to the client
#
sub vcl_deliver {
    # insert HIT or MISS headers for some client-side debugging
    if (obj.hits > 0) {
	set resp.http.X-Cache = "HIT";
    } else {
	set resp.http.X-Cache = "MISS";
    }

    # see http://varnish-cache.org/wiki/VCLExampleLongerCaching
    if (resp.http.magicmarker) {
	/* Remove the magic marker */
	unset resp.http.magicmarker;

	/* By definition we have a fresh object */
	set resp.http.age = "0";
    }

    deliver;
}

#
## Called when an object nears its expiry time
#
sub vcl_timeout {
    discard;
}

#
## Called when an object is about to be discarded
#
sub vcl_discard {
    discard;
}

Andere Sprachen
Übersetzungen dieser Seite: