Installing Varnish & Configuring it for Wordpress On Centos

Status
Not open for further replies.

Ifirst

Active Member
281
2011
133
0
In this tutorial i am going to teach you how to install varnish and configuring it for wordpress on a Centos server.
What is needed for this work:
1.SSH access to your server

Connect to your server via your SSH client and execute the following commands
First we install varnish on the machine
Code:
yum install varnish
Next we start the service
Code:
/etc/init.d/varnish start
Make sure that varnish is automatically started on server reboot
Code:
chkconfig --level 345 varnish on
Open default.vcl
Code:
nano /etc/varnish/default.vcl
Make sure you default.vcl matches exactly the one shown below.
# Back-End
backend default {
.host = "127.0.0.1";
.port = "80";
.connect_timeout = 60s;
.first_byte_timeout = 10s;
.between_bytes_timeout = 10s;
}

# Custom
sub vcl_recv {
remove req.http.X-Forwarded-For;
set req.http.X-Forwarded-For = client.ip;

if (req.url ~ "^/wp-(login|admin)") {
return (pipe);
}

if (req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)") {
return (pipe);
}

if (req.request == "POST") {
return (pass);
}

if (req.http.Cache-Control ~ "no-cache") {
return (pass);
}

if (req.http.Authorization) {
return (pass);
}

if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|htm|html)$") {
unset req.http.Cookie;
unset req.http.Accept-Encoding;
unset req.http.Vary;
return (lookup);
}

if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
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 {
remove req.http.Accept-Encoding;
}
}

if (req.backend.healthy) {
set req.grace = 30s;
} else {
set req.grace = 1h;
}

unset req.http.Cookie;
unset req.http.Vary;
return (lookup);
}

sub vcl_fetch {
set beresp.grace = 1h;
unset beresp.http.set-cookie;
if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|htm|html)$") {
set beresp.ttl = 24h;
} else {
set beresp.ttl = 5m;
}
return (deliver);
}

sub vcl_deliver {
if(obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
set resp.http.Cache-Control = "private";
set resp.http.Pragma = "private";
remove resp.http.X-Varnish;
remove resp.http.Via;
remove resp.http.Age;
remove resp.http.Server;
remove resp.http.X-Powered-By;
}

sub vcl_pipe {
set bereq.http.connection = "close";
}
Finally you restart vanish for changes to take effect
Code:
/etc/init.d/varnish restart
All done , your websites are now accelerated by varnish
 
Last edited:
Status
Not open for further replies.
Back
Top