Support #443

Installing Varnish For Load Balancing on FreeBSD

Added by Daniel Curtis about 6 years ago. Updated over 5 years ago.

Load Balancing
Target version:
Start date:
Due date:
% Done:


Estimated time:
1.00 h
Spent time:


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 packages are up to date.

Root access is required to edit the following files and to execute commands.

Install Varnish

  • Begin by installing Varnish:
    pkg install varnish

Configure Varnish Cache

  • You can use the default configuration.
    vi /usr/local/etc/varnish/default.vcl

For any other configuration please read the documentation.

For example to let Varnish Load Balancing know about your backend add the backend block to the top of the file. This implies that you have a running web server (Nginx, Apache etc.) under this location.

## backend
backend www {
  .host = "";
  .probe = {
    .url = "";
    .interval = 5s;
    .timeout = 1s;
    .window = 5;
    .threshold = 3;

## director
director wwwexamplecom round-robin {
  { .backend = www; }

## custom vcl_recv for load balancing
sub vcl_recv {
  set req.backend = wwwexamplecom;

  if ( ~ "" ) {
    set req.backend = wwwexamplecom;

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 Varnish
    service varnishd start


#1 Updated by Daniel Curtis about 6 years ago

  • Description updated (diff)
  • % Done changed from 50 to 70

#2 Updated by Daniel Curtis about 6 years ago

  • Status changed from In Progress to Closed
  • % Done changed from 70 to 100

Unfortunately Varnish does not support HTTPS, which is essential for my environment. So for the mean time I am continuing to use nginx.

#3 Updated by Daniel Curtis over 5 years ago

  • Target version set to FreeBSD 9

#4 Updated by Daniel Curtis over 5 years ago

  • Category set to Load Balancing

Also available in: Atom PDF