Varnishing Polls

Accepting votes from anonymous users on Drupal polls is complicated when you use caching. By using Edge Side Includes (ESI), you can dynamically load some parts of a Drupal page while caching the rest of it. The addition of the Poll Enhancements module allows multiple anonymous users to vote on polls by using cookies to track who has voted.

Edge Side Includes integration
The ESI module includes two Varnish VCL files (located in the docs directory of the module:

  • docs/esi_blocks.vcl - custom sub-routines to handle ESI-block integration.
    Copy this file to the Varnish config directory, /usr/local/etc/varnish/

Purging the Varnish Cache

Various ways for removing entries from your Varnish cache


Stop image bandwidth theft

Prevent other websites from stealing your bandwidth by including your images in their pages.

  • Apache
    This example services up a different image instead:

    RewriteEngine On
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} ^http://.*$
    RewriteRule \.(jpe?g|gif|bmp|png)$ /media/nohotlinks.png [L]
  • Lighttpd
    $HTTP["referer"] !~ "^(http://example\.com|http://www\.example\.com)" {
      $HTTP["referer"] != "" {
        url.access-deny = ( ".jpg", ".jpeg", ".png", ".gif", ".pdf" )

Varnish Configuration

Extra Header Information
Add some extra HTTP header details to tell whether the page you are getting came from the Varnish cache by adding this snippet to /usr/local/etc/varnish/drupal.vcl:

sub vcl_deliver {
  #add cache hit data
  if (obj.hits > 0) {
    #if hit add hit count
    set resp.http.X-Varnish-Cache = "HIT";
    set resp.http.X-Cache-Hits = obj.hits;
  } else {
    set resp.http.X-Varnish-Cache = "MISS";

Grace for Misbehaving Backends


Varnish Module

The Varnish HTTP Accelerator Integration module provides integration between your Drupal site and Varnish. This module provides admin-socket integration which allows Drupal to dynamically invalidate cache entries, and also lets you query the Varnish admin interface for status, etc.


  • PHP must be compiled with --enable-sockets
  • Edit /service/varnish/run and add -T to start the admin interface on the default module port.



Running Varnish in front of your web server will mess up your normal Apache log files as the majority of traffic will be served from the cache. Varnish normally writes its log information to memory, but includes the varnishncsa program which displays the data in the Apache/NCSA combined format and can write that data to a file.

varnishncsa Startup
With Varnish installed from source, daemontools is an easy way to run the logging daemon. If you haven't already, install daemontools.

Create a directory for the varnishncsa service:

mkdir -m 1755 /var/service/varnishncsa
cd /var/service/varnishncsa

Create the run script and make it executable:

echo '#!/bin/sh' > run
echo 'exec 2>&1' >> run
echo 'exec varnishncsa' >> run
chmod 755 run



Accelerate your Drupal/Pressflow site using Varnish

The primary reason for switching to Pressflow is to use an HTTP accelerator such as Varnish
Apache Configuration
Adjust your Apache configuration to server the Drupal site on port 8080. Edit your Apache conf file and add:

Listen 8080
NameVirtualHost *:8080

Restart Apache and make sure you can access your site on that port.

Varnish Configuration


In addition to the basic development tools, there's a few other dependencies:

yum install ncurses-devel pcre-devel libtool

Download the Varnish source code:

cd /extra/src
tar zxf varnish-2.1.5.tar.gz
cd varnish-2.1.5

Configure and build it:


If you want, you can run the tests before installing:


Subscribe to RSS - varnish

Recent Updates

  • 2 years 1 month ago
  • 2 years 1 month ago
  • 2 years 1 month ago
    php 8.x
  • 2 years 2 months ago
  • 2 years 2 months ago
    Drop Centos 5/6 stuff