Irfan Fadilah's Blog

Sometimes I Write Something, Sometimes I Don't.

Accelerate Your Websites Using Varnish

I believe many of you ever heard of Varnish when visiting some websites. If you wonder what is that Varnish-thing, we can simply call it a software for accelerate any website using caching method. Varnish also very well known and popular since around 10% of top 10.000 websites in internet use it to speeds up their websites.

The New York Times, The Guardian, The Hindu, Corriere della Sera, also social media and content sites such as Wikipedia, Facebook, Twitter, Vimeo, and Tumblr, they're all using Varnish. Pretty interesting, isn't it? Before we go any further, here's short explanation about Varnish from it's official website:

Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300-1000x, depending on your architecture.

300-1000x faster, wow, imagine that speeds. In this post I will cover basic instalation and usage of Varnish based on my experience. Basically any Linux based OS with amd64 architecture are supported, however Ubuntu is more preferable. But please also note that Varnish only support LTS releases of Ubuntu, so we're gonna use 14.04 (Trusty Tahr) LTS. You can use local computer or VPS, the process is not that different though.

Installation

sudo apt-get install apt-transport-https
curl https://repo.varnish-cache.org/GPG-key.txt | apt-key add -
echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.1" >> /etc/apt/sources.list.d/varnish-cache.list
sudo apt-get update
sudo apt-get install varnish

Note: If you get Permission Denied warnings for step 2 and 3, you might need to login as root using sudo -i and rerun the commands, when it's done, logout from root using exit command.

Starting Varnish

You will see the printed log: "Starting HTTP accelerator varnishd ... done", it's mean Varnish is successfully installed and running. If not, try to run it manually using this command:

sudo service varnish start

Then navigate to http://127.0.0.1:6081 (change 127.0.0.1 with server IP address if you installed Varnish on VPS). You will probably see "503 Backend fetch failed" error message, it's because there's no web application being served up, since the default configuration will try to forward requests to a web application running on the same machine as Varnish was installed on. Varnish expects the web application to be exposed over http on port 8080.

Ok, now you get yourself Varnish, let's run your web app as usual, but try to run it on port 8080. In this case I will use a Ruby on Rails application, but you can use any platforms: Sinatra, Node, Meteor, etc.

rails s -p 8080

Note that I specify port 8080 for the sake of Varnish. Now visit http://127.0.0.1:6081 again in your browser and you should see your web app being served by Varnish, yay!

Well, it's something. But what if you want Varnish to point other port than 8080? Port 3000 for example, the default port for Rails app. You can do it by editing the configuration file, let's get back to our teminal and run:

sudo nano /etc/varnish/default.vcl

You can replace nano with your favorite text editor like gedit or something like that. After that, in configuration file you'll see something like this:

vcl 4.0;

backend default {
  .host = "127.0.0.1";
  .port = "8080";
}

You change the host and port to point to your application, in this case, we will just change the port to "3000". When you done, save it then run this command to restart Varnish:

sudo service varnish reload

Restart your Rails app and make sure to run on default port then open http://127.0.0.1:6081 again to see it's now backed by Varnish on port 3000.

Put Varnish on Port 80

If you use web servers such Nginx or Apache, you may skip this and continue read last section.

We already have Varnish running with our app, but if we installed it on VPS we may want to place Varnish on port 80. Let's put Varnish on the default HTTP port, 80. First, we need to stop Varnish with sudo service varnish stop. When it's successfully stopped, go to your terminal and fire sudo nano /etc/default/varnish to open Varish config file. You'll see something like:

DAEMON_OPTS="-a :6081 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"

Change :6081 with :80, once the change is done, save the file and start Varnish with this command: sudo service varnish start. Now everyone accessing your public IP will be accessing through Varnish.

Cool, Now What?

You can also integrate Varnish with web servers like Nginx or Apache and make it to listen Varnish. The screnario might look like this: Nginx > Varnish > Rails App.

There's still lot of things you might want to discover, the first destination is the official website of course. You can find any informations you need there, documentations, books, news.


Well, that's all for Varnish. I hope this article will be helpful for you and feel free to ask in comments box below if you have any questions. :)

References: Varnish Official Website & Wikipedia