Difference between revisions of "Setup Varnish for Wordpress"

From Brian Nelson Ramblings
Jump to: navigation, search
(Setup Varnish with Wordpress)
(Setup Varnish with Wordpress)
Line 4: Line 4:
  
 
The good news is WordPress doesn’t have to be a speed demon. In most cases it’s just a CMS to produce static pages. If the content is static it doesn’t make any sense to waste CPU cycles on re-rendering the same HTML over and over again.
 
The good news is WordPress doesn’t have to be a speed demon. In most cases it’s just a CMS to produce static pages. If the content is static it doesn’t make any sense to waste CPU cycles on re-rendering the same HTML over and over again.
 +
 +
===Start off by install varnish on your server===
  
 
You can follow the guide to install varnish [[Setup_Varnish_Cache|Click Here]]
 
You can follow the guide to install varnish [[Setup_Varnish_Cache|Click Here]]
 +
 +
===Adjust the Default.vcl===
  
 
Below is a basic default.vcl for use with wordpress.  
 
Below is a basic default.vcl for use with wordpress.  

Revision as of 04:02, 19 January 2014

Setup Varnish with Wordpress

Lets face it, WordPress is slow. With every request it has to go though thousands lines of code and multiple SQL queries to render a page. Very popular configuration for a WordPress site is Apache, PHP and MySQL. It’s very good setup but can’t be consider the fastest (at least without any additional tweaking).

The good news is WordPress doesn’t have to be a speed demon. In most cases it’s just a CMS to produce static pages. If the content is static it doesn’t make any sense to waste CPU cycles on re-rendering the same HTML over and over again.

Start off by install varnish on your server

You can follow the guide to install varnish Click Here

Adjust the Default.vcl

Below is a basic default.vcl for use with wordpress.

backend default {
    .host = "127.0.0.1";
    .port = "81";
}

sub vcl_recv {
  if (req.http.Accept-Encoding) {
#revisit this list
   if (req.url ~ "\.(gif|jpg|jpeg|swf|flv|mp3|mp4|pdf|ico|png|gz|tgz|bz2)(\?.*|)$") {
     remove req.http.Accept-Encoding;
   } elsif (req.http.Accept-Encoding ~ "gzip") {
     set req.http.Accept-Encoding = "gzip";
   } elsif (req.http.Accept-Encoding ~ "deflate") {
     set req.http.Accept-Encoding = "deflate";
   } else {
     remove req.http.Accept-Encoding;
   }
 }
 if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
   unset req.http.cookie;
   set req.url = regsub(req.url, "\?.*$", "");
 }
 if (req.url ~ “\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=”) {
   set req.url = regsub(req.url, “\?.*$”, “”);
 }
 if (req.http.cookie) {
   if (req.http.cookie ~ "(wordpress_|wp-settings-)") {
     return(pass);
   } else {
     unset req.http.cookie;
   }
  }
} 

sub vcl_fetch {
  if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") {
    return (hit_for_pass);
  }
  if ( (!(req.url ~ "(wp-(login|admin)|login)")) || (req.request == "GET") ) {
    unset beresp.http.set-cookie;
  }
  if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    set beresp.ttl = 365d;
  }
} 

sub vcl_deliver {
# multi-server webfarm? set a variable here so you can check
# the headers to see which frontend served the request
#   set resp.http.X-Server = "server-01";
   if (obj.hits > 0) {
     set resp.http.X-Cache = "HIT";
   } else {
     set resp.http.X-Cache = "MISS";
   }
}