Installation of Varnish on Linux

Download the Varnish through YUM,

If you have redhat based distribution follow this link to install.
https://www.varnish-cache.org/installation/redhat

Follow this to configure varnish.
http://ocaoimh.ie/2011/08/09/speed-up-wordpress-with-apache-and-varnish/

Proxy Cache Purge


This plugin can help in purging.

Copy the Text below for /etc/sysconfig/varnish file

#################Start the Text######################

START=yes

# Maximum number of open files (for ulimit -n)
NFILES=131072

# Maximum locked memory size (for ulimit -l)
# Used for locking the shared memory log in memory. If you increase log size,
# you need to increase this number as well
MEMLOCK=82000
RELOAD_VCL=1
VARNISH_VCL_CONF=/etc/varnish/niticentral.vcl
VARNISH_ADMIN_LISTEN_ADDRESS=180.149.244.129
VARNISH_ADMIN_LISTEN_PORT=6082
VARNISH_SECRET_FILE=/etc/varnish/secret
#DAEMON_OPTS=”-a :6081 \
# -T localhost:6082 \
# -f /etc/varnish/default.vcl \
# -S /etc/varnish/secret \
# -s malloc,256m”

#DAEMON_OPTS=”-a 172.31.27.156:80 \
# -T localhost:6082 \
# -f /etc/varnish/default.vcl \
# -S /etc/varnish/secret \
# -s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,1G”

DAEMON_OPTS=”-a 180.149.244.129:80 \
-T 180.149.244.129:6082 \
-f /etc/varnish/niticentral.vcl \
-S /etc/varnish/secret \
-s malloc,5G \
-w 100,3000,120 \
-p thread_pools=10 -p thread_pool_add_delay=2″

##################End the Text##########################

 

Copy the text at /etc/varnish/filename.vcl file

Edit the text as requirement on VCL and exclude the files from varnish,

###################Start the Text############################

backend niticentral {
.host = “127.0.0.1”;
.port = “8080”;
}

#acl purgers {
# “localhost”;
# “172.31.27.156”;
# “india272-623582495.ap-southeast-1.elb.amazonaws.com”;
# “46.51.219.239”;
# “127.0.0.1”;
# “172.31.25.29”;
#
#}

sub vcl_recv {
set req.grace = 300s;
set req.http.host = “www.niticentral.com”;
set req.backend = niticentral;

if (req.request == “POST”) {
return (pass);
}
if (req.http.Accept-Encoding) {
if (req.url ~ “\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$”) {
# No point in compressing these
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 {
# unknown algorithm
remove req.http.Accept-Encoding;
}
}

# remove req.http.X-Forwarded-For;
# set req.http.X-Forwarded-For = client.ip;

## This lines was added by Shri start
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;
}
}
## The Above lines were added by Shri END

 

if (req.http.user-agent ~ “iP(hone|od)” || req.http.User-Agent ~ “Android” || req.http.User-Agent ~ “Symbian” || req.http.User-Agent ~ “^BlackBerry” || req.http.User-Agent ~ “^SonyEricsson” || req.http.User-Agent ~ “^Nokia” || req.http.User-Agent ~ “^SAMSUNG” || req.http.User-Agent ~ “^LG” || req.http.User-Agent ~ “webOS”) {
return (pass);
}
if(req.url == “/” || req.http.Cookie ~ “wordpress_logged_in_” || req.http.X-Requested-With == “XMLHttpRequest” || req.url ~ “getCurrentShow.php” || req.url ~ “ajax_feed_process.php” || req.url ~ “index.php” || req.url ~ “saveList.php” || req.url ~ “action_first_vote.php” || req.url ~ “action.php” || req.url ~ “admin-ajax.php” || req.url ~ “wp-comments-post.php” || req.url ~ “update-youtube-count.php” || req.url ~ “page-ajax.php” || req.url ~ “ajax-debate.php” || req.url ~ “media-upload.php” || req.url ~ “manage-images.php” || req.url ~ “edit-thumbnail.php” || req.url ~ “rotate.php” || req.url ~ “cron/*” || req.url ~ “icecast_source_cron.php” || req.http.Cookie ~ “wordpress_logged_in_” || req.url ~ “cache=true” || req.url ~ “cache=clean” || req.url ~ “preview=true” || req.url ~ “wp-login” || req.url ~ “wp-admin” ||req.url ~ “admin” || req.url ~ “FBAPP” || req.url ~ “/modirally/*” || req.url ~ “/pollmap/*” || req.url ~ “/api/get_category_post” || req.url ~ “/api/get_category_index_post_mobile” || req.url ~ “/api/get_tag_post” || req.url ~ “/api/get_post” || req.url == “^/nititv” || req.url == “^/election-2014” || req.url ~ “/api/get_category_index_post” || req.url ~ “home-html/*” || req.url ~ “recent_comments_call.html” || req.url ~ “latest_posts_call.html” || req.url ~ “latest_videoes_call.html” || req.url ~ “redis-election-2014-create.php” || req.url ~ “redis-nititv-create.php” || req.url ~ “nititv” || req.url ~ “election-2014” || req.url ~ “nitiworks” || req.url ~ “nitiworks-slave” || req.url ~ “action.php” || req.url ~ “iphonehome.js”) {
return (pass);
} else {
unset req.http.Cookie;
return(lookup);
}

if (req.request == “PURGE”) {
return (lookup);
}

if (req.request == “BAN”) {
# if (!client.ip ~ purgers) {
# error 405 “Not allowed.”;
# }
ban(“req.url ~ “+req.url+” && req.http.host == “+req.http.host);
error 200 “Ban added”;
}

if (req.request == “REFRESH”) {
set req.request = “GET”;
set req.hash_always_miss = true;
}

}

sub vcl_hit {

if (req.request == “PURGE”) {
# if (!client.ip ~ purgers) {
# error 405 “Not allowed.”;
# }
purge;
error 200 “Purged”;
}

if (req.request == “BAN”) {
# if (!client.ip ~ purgers) {
# error 405 “Not allowed.”;
# }
ban(“req.url ~ “+req.url+” && req.http.host == “+req.http.host);
error 200 “Ban added”;
}
}
sub vcl_miss {

if (req.request == “PURGE”) {
# if (!client.ip ~ purgers) {
# error 405 “Not allowed.”;
# }
purge;
error 404 “Not in cache”;
}

if (req.request == “BAN”) {
# if (!client.ip ~ purgers) {
# error 405 “Not allowed.”;
# }
ban(“req.url ~ “+req.url+” && req.http.host == “+req.http.host);
error 200 “Ban added”;
}
}
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (hash);
}

sub vcl_error {
if (obj.status == 750) {
set obj.http.Location = “http://www.niticentral.com/”;
set obj.status = 301;
return(deliver);
}

}

sub vcl_fetch {
set req.grace = 300s;
set beresp.do_esi = true;
set beresp.do_gzip = true;

if (beresp.status == 302 || beresp.status == 404 || beresp.status == 403 || beresp.status == 500) {
return(hit_for_pass);
}

if (req.url ~ “wp-login” || req.url ~ “wp-admin” || req.url ~ “admin_login” || req.url ~ “rightside_blog_collection_varnish.php” || req.url ~ “rightside_common_collection_varnish.php” || req.url ~ “rightside_hindi_varnish.php” || req.url ~ “rightside_infographics_collection_varnish.php” || req.url ~ “rightside_primenews_collection_varnish.php” || req.url ~ “rightside_topofthecharts_page_varnish.php” || req.url ~ “sidebar-tag-narendra-modi_varnish.php” || req.url ~ “rightside_common_single_varnish.php” || req.url ~ “sidebar-1col-poll-details.php”) {
return(hit_for_pass);

} elsif(req.url ~ “menu_bloggers_list.html” || req.url ~”load_trending_data.html” || req.url ~ “load_breakingnews_data.html” || req.url ~ “menu_latest_galleries_call.html” || req.url ~ “menu_latest_videos_call.html” || req.url ~ “today_nc.html” || req.url ~ “menu_election_2014.html”){
unset beresp.http.set-cookie;
remove beresp.http.Cache-Control;
remove beresp.http.Vary;
set beresp.http.Cache-Control = “public”;
set beresp.http.Vary = “Accept-Encoding”;
set beresp.ttl = 10m;
return(deliver);

} else {
unset beresp.http.set-cookie;
remove beresp.http.Cache-Control;
remove beresp.http.Vary;
set beresp.http.Cache-Control = “public”;
set beresp.http.Vary = “Accept-Encoding”;
set beresp.ttl = 24h;
return(deliver);
}

if (beresp.ttl <= 0s) {
set beresp.http.X-Cacheable = “NO:Not Cacheable”;

} elsif (beresp.http.Cache-Control ~ “private”) {
set beresp.http.X-Cacheable = “NO:Cache-Control=private”;
return(hit_for_pass);

} else {
set beresp.http.X-Cacheable = “YES”;
}
return(deliver);
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = “HIT”;
} else {
set resp.http.X-Cache = “MISS”;
}
remove resp.http.X-Varnish;
remove resp.http.Via;
remove resp.http.Server;
remove resp.http.WP-Super-Cache;
remove resp.http.X-Powered-By;
remove resp.http.X-Pingback;
}

#######################End of the Test#########################

 

Install the Wp-varnish Plugin on WordPress so that caching should automatically reset and add the servers in wp-varnish for multiple servers,

Once installation is done change the port of APACHE and set Varnish to port 80,

Restart the Apache first and then start the Varnish,

 

Your installation is finish and site will server from Varnish

 

Comments are closed.