Project

General

Profile

Support #443

Updated by Daniel Curtis over 9 years ago

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. 

 h2. Install Varnish Cache 

 * Begin by installing Varnish: 
 <pre> 
 pkg install varnish 
 </pre> 

 h2. Configure Varnish Cache 

 * You can use the default configuration.  
 <pre> 
 vi /usr/local/etc/varnish/default.vcl 
 </pre> 

 For any other configuration please read the documentation. documentation here. 

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

 For example to let Varnish Load Balancing know about your backend add the backend block to the top of the file. This example will redirect unchached requests to the IP 123.123.123.123 on port 81. This implies that you have a running web server (Nginx, Apache etc.) under this location. 
 <pre> 
 ## backend www.example.com backend 
 backend www { 
   .host = "www.example.com"; 
   .probe = { 
     .url = ""; 
     .interval = 5s; 
     .timeout = 1s; 
     .window = 5; 
     .threshold = 3; 
   } 
 } 

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

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

   if ( req.http.host ~ "www.example.com" ) { 
     set req.backend = wwwexamplecom; www.example.com; 
   } 
 

 } 
 </pre> 

 h3. 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. 
 <pre> 
 vi /etc/rc.conf 
 </pre> 
 > varnishd_enable="YES" 
 > varnishd_listen=":80" 
 > varnishd_config="/usr/local/etc/varnish/default.vcl" 
 > varnishd_storage="malloc,4G" 
 > varnishd_admin=":6082" 


 h3. 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. 
 <pre> 
 varnishd -C -f /usr/local/etc/varnish/default.vcl 
 </pre> 

 Start Varnish Cache 

 * Now you can start Varnish 
 <pre> 
 service varnishd start 
 </pre> 

Back