How-to: Install Varnish and Nginx on ARM Servers

With the release of the Raspberry Pi 3 Model B, we read that Mythic Beasts was hosting a portion of the traffic going to the RaspberryPi.org site on an actual Pi 3! Pretty neat! We took a closer look and realized there wasn’t too much magic going on here, so we decided to write a quick how-to on setting up Varnish to cache content and speed up rendering and delivery of dynamic web pages. Since our little ARM servers are (obviously) not the most powerful platforms around, this can dramatically increase the performance and responsiveness of web servers running on ARM.

This guide will detail how setup Varnish and Nginx on a Debian 8 “Jessie” Server running on ARM.

First and foremost, we need to perform the actual installation of the software, then we can configure it. We simply run:

sudo apt-get install nginx varnish.

Now, we need to make some changes to setup the environment properly.

The first file we need to take a look at is the Nginx configuration file which should be located at /etc/nginx/sites-available/default

Specifically, we need to move Nginx off of port 80 so that Varnish will be able to take over port 80 traffic. For this example, we will place Nginx on port 8080 instead, so, we update the file to reflect the following:

server
listen 8080; {
root /usr/share/nginx/html;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost.localdomain;

(We are not using a fully qualified hostname in this example, so later on we will go update our hosts file to reflect this.)

Next, we can configure Varnish to go look for Nginx on port 8080, by editing the Varnish config file located at /usr/local/etc/varnish/default.vcl and editing the ‘backend’ section. Here is a simple example: (Note – This one has some common WordPress login pages excluded from being cached)

vcl 4.0;
# Based on: https://github.com/mattiasgeniar/varnish-4.0-configuration-templates/blob/master/default.vcl

import std;
import directors;

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

sub vcl_recv {
# Do not cache following pages (edit as needed for your configuration).
if (!( req.url ~ "wp-(login|admin|comments-post)" )) {
return (pass);
}

# Drop the cookies for everything else
unset req.http.cookie;

# Try a cache-lookup
return (hash);
}

sub vcl_backend_response {
# Below will cache the page for one week.(1s = 1 sec, 1d = 1 day)
set beresp.ttl = 1w;
}

Once Varnish knows where to look for Nginx, we can now set Varnish to take over port 80 that we previously freed up. To do this, we need to modify the Varnish start file, which in this case is located at /etc/systemd/system/varnish.service because we are using Debian. If you are on another distro, this location may vary. We need to change the ‘-a’ flag from -a :6081 to -a :80, then save and close the file.

The last change we need to make is to our hosts file, to ensure that the local network is interpreted correctly. We need to edit the /etc/hosts file, and add a line that translates 127.0.0.1 to localhost and localhost.localdomain:

127.0.0.1 localhost localhost.localdomain

At this point, we could technically restart the Nginx and Varnish services to read in these changes, but a reboot is probably a good idea as well if you can afford it.

Once the server comes back up, you should now have Varnish running on port 80 caching content from your Nginx web server. One way to test this is to go to http://www.isvarnishworking.com, and put in your IP address (keep in mind, you need a public facing IP for this). If everything is working, you should see a message like this:

varnish-nginx-on-arm-server

Additionally, you can run ‘sudo varnishstat’ to see a detailed breakdown of how much caching is being done by Varnish.

So, thanks go to Raspberry Pi for inspiring us to attempt to duplicate their work. Hopefully this helps you install Varnish and Nginx on ARM, and speed up web page rendering on your Pi’s or other small devices!

Leave a Reply