Apache CDN Setup with mod_cdn
Various CDN integration methods are available to users depending on which CMS or framework is used. However, if you are an Apache user, you can integrate a CDN directly into your web server with a module called mod_cdn
. Once installed and configured, this module allows you to define which asset types you want to accelerate by rewriting the origin URL with a CDN URL. You can also get quite granular regarding which directory and/or assets you would like to accelerate. This is because the configuration is specified within your Apache's configuration file, therefore providing lots of flexibility.
Configuring Apache CDN with mod_cdn
The following section outlines the steps required to install and configure the Apache mod_cdn
module in order to setup an Apache CDN combination.
Ensure that you have the following libraries installed on your server. Otherwise, the
mod_cdn
module may not work as expected.sudo apt-get install libxml2-dev libapr1-dev apache2-dev libssl-dev
From your CLI, download the
mod_cdn
module with the following snippet and change into themod_cdn-1.1.0
directory once the file has been unzipped.wget http://agile.internap.com/assets/mod_cdn-1.1.0.tar.gz
Compile the
mod_cdn
from source against Apache 2.2.7 or higher.Copy
mod_cdn.so
to the following directory/usr/lib/apache2/modules/
. Additionally, ensure that the cdn.load file contains the following:LoadFile /usr/lib/libxml2.so.2 LoadFile /usr/lib/libssl.so.0.9.8 LoadModule cdn_module /usr/lib/apache2/modules/mod_cdn.so
Now, we need to copy the
cdn.load
andcdn.conf
files into the/etc/apache2/mods-available/
directory with the following command.cd /etc/apache2/mods-enabled cp cdn.load /etc/apache2/mods-available/ cp cdn.conf /etc/apache2/mods-available/
Then, link them from
mods-enabled
with the following.ln -s ../mods-available/cdn.conf cdn.conf ln -s ../mods-available/cdn.load cdn.load
A variety of Apache directives can be used within your configuration file to properly deliver the desired static assets from the CDN instead of the origin server. You must define the following snippet within the
<VirtualHost>
if you want to rewrite all of your site's static assets to use the CDN URL. However, you can also specifically define a set of directives for a particular directory if you do not want to accelerate the entire website.<IfModule mod_cdn.c> CDNHTMLDocType XHTML CDNHTMLToServer https://cdn.yourwebsite.com CDNHTMLFromServers https://yourwebsite.com CDNHTMLRemapURLServer \.png$ i CDNHTMLRemapURLServer \.jpg$ i CDNHTMLRemapURLServer \.gif$ i CDNHTMLRemapURLServer \.css$ i CDNHTMLRemapURLServer \.js$ i CDNHTMLLinks img src CDNHTMLLinks link href CDNHTMLLinks object data CDNHTMLLinks input src CDNHTMLLinks script src CDNHTMLLinks a href </IfModule>
The snippet above enables the Apache CDN configuration by telling the server which extensions should be replaced. The CDNHTMLToServer
directive corresponds to your CDN's Zone URL (e.g. example-hexid.kxcdn.com
) or Zone Alias (e.g. cdn.yourwebsite.com
). The CDNHTMLFromServers
corresponds to your origin domain. The CDNHTMLRemapURLServer
directive defines the extensions for which you want to accelerate. Finally, the CDNHTMLLinks
directive defines the tag/attribute pair for which the module expects to find links to content in order to replace them.
Once you have defined the snippet from step 5 in your Apache's configuration file, save your changes and restart Apache with the following command service apache2 restart
. Once Apache has restarted, go to your website and view the page source to ensure that the static assets you have defined are being properly rewritten to reflect the CDN URL.