Integrating Drupal with Cloudflare

The super smart lead developer at Xeno Media first brought the Cloudflare service to my attention as it was integrated with one of the hosts we were using.  We experimented with a site that was experiencing outages due to traffic spikes it received after sending an email newsletter.  After the switch, the server never went down again, saw considerable speed improvements, and we were able to quadruple the number of emails we could send per hour, with no performance hits.

According to Cloudflare, sites that use their service:

  • Load twice as fast
  • Use 60% less bandwidth
  • Have 65% fewer server requests
  • Are way more secure

If a free Content Delivery Network that makes your site seriously super awesome, incredibly blazing fast wasn't enough, the more you look at Cloudflare's features, the more you will be impressed.  Not only does Cloudflare serve your site from a distributed network, closer to where the user actually is, it optimizes, aggregating content from different external services (think Google/Facebook/Marketo) into one, speeding up delivery to the browser

Thanks to Cloudflare's large reach, they are aware of, and are able to block security threats including DDOS attacks, and block nuisances like spam bots before they even reach your server.  For more detailed information, visit Cloudflare's Security page.  Because of the data, Cloudflare insists that their analytics are more accurate than Google.  Think of your old server analytics from Webstats, or AWStats with the bots and spam removed.  That's what Cloudflare reports.

The Pro Plan brings more speed and better mobile optimizations if you can believe it, a Web application firewall (WAF), and real time statistics.  The Business plan brings all that plus full customization, including partial page caching, and better optimization/deliver, DDOS protection, and a 100% uptime SLA.

Setting up Cloudflare is ridiculously easy.  Their onboarding strategy should be rewarded and mimicked extensively.  The base account is free, and takes less than 5 minutes.

Signup at Cloudflare.com

  • Sign up for an account at cloudflare.com
  • Enter the URL of your site, if they can, they will gather the DNS records for it already.
  • If they can't gather that data, open up your DNS server/domain registrar and copy the DNS settings from the old into Cloudflare.
  • At your domain registrar, point your Name Servers to Cloudflare

Integrating Cloudflare with Drupal

Because we are now pointing the DNS to Cloudflare, and Cloudflare is pointing towards your web server, we don't necessarily need to do anything else, but using the the Drupal Cloudflare Module brings the following benefits:

  • Corrects the referrer so it contains the IP address of your visitor, instead of the CloudFlare server IP.
  • Integration with CloudFlare's Threat API so you can ban/whitelist IP addresses from the Drupal Comment admin screen if you use Drupal's Core Commenting.

Install and Configure the Drupal Cloudflare Module

  • Download the Drupal Cloudflare Module
    drush dl -y cloudflare
  • Enable the Drupal Cloudflare Module
    drush en -y cloudflare
  • Set permission for the module
  • Enter the configuration, email and API Key for the Cloudflare account into Drupal at:
    /admin/config/people/cloudflare
  • Add the following code at the end of your production server settings.php file:
    
    if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
      $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
    }
    
  • Save and wait for the DNS to update!

Downsides

I would be remiss in saying that there aren't any downsides.  It is another cache to clear if you publish content, and need to make corrections.  You may find your self needing to clear the Drupal cache, and login to Cloudflare and purge the specific URL you are working on from their cache also. This is far from a deal breaker for me, but real life feedback I have received from clients.