Project

General

Profile

Support #443

Installing Varnish For Load Balancing on FreeBSD

Added by Daniel Curtis over 9 years ago. Updated about 9 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Load Balancing
Target version:
Start date:
08/29/2014
Due date:
% Done:

100%

Estimated time:
1.00 h
Spent time:

Description

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.

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

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

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

  if ( req.http.host ~ "www.example.com" ) {
    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
    

varnishd_enable="YES"
varnishd_listen=":80"
varnishd_config="/usr/local/etc/varnish/default.vcl"
varnishd_storage="malloc,4G"
varnishd_admin=":6082"

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 over 9 years ago

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

Updated by Daniel Curtis over 9 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 about 9 years ago

  • Target version set to FreeBSD 9
#4

Updated by Daniel Curtis about 9 years ago

  • Category set to Load Balancing

Also available in: Atom PDF