How to Install Varnish on Ubuntu?

[ad#mconnect-solutions-1]

Varnish is the type of cache which is used to optimize speed of the server, my experience says that it will just doubled the speed of your website in just short time.

Below are the points how to install varnish on ubuntu, centos and other unix and linux interfaces.

apt-get install varnish

Go to “/etc/default/varnish” and edit 6081 to 80 and let varnish listen to port 80

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

You should change the storage size also to the size of your server.

Edit /etc/apache2/ports.conf and change the port setting to listen port 80 for varnish and let apache listen to local host at 127.0.0.1 on port 8000 or whatever port you want

Then change your apache2 virtual host file to listen to the new port.

install this for apache: sudo aptitude install libapache2-mod-rpaf

Once you are done…you can run varnishstat to see if it is working. hope this helps 🙂

(make sure you change the admin28 to your admin name for backend and also change the port to the port you have on apache…..)

————————– BELOW IS THE VARNISH VCL ——————————–

backend apache {
.host = “localhost”;
.port = “8000”;
.max_connections = 30;
.connect_timeout = 4.0s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
}

acl purge {
“localhost”;
“127.0.0.1”;
}

sub vcl_recv {
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 == “GET” && (req.url ~ “^/admin28”) || req.url ~ “^/index.php/admin28”) {
return (pipe);
}

# Force lookup if the request is a no-cache request from the client
#if (req.http.Cache-Control ~ “no-cache”) {
#return (pass);
#}

## Default request checks
if (req.request != “GET” &&
req.request != “HEAD” &&
req.request != “PUT” &&
req.request != “POST” &&
req.request != “TRACE” &&
req.request != “OPTIONS” &&
req.request != “DELETE”) {
# Non-RFC2616 or CONNECT which is weird.
return (pipe);
}
if (req.request != “GET” && req.request != “HEAD”) {
# We only deal with GET and HEAD by default
return (pass);
}

## Modified from default to allow caching if cookies are set, but not http auth
if (req.http.Authorization) {
/* Not cacheable by default */
return (pass);
}

// Strip cookies for static files:
if (req.url ~ “\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$”) {
unset req.http.Cookie;
return(lookup);
}

// Remove has_js and Google Analytics __* cookies.
set req.http.Cookie = regsuball(req.http.Cookie, “(^|;\s*)(__[a-z]+|has_js)=[^;]*”, “”);

// Remove a “;” prefix, if present.
set req.http.Cookie = regsub(req.http.Cookie, “^;\s*”, “”);

// Remove empty cookies.
if (req.http.Cookie ~ “^\s*$”) {
unset req.http.Cookie;
}

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

sub vcl_hash {
if (req.http.Cookie) {
set req.hash += req.http.Cookie;
}
}

sub vcl_fetch {

// Strip cookies for static files:
if (req.url ~ “\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$”) {
unset beresp.http.set-cookie;
}

// Varnish determined the object was not cacheable
if (!beresp.cacheable) {
set beresp.http.X-Cacheable = “NO:Not Cacheable”;
}

// You don’t wish to cache content for logged in users
elsif(req.http.Cookie ~”(UserID|_session)”) {
set beresp.http.X-Cacheable = “NO:Got Session”;
return(pass);
}

// You are respecting the Cache-Control=private header from the backend
elsif ( beresp.http.Cache-Control ~ “private”) {
set beresp.http.X-Cacheable = “NO:Cache-Control=private”;
return(pass);
}

// You are extending the lifetime of the object artificially
elsif ( beresp.ttl < 1s ) {
set beresp.ttl = 300s;
set beresp.grace = 300s;
set beresp.http.X-Cacheable = “YES:Forced”;
}

// Varnish determined the object was cacheable
else {
set beresp.http.X-Cacheable = “YES”;
}

return(deliver);
}

sub vcl_error {

if (obj.status == 503 && req.restarts < 5) {
set obj.http.X-Restarts = req.restarts;
restart;
}

}
[ad#mconnect-solutions-1]
[ad#yogesh-1]
[ad#mconnect-solutions-1-1]

 

This post created by M-Connect Media (www.mconnectmedia.com) a Magento design agency.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s