5

I have a poll on my front page that I want users to be able to vote on, but anonymous users always just see the results and are never able to vote. I've been trying just about everything I can think of to fix this and nothing works.

I'll start by saying that I can access my web server directly through an alternate port. When I do this I see the poll when I'm not logged in; so I know that anonymous users can vote.

I've tried using ESI and the Drupal 7 ESI module. I got it set up and working so that the poll block is being loaded via an ESI tag (obviously accessing it directly through the alternate port stopped working when I did this since browsers don't parse ESI tags). No dice.

I tried using the AJAX Blocks module, I set a delay on loading the block just so I could visibly make sure the block was loading via AJAX, it was and it was still just displaying the results.

I tried voting from the same IP address using the alternate port, thinking that since someone has voted going through Varnish before the server wont allow it again, but I can vote twice from the same IP address through the alternate port.

I tried logging into the server via ssh and using Links to view the site, thinking that maybe it's the local IP address that is making the web server serve the results page for some other reason, but when I use Links I can see the voting form.

Clearly there's something that's causing Drupal to serve the results instead of the form when Varnish requests the page, but what is it?!?

  • 1
    Have you looked at Varnishing Polls? ESI + Poll Enhancements looks to be the combo you're looking for. – mrP May 22 '13 at 0:58
  • It sounds like it could be one of two things; either the page is caching for anonymous users, in which case you could use the authcache module to make sure it doesn't cache for anonymous users, or you simply haven't given users permission to vote on the poll - check there first. – CR47 Sep 19 '13 at 15:04
1

https://drupal.org/project/pollanon This lets anonymous users vote in a poll more than once, and here's an article on using ESI to allow parts of your page to remain cached while other parts remain dynamic.

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

Source: http://www.productionmonkeys.net/guides/drupal/performance/pressflow/varnish/varnishing-polls

0

I know this is old but it ranks well in Google so others may still stumble upon this.

If you don't want to use blocks (in my case we have hundreds of polls, a block for each has overhead) then you can simply not cache the page that the poll is on;

/**
 * Implements hook_node_view().
 */
function your_module_node_view($node, $view_mode, $langcode) {
  // Don't cache pages with polls. This needs to be changed to an ESI solution.
  if ($node->type == 'poll') {
    if (in_array($view_mode, array('full', 'default', 'embedded'))) {
      drupal_page_is_cacheable(FALSE);
    }
  }
}

This just avoids the page cache. Performance on our production server is still good, the polls make up only a small part of the content so the other pages are still cached with Varnish.

I have a comment in there for ESI - we're going to investigate using ESI on a Paragraph or a Field Group as thats what we use.

0

I stumbled upon this question because I wanted to see how well my new (sort of.. it has been a sandbox project for ages) contrib module ranks for a search on this issue. This is the top result, so I'll link to the module here: http://drupal.org/project/cached_poll. It addresses exactly this problem.

A benefit of the module over Christian's solution includes that pages with Polls still are cached (depending on where exactly you show the Poll, this may be an important consideration; you don't really want your frontpage excluded from caching just because it shows a poll). The module prevents the version of the poll showing results getting cached, but it still allows the non-voted version (i.e. the voting form) into the cache.

ESI is something that I researched a long time ago. At least back then, it didn't make the cut and was too complicated to set up and get working reliably with Drupal. It also led directly to the development of this module.

  • Looks like your module is not covered by the security advisory policy. Any plans on updating it? – Keven Mar 9 '17 at 22:27
  • Thanks, I had practically forgotten about this module. I've updated some of the meta data, created a 1.0 release and opted into the security policy. I'm not sure how this is going to work exactly, possibly a security review is performed before the sought-after shield appears next to the release. – Eelke Blok Mar 11 '17 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.