• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

Setup varnish cache server for wordpress site

September 30, 2013 by Sharad Chhetri Leave a Comment

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.

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. Install and configure Varnish Cache server on CentOS/RHEL 6.x
  2. How to setup varnish to do not cache particular url
  3. How to setup Varnish to do not cache particular url specific domain
  4. WordPress host IP changed not able to open wp-admin and site page looks scattered
  5. How to install Varnish 4 version on CentOS 7 / RHEL 7
  6. How to clear apc cache by using php script
  7. How to setup LAMP stack on Ubuntu 14.04 LTS Server
  8. Setup self signed ssl certificate on Owncloud 6 in Ubuntu 14.04 LTS Server
  9. How to configure FTP server in CentOS 6.3 – vsftpd server
  10. Install Openssh server on Ubuntu Desktop / Server

Filed Under: Linux, web accelerator Tagged With: varnish

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Linux Commands

grep command to remove commented lines

6 important examples of cd command on Linux and Unix Systems

Head command in Linux / Unix

Yum command to download rpm file without installing in linux system

sed command to display text between two strings or keywords

learn very useful alias command with examples

How to set root password on Ubuntu / Debian / Linux Mint

Explore 70+ Articles On Linux Commands

Always Useful Tips And Tricks

Change the default port number of ssh server

How to encode and decode the strings with base64

How to find swap partition or file in linux

fatal error: error writing to /tmp/ccwAjc9Z.s: No space left on device

How to configure ethernet in CentOS 6 after installing in Virtual Box

30 useful Linux terminal keyboard shortcuts

AttributeError: ‘WSGIRequest’ object has no attribute ‘session’ Django python web framework

Explore 90+ Article On "Linux Tips And Tricks"

You Might Like These Articles!

Internal External Command

What is Linux/Unix Internal And External Command

Linux basic command

Linux Basic Commands For Every Beginner

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission but Schools and Colleges can do in their Private Network
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy