Setup varnish cache server for wordpress site

In this turorial we will learn,how to configure Varnish 3.x vcl file for WordPress.We will setup varnish cache server for wordpress site.This will increase the page load speed of the site/blog.
Recently I migrated my server to DigitalOcean.To migrate the server from web hosting to cloud server was not done first time,it was also done in past. Reason to migrate to DigitalOcean was its cheap price and good service.
The biggest reason is now I am going to write many things which actually comes under sharadchhetri.com network.
I configured the varnish in sharadchhetri.com and my blog is wordpress based. Now here I got a challenge after reading many manuals and I finally able to get the Varnish vcl file.

If you are configuring first time Varnish,read my this earlier post

https://sharadchhetri.com/2013/09/25/how-to-setup-varnish-web-accelerator-in-centos-and-rhel/

After finish the above given setup,edit the /etc/varnish/default.vcl file and paste the below given contents

Note: Change the port no. 8880 as you want to apache run on. If you select port no. 8880 then you have run apache in port no. 8880

Below is my default.vcl file for wordpress.

backend default {
  .host = "127.0.0.1";
  .port = "8880";
}
##################################################
sub vcl_recv {
    if (req.restarts == 0) {
        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 == "PURGE") {
        if ( client.ip != "192.241.190.251") {
            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);
    }
    if (req.request != "GET" && req.request != "HEAD") {
        return (pass);
    }
    if (!(req.url ~ "wp-(login|admin)") &&
        !(req.url ~ "&preview=true" ) ) {
        unset req.http.cookie;
    }
 
    if (req.http.Authorization || req.http.Cookie) {
        return (pass);
    }
    return (lookup);
}

sub vcl_fetch {
    if (!(req.url ~ "wp-(login|admin)")) {
        unset beresp.http.set-cookie;
        set beresp.ttl = 96h;
    }
 
    if (beresp.ttl <= 0s ||
        beresp.http.Set-Cookie ||
        beresp.http.Vary == "*") {
            set beresp.ttl = 120 s;
            return (hit_for_pass);
    }
    return (deliver);
}
 
sub vcl_hit {
    if (req.request == "PURGE") {
        purge;
        error 200 "Purged.";
    }
    return (deliver);
}
 
sub vcl_miss {
    if (req.request == "PURGE") {
        purge;
        error 200 "Purged.";
    }
    return (fetch);
}
 

After editing the default.vcl file,restart the varnish

/etc/init.d/varnish restart

Checking is Varnish cacheing or not

Yes,it was working

See the below output

linux@tuxworld:~$  curl -I https://sharadchhetri.com
HTTP/1.1 200 OK
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-Mobilized-By: WordPress Mobile Pack 1.2.5
Content-Type: text/html; charset=UTF-8
Content-Length: 50037
Accept-Ranges: bytes
Date: Mon, 30 Sep 2013 15:37:36 GMT
X-Varnish: 1302477386 1302477385
Age: 3
Via: 1.1 varnish
Connection: keep-alive

linux@tuxworld:~$ 

After using Varnish,my blog speed is increased.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.