Sample Config for WordPress Running on Varnish 2.1

This is a sample config for running varnish on a wordpress blog, this will stop dynamic content such as the logins from being cached.

WordPress Varnish Sample Config

  • This is a sample config for running varnish on a wordpress blog, this will stop dynamic content such as the logins from being cached.
# Send all requests to your webserver
backend default {
 .host = "127.0.0.1";
 .port = "8080";
}

sub vcl_recv {
 # If we don't set this, in our webserver's logs, we'll get varnish's IP instead
 # of the real client's IP
 if (req.http.x-forwarded-for) {
 set req.http.X-Forwarded-For =
 req.http.X-Forwarded-For ", " client.ip;
 } else {
 set req.http.X-Forwarded-For = client.ip;
 }
 if (req.request != "GET" && req.request != "HEAD") {
 return (pass);
 }
 # Unless we're in the admin/login section, remove all cookies
 if (!(req.url ~ "wp-(login|admin)")) {
 unset req.http.cookie;
 }
 return (lookup);
}

sub vcl_pipe {
 return (pipe);
}

sub vcl_pass {
 return (pass);
}

sub vcl_hash {
 set req.hash += req.url;
 if (req.http.host) {
 set req.hash += req.http.host;
 } else {
 set req.hash += server.ip;
 }
 return (hash);
}

sub vcl_hit {
 if (!obj.cacheable) {
 return (pass);
 }
 return (deliver);
}

sub vcl_miss {
 return (fetch);
}

sub vcl_fetch {
 if (!beresp.cacheable) {
 return (pass);
 }
 if (beresp.http.Set-Cookie) {
 return (pass);
 }
 if (!(req.url ~ "wp-(login|admin)")) {
 unset req.http.cookie;
 }
 return (deliver);
}

sub vcl_deliver {
 # Remove bad headers
 remove resp.http.X-Varnish;
 remove resp.http.Via;
 remove resp.http.Age;
 remove resp.http.X-Powered-By;
 remove resp.http.Cache-Control;
 remove resp.http.Pragma;
 return (deliver);
}
						

Leave a Reply