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/
  • docs/default.vcl - example code showing how to include the ESI blocks VCL in your Varnish config. Portions of this should be added to appropriate places your Varnish config file. We'll do it by creating a copy, /usr/local/etc/varnish/default-esi.vcl, that has the portions we want:
    include "/usr/local/etc/varnish/esi_blocks.vcl";

    sub vcl_recv {
      call esi_block__recv;
    }

    sub vcl_hash {
      call esi_block__hash;
    }

    sub vcl_fetch {
      # don't ESI anything with a 3/4 letter extension
      # (e.g. don't try to ESI images, css, etc).
      if (! req.url ~ "\..{3,4}$") {
        esi;
      }

      call esi_block__fetch;
    }

    Then, edit /usr/local/etc/varnish/drupal.vcl and add a line near the beginning:

    include "/usr/local/etc/varnish/default-esi.vcl";

    Restart the Varnishcache:

    svc -t /service/varnish

With the module enabled in your Drupal site, block configuration pages now have an ESI Settings section with two options: Enable ESI, and a TTL value.

Poll Enhancements
Installing this module allows anonymous users to vote on polls. By using cookies to track who has voted, multiple users who are behind a firewall will be able vote on polls. There's a threshold setting for how many votes from a single IP in a time period. The module also provides a Use AJAH option - I needed to enable this to have the ESI-enabled Poll block work properly.

Configure Varnish to ignore the poll cookie ( pa-[nid] ) by editing the vcl_recv section of /usr/local/etc/varnish/drupal.vcl and inserting a line to remove the cookie:

// Remove pollanon cookies.
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(pa(.*))=[^;]*", "");

Restart Varnish:

svc -t /service/varnish

Recent Updates

  • 7 months 1 week ago
  • 7 months 1 week ago
  • 7 months 1 week ago
    php 8.x
  • 7 months 1 week ago
    10.6.7
  • 7 months 1 week ago
    Drop Centos 5/6 stuff