Setup Varnish for MediaWiki

From Brian Nelson Ramblings
Revision as of 06:32, 19 January 2014 by Brian (Talk | contribs) (Adjust the Default.vcl)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Setup Varnish with MediaWiki

Lets face it, MediaWiki 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 MediaWiki 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 MediaWiki 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

Yes, this is the default.vcl for this site

Adjust the Default.vcl

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

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

acl purge {
    "127.0.0.1";
}
 
sub vcl_recv {

set req.http.host = regsub(req.http.host, "^www\.briansnelson\.com$","briansnelson.com");

       set req.grace = 120s;
       set req.http.X-Forwarded-For = client.ip;
       set req.backend = default;
  
        if (req.request == "PURGE") 
            {if (!client.ip ~ purge)
               {error 405 "Not allowed.";}
           return(lookup);}
  
        if (req.request != "GET" && req.request != "HEAD" &&
            req.request != "PUT" && req.request != "POST" &&
            req.request != "TRACE" && req.request != "OPTIONS" &&
            req.request != "DELETE") 
             {return(pipe);}     /* Non-RFC2616 or CONNECT which is weird. */
 
        if (req.request != "GET" && req.request != "HEAD")
           {return(pass);}      /* We only deal with GET and HEAD by default */
 
        if (req.http.Authorization || req.http.Cookie)
           {return(pass);}      /* Not cacheable by default */
 
        if (req.http.If-None-Match)
           {return(pass);}
 
        if (req.http.Cache-Control ~ "no-cache")
           {ban_url(req.url);}
 
        if (req.http.Accept-Encoding) {
          if (req.http.User-Agent ~ "MSIE 6") {
            unset 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 {
            unset req.http.Accept-Encoding;
          }
        }
 
        return(lookup);
}
 
sub vcl_pipe {
 
 
        set req.http.connection = "close";
}
 
sub vcl_hit {
        if (req.request == "PURGE") 
            {ban_url(req.url);
            error 200 "Purged";}
 
        if (!obj.ttl > 0s)
           {return(pass);}
}
 
sub vcl_miss {
        if (req.request == "PURGE") 
           {error 200 "Not in cache";}
}
  
sub vcl_fetch {
 
        set beresp.grace = 120s;
 
        if (beresp.ttl < 48h) {
          set beresp.ttl = 48h;}
 
        if (!beresp.ttl > 0s) 
            {return(hit_for_pass);}
 
        if (beresp.http.Set-Cookie) 
            {return(hit_for_pass);}
  
        if (req.http.Authorization && !beresp.http.Cache-Control ~ "public") 
            {return(hit_for_pass);}
 
}
sub vcl_error {
 if (obj.status == 750) {
  set obj.http.location = req.http.Location;
set obj.status = 301;
return (deliver);
  }
 }

Additional Articles