Installing and configuring Varnish on Ubuntu

On a brand new debian/*ubuntu distro, there are only a couple steps to do in order to make Varnish act as a proxy do its job in front of apache.

Installation:

1
sudo apt-get install varnish

then you need to configure it to listen to the :80 port, editing /etc/default/varnish:

1
2
3
4
5
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,1G"

and telling it to forward requests to apache ( we’re gonna make it listen to the 8090 ), into the /etc/varnish/default.vcl:

1
2
3
4
5
6
7
8
9
backend apache {
        .host = "127.0.0.1";
        .port = "8090";
}
sub vcl_fetch {
        remove req.http.X-Forwarded-For;
        set    req.http.X-Forwarded-For = req.http.rlnclientipaddr;
        return(deliver);
}

Then it’s time to tell apache it has to listen to a new port, in /etc/apache2/ports.conf:

1
2
NameVirtualHost *:8090
Listen 127.0.0.1:8090

Make sure the hosts in your /etc/apache2/httpd.conf listen to :* or :8090, then you can start the services:

1
2
/etc/init.d/apache2 start
/etc/init.d/varnish start

Now you have varnish responding to all the hosts on the :80: if you experience some troubles try to go deeper with this extended guide.


Hi there! I recently wrote an ebook on web application security, currently sold on leanpub, the Amazon Kindle store and gumroad.

It contains 160+ pages of content dedicated to securing web applications and improving your security awareness when building web apps, with chapters ranging from explaining how to secure HTTP cookies with the right flags to understanding why it is important to consider joining a bug bounty program.

Feel free to skim through some of the free chapters published on this blog and, if the content seems interesting enough to you, grab a copy on leanpub, the Amazon Kindle store, gumroad or simply checkout right down below!

Buy the Web Application Security ebook for $9.99

In the mood for some more reading?

...or check the archives.