GEO IP Locations in: France IP Location United Kingdom IP Location Germany IP Location Italy IP Location Netherlands IP Location Spain IP Location United States of America IP Location Canada IP Location
Home » Hosting Industry » How to Use Varnish to Speed up my Website?

How to Use Varnish to Speed up my Website?

Varnish Logo

Have you ever experienced posting something on your blog, being tweeted by readers a couple of times until the popularity of what you have written builds up and hundreds of people eventually visit your blog at once, only to be disappointed in the end because people keep complaining that they cannot access your website, and they are in fact tweeting that your website is down, and that a data connection error appears? If so, then you are not the only blogger or website owner who often gets dismayed when your website becomes unstable after it is visited by hundreds of readers.

There may also have been an instance when a celebrity tweets about the product that you advertise in your website, and so out of nowhere, thousands of people begin looking at your website, only for the domain to stop responding and begin timing out. Even though you have continuously apologized on Twitter about the mishap, people become disinterested and begin moving on with their lives, preventing you from actually taking advantage of that short moment of fame.

These scenarios are very common, and if you have experienced this, you are not the only one who has. Thankfully, you can use Varnish web accelerator to prevent these types of things from happening to your website. Here is how.

The Varnish web accelerator will serve the requested page and assets from memory. Every other aspect, including Apache, MySQL, and PHP, will be left untouched. The Varnish web accelerator will hand the task to Apache only if the browser requests something which is not cached yet. This results in Apache doing the job only once, which means that it will allow you to configure more than one back end.

Read also our article: What is a Varnish Cache Server?

Install Varnish web accelerator. The instructions regarding how to install the Varnish web accelerator are fairly simple and are usually found in the program’s documentation, so you do not have to go browsing the Internet to look for a way for you to install the program with ease. You can follow the instructions provided for Debian and use Debian Wheezy as your root. After you have installed Varnish, you will be provided with a terminal line which would indicate that the installation was successful.

Reconfigure your Apache. Once the web Varnish has been installed, you can now begin reconfiguring Apache. In order to change the port where Apache is listed on as root, open /etc/apache2/ports.conf and look for these lines:

NameVirtualHost *:80
Listen 80

You will then need to change these lines to:

NameVirtualHost *:8080
Listen 8080

Once you are done, save this and look for the line “” and replace it with “”.

Configure Varnish web server. After you have reconfigured Apache, you will now need to configure the Varnish web program by opening up /etc/default/varnish and editing DAEMON_OPTS to look like the following:

DAEMON_OPTS=”-a :80
-T localhost:1234
-f /etc/varnish/default.vcl
-S /etc/varnish/secret
-s malloc,256m”

After you have changed this, open /etc/varnish/default.vcl to make sure that the default back end has been set to port 8080. Afterwards, restart the program.

Clean the cache. You will now need to clean the cache in order for the changes to take effect. Usually, people will find it baffling that after the Varnish web cache has been installed and configured, the effects still cannot be seen. To see the changes, you will need to type in the command line “varnishadm “ban.url.” to clear the cache. After this, you have successfully installed Varnish and you’re now ready to go!

Now you might be wondering how you can do more with Varnish. Most will likely want to know more about Varnish so that they would be able to change it whenever they want. Using the steps listed above to reconfigure the website usually does the trick of having basic pages and assets served from the cache after they have been put into memory. There are, however, some elements in your website which might not be cached by Varnish because it is not configured to do so. One of these elements is cookies, and the steps in how these can also be cached by Varnish are different from the steps listed above.

To do this, let us assume that your admin area is located at /admin. You would need to edit /etc/varnish/default.vcl and add:

sub vcl_recv {
if ( !( req.url ~ ^/admin/) ) {
unset req.http.Cookie;
}
}

This will essentially clean up the cookies. However, there are some cookies that are important and you might not want to get rid of those. To be more specific in how Varnish analyzes and caches cookies, you can add:

sub vcl_recv {

Thankfully, you can use Varnish web accelerator to prevent these types of things from happening to your website.

// 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*”, “”);
}

For a more detailed explanation, you can also find a section on cookies within Varnish’s documentation.

Leave a Reply

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

*
*