December 12, 2018

How to Install Varnish Cache on FreeBSD

Varnish Cache Logo

Caching dynamic contents can heavily increase your overall website speed. Varnish is a HTTP accelerator designed for content-heavy dynamic web sites. It was designed from the ground up as a HTTP accelerator and is focused exclusively on HTTP. Varnish is distributed through the FreeBSD ports system and we recommend you use the packages provided through there. Make sure your ports tree is up to date.

Root access is required to edit the following files and to execute commands. Log in as root (su) or simply prepend sudo to all commands that require root privileges.

Install Varnish Cache

I have choosen the default installation options without IPV6.

cd /usr/ports/www/varnish
make install clean; rehash

Configure Varnish Cache

You can use the default configuration. For any other configuration please read the documentation here.

vi /usr/local/etc/varnish/default.vcl

For example to let Varnish Cache know about your backend add the backend block to the top of the file. This example will redirect unchached requests to the IP on port 81. This implies that you have a running web server (Nginx, Apache etc.) under this location.

backend default {
  .host = "";
  .port = "81";

Add Varnish Cache to your rc.conf

This configuration will start the Varnish Cache on system start, binding it to port 80 and keeps 4 GB of cached data in memory. A good choice is to use no more than 80% of your total memory. The admin port listens on port 6082.

vi /etc/rc.conf

Test your configuration

After the default.vcl may have been changed you need to check it with the following command. The output will help you to find possible syntax failures.

varnishd -C -f /usr/local/etc/varnish/default.vcl

Start Varnish Cache

Now you can start the Varnish Cache.

/usr/local/etc/rc.d/varnishd start

Speak Your Mind