MetaCDN - StreamShark

Using the Apache mod_cdn module with MetaCDN

Overview

This integration is for advanced users only. You should be already comfortable installing and setting up Apache, as well as installing and configuring Apache modules. This guide was written for users running Apache 2 on Ubuntu 10.04 LTS Server. It may vary for other Linux distributions, or other operating systems.

Step 1: Download and install mod_cdn

From your origin server (e.g. on the machine directly or via SSH), download the mod_cdn source code:

wget http://agile.internap.com/assets/mod_cdn-1.1.0.tar.gz

Unpack the source code and change into the source directory:

tar zxvf mod_cdn-1.1.0.tar.gz

cd mod_cdn-1.1.0/

Before we can compile and install the module, we need to ensure the pre-requisite libraries are present:

sudo apt-get install libxml2-dev libapr1-dev apache2-dev libssl-dev

Now we can build and install the module:

make

sudo cp mod_cdn.so /usr/lib/apache2/modules/

We need to make a small edit to cdn.load before copying it into place. Make sure it has the following content:

LoadFile /usr/lib/libxml2.so.2

LoadFile /usr/lib/libssl.so.0.9.8

LoadModule cdn_module /usr/lib/apache2/modules/mod_cdn.so 

Step 2: Enable mod_cdn

We now need to copy mod_cdn so that it is present in the list of available Apache modules:

cd /etc/apache2/mods-enabled

sudo cp cdn.load /etc/apache2/mods-available/

sudo cp cdn.conf /etc/apache2/mods-available/

 Now that the files are in place, we have two options to activate mod_cdn. We can do it manually or via a2endmod:

Step 2a: Manually

sudo ln -s ../mods-available/cdn.conf cdn.conf

sudo ln -s ../mods-available/cdn.load cdn.load

OR

Step 2b: Via a2enmod

sudo a2enmod (and choose "cdn")

Step 3: Configure your site to use MetaCDN

Finally, we must edit our Apache site configuration, to decide which links we want rewritten to point to accelerated assets, Almost always, these directives should be applied within the <VirtualHost> section of your Apache site configuration document. These directives can be applied site wide (as per this example) or within a specific <Directory>.

Let's edit the default site configuration:

sudo vi /etc/apache2/sites-available/default

Now, let's add the following contents to the file inside the <VirtualHost> section:

<IfModule mod_cdn.c>

    CDNHTMLDocType XHTML

    CDNHTMLToServer http://mysite.sa.metacdn.net

    CDNHTMLFromServers http://www.mysite.com

    CDNHTMLRemapURLServer \.jpg$ i

    CDNHTMLRemapURLServer \.png$ i

    CDNHTMLRemapURLServer \.gif$ i

    CDNHTMLRemapURLServer \.css$ i

    CDNHTMLRemapURLServer \.js$ i

    CDNHTMLRemapURLServer \.mp4$ i

 

    CDNHTMLLinks    link            href

    CDNHTMLLinks    img             src

    CDNHTMLLinks    object          data

    CDNHTMLLinks    embed           data

    CDNHTMLLinks    input           src

    CDNHTMLLinks    script          src

    CDNHTMLLinks    a               href

</IfModule>

With the above configuration, we have done the following:

  • We are editing the Apache configuration of an origin server with a domain name www.mysite.com via the directive CDNHTMLFromServers
  • We have received an accelerated domain from the MetaCDN site accelerator with a domain name of mysite.sa.metacdn.net via the directive CDNHTMLToServer
  • The module will rewrite all HTML pages with a doctype of XHTML via the CDNHTMLDocType directive
  • We wish to rewrite the links of assets of file types jpg, png, gif, css, js, and mp4, replacing origin domain links to accelerated links within the HTML document via the CDNHTMLRemapURLServer directive
  • Within the HTML document, we are searching for tags of type link, img, object, embed, input, script and a as candidates to replace their href, src or data values, via the CDNHTMLLinks directive.
FInally, we need to restart our Apache server to pick up the new mod_cdn module and it's configuration.
 
sudo /etc/init.d/apache2 restart

Step 4: The end result

Based on our configuration in the previous step, the mod_cdn Apache module will replace certain links to content in our HTML page to accelerated MetaCDN links.

Have more questions? Submit a request