Installing Varnish on Ubuntu

Varnish is a HTTP accelerator which reduce the server load by caching the static pages and serving them to the browser. Varnish can greatly reduce your server load time if configured properly.

How Varnish Works:

Varnish acts as an intermediate between user request and your web server. When user sends a HTTP request, it firstly goes to Varnish, which will check if this is a cached page. If it is a cached page then Varnish will deliver it without bothering your web server.

Install Varnish using apt-get

Here I assume that you have already working nginx server. Open the terminal and run the commands below one by one.
sudo apt-get install apt-transport-https
curl | apt-key add -
echo "deb trusty varnish-4.0" >> /etc/apt/sources.list.d/varnish-cache.list
sudo apt-get update
sudo apt-get install varnish

Varnish Configuration on nginx

We will run the varnish on Port 80 and nginx will run on 8080. Lets configure it:
sudo nano /etc/default/varnish
Your code should look like:
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"

Now Edit /etc/varnish/default.vcl
backend default {
.host = "";
.port = "8080";
.connect_timeout = 60s;
.first_byte_timeout = 60s;
.between_bytes_timeout = 60s;
.max_connections = 800;

We are almost done now. Now we have to configure caching for specific pages. For example you don’t want to show cached pages for admin users then you can exclude the urls in configuration:
sub vcl_recv {
if (!(req.url ~ "(login|admin)")) {
unset req.http.cookie;

if (!(req.url ~ "ghost")) {
unset req.http.cookie;

Now lets restart Varnish and nGnix servers:
sudo service nginx restart
sudo service varnish restart

We can also configure caching based upon cookies as:
sub vcl_recv {
# Remove all cookies that backend doesn't need to know about.
# See
if (req.http.Cookie) {
/* Warning: Not a pretty solution */
/* Prefix header containing cookies with ';' */
set req.http.Cookie = ";" + req.http.Cookie;
/* Remove any spaces after ';' in header containing cookies */
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
/* Prefix cookies we want to preserve with one space */
/* 'S{1,2}ESS[a-z0-9]+' is the regular expression matching a Drupal session cookie ({1,2} added for HTTPS support) */
/* 'NO_CACHE' is usually set after a POST request to make sure issuing user see the results of his post */
/* Keep in mind we should add here any cookie that should reach the backend such as splahs avoiding cookies */
set req.http.Cookie = regsuball(req.http.Cookie, ";(S{1,2}ESS[a-z0-9]+|NO_CACHE|OATMEAL|CHOCOLATECHIP)=", "; \1=");
/* Remove from the header any single Cookie not prefixed with a space until next ';' separator */
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
/* Remove any '; ' at the start or the end of the header */
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

if (req.http.Cookie == "") {
/* If there are no remaining cookies, remove the cookie header. */
unset req.http.Cookie;

Leave a Reply

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


12 + fourteen =