Varnish for Wordpress, drupal etc

Author - Navneet Kashyap
10.07.2018
|
0 Comments
|

Purpose of Varnish for WordPress, Drupal etc

Varnish is a well known open source web cache application for reverse http which helps to accelerate the delivery time for clients. It itself works at 80 so that apache works behind it on a different port.

Below are the steps involved in proper Installation of Varnish:

Step:1 Install pygpgme utility (Python module that lets you sign, verify, encrypt and decrypt) by using command –

sudo yum install pygpgme yum-utils

Step:2 Create a repo file in default location

# vi /etc/yum.repos.d/varnishcache_varnish41.repo

And then, enter the following directives configuration in this file

[varnishcache_varnish41]
name=varnishcache_varnish41
baseurl=https://packagecloud.io/varnishcache/varnish41/el/6/$basearch
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/varnishcache/varnish41/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300

[varnishcache_varnish41-source]
name=varnishcache_varnish41-source
baseurl=https://packagecloud.io/varnishcache/varnish41/el/6/SRPMS
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/varnishcache/varnish41/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300

Step:3 Update the local yum cache using below command

sudo yum update
sudo yum -q makecache -y --disablerepo='*' --enablerepo='varnishcache_varnish41'

Use the below configuration to begin:

#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and https://www.varnish-cache.org/trac/wiki/VCLExamples for more examples.

# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
	.host = "127.0.0.1";
	.port = "8080";
}
sub vcl_recv {
	# Some wordpress URL manipulation
    	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, "\?.*$", "");
    	}

    	# Pass if the page is login, admin, preview, search or xmlrpc
    	if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true" || req.url ~ "\?s=" || req.url ~ "xmlrpc.php") {
        	return (pass);
    	}

    	# Some generic URL manipulation, useful for all templates that follow
    	# First remove the Google Analytics added parameters, useless for our backend
    	if (req.url ~ "(\?|&)(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=") {
        	set req.url = regsuball(req.url, "&(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "");
        	set req.url = regsuball(req.url, "\?(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "?");
        	set req.url = regsub(req.url, "\?&", "?");
        	set req.url = regsub(req.url, "\?$", "");
    	}

    	# Strip hash, server doesn't need it.
    	if (req.url ~ "\#") {
        	set req.url = regsub(req.url, "\#.*$", "");
    	}

    	# Strip a trailing ? if it exists
    	if (req.url ~ "\?$") {
        	set req.url = regsub(req.url, "\?$", "");
    	}

    	# Remove the wp-settings-1 cookie
    	set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "");

    	# Remove the wp-settings-time-1 cookie
    	set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?", "");

    	# Remove the wp test cookie
    	set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(; )?", "");

    	# Remove the cloudflare cookie
    	set req.http.Cookie = regsuball(req.http.Cookie, "__cfduid=[^;]+(; )?", "");

    	# Remove the PHPSESSID in members area cookie
    	set req.http.Cookie = regsuball(req.http.Cookie, "PHPSESSID=[^;]+(; )?", "");

    	# Remove the Quant Capital cookies (added by some plugin, all __qca)
    	set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");

    	# Are there cookies left with only spaces or that are empty?
    	if (req.http.cookie ~ "^\s*$") {
        	unset req.http.cookie;
    	}

    	#Drop ALL cookies sent to WordPress, except those originating from the URLs defined.
    	if (!(req.url ~ "(wp-login|wp-admin|cart|my-account|checkout|addons|wordpress-social-login|wp-login\.php|forumPM|members)")) {
        	unset req.http.cookie;


    	}
}
sub vcl_backend_response {

  if (!(bereq.url ~ "(wp-(login|admin)|login)")) {
	unset beresp.http.set-cookie;
  }

  set beresp.ttl = 10m;
#set beresp.http.Cache-Control = "public,max-age=602"
  return (deliver);


  if(beresp.status == 200) {
	# Cache 404 responses for 15 seconds
	set beresp.http.Cache-Control = "max-age=15";
	set beresp.ttl = 15s;
	set beresp.grace = 15s;
  }

}
sub vcl_deliver {
	# Happens when we have all the pieces we need, and are about to send the
	# response to the client.
	#
	# You can do accounting or modifying the final object here.
}

Step:4 Add the plugin
After installing Varnish we have to instruct WordPress to purge the cached content whenever it is modified. There are several plugins to achieve this. Here we will use Varnish HTTP Purge. Go to the WordPress dashboard, click on Plugins and click Add New and search for ‘Varnish HTTP Purge’. Click on ‘Install Now’ and confirm. Finally, activate it.

Put the below configuration in .htaccess file

# One year for image files (time is in seconds)
<filesMatch ".(jpg|jpeg|png|gif|ico)$">
Header set Cache-Control "max-age=31536000, public"
</filesMatch>
# One month for css and js (time is in seconds)
<filesMatch ".(css|js)$">
Header set Cache-Control "max-age=2628000, public"
</filesMatch>

Step:5 Now test URL whether caching is enabled or not.
http://www.isvarnishworking.com/

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

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