Making Drupal Play Nicely with a CDN

Achieve works on many large-scale Drupal deployments (what we call Enterprise Drupal websites). We also work on many sites aspiring to reach this Enterprise level of deployment. During the process of growing a Drupal website, we leverage a host of technologies and apply various insider “tricks of the trade” to produce the best possible performance for every site we deploy. One of the most-common performance improvements we're asked to implement is to leverage a content distribution network or content delivery network (CDN). Below, you will discover two small patches that Achieve has been utilizing and learn more about the relatively painless steps involved with integrating a CDN.

To begin, you will first need to set up the basics to allow serving of static content from the CDN. Tag1 has a patch, originally created by Ted Serbinski (m3avrck)), which does just that. You can read more about it here.  As of this article, they didn't have an up to date version against the latest version of Drupal (6.19 as of this writing), but you can download their patches here. Attached here is a version rolled against Drupal 6.19 in case that proves easier. The simple explanation for this patch is that it's setting up publicly accessible user-uploaded files, CSS, JavaScript, and theme images to potentially be served by a different URL. Note that this does not mean it must be served by a different URL, but at the very least, you will have that option.

Next, you'll need to set $conf values to determine what is going to be served by a different host. Tag1's post goes into this, so I won't go too far into it. The quick version is that cdn_base_url becomes the default domain for serving files, CSS, JavaScript, and theme images, but you can override any of the elements by setting the appropriate $conf value. If you would like all static content to be served by http://cdn.sample.com, except uploaded files, it should come from http://cdn2.sample.com. Then you can set values as seen in the example below (see Tag1 for another example):


$conf = array(
  'cdn_base_url' => 'http://cdn.sample.com',
  'file_base_url' => 'http://cdn2.sample.com',
);

 
If the performance improvement from serving the files, CSS, JavaScript, and theme images is sufficient for you: congratulations, you're done (at least, with the Drupal side). See your CDN provider for setup requirements on that end.

Achieve's clients, in general, also want to improve performance by serving ImageCache images and SWFTools uploaded flash video. Because both SWFTools and ImageCache directly access the $GLOBALS['base_url'] value, you will need to apply a couple additional patches to get them to recognize and respect the CDN settings. Since both the ImageCache images and the SWFTools uploads end up in the files directory, our patch makes file_base_url the override for those files with cdn_base_url as the default.

You can download our patch against ImageCache (6.x-2.x) below or by clicking here.
Download our patch against SWFTools (6.x-2.x) below or by clicking here.

With these patches (and the core patch above) your Drupal site should now be able to leverage a CDN for caching of all public files, all ImageCache-served images, all SWFTools-uploaded/displayed flash videos, all CSS, and all JavaScript. From a performance perspective, this will help offload a significant chunk of the static content on any given site to another host, freeing up bandwidth on the server and distributing the request load between the server and CDN. In short, performance will be improved and the site should load faster for end users.

Employing a CDN is just one piece of the overall performance and optimization puzzle, but it's a good step in the right direction. As seen above, the effort required is far from superhuman and the return on your investment is definitely worth the work.

For more than a decade, Achieve has built some of the most dynamic and expansive Enterprise Drupal websites on the market today for organizations such as Disney, NBC Universal, Viacom, FastCompany, Bella Pictures, and LifetimeTV. Hiring only the best and brightest talent in the industry, Achieve’s team of Drupal experts provide value and innovation, while always delivering quality projects on time and within budget. For more information on Achieve and its team of in-house developers who have more than 30,000 hours of architecting, developing, and deploying Drupal websites, visit www.AchieveInternet.com or call 877-869-8080.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options