Using Azure CDN with Sitecore 7

Today I'm going to talk about the CDN service provided by Microsoft Azure. If you have a website there you will find this service very useful and you can implement it super fast. A Content Delivery Network (CDN) works by providing alternative server nodes for users to download resources (usually static content like images and JavaScript). These nodes spread throughout the world, therefore being geographically closer to your users, ensuring a faster response and download time of content due to reduced latency.

Microsoft provide a CDN service on the cloud. This service needs to take the static files from a local folder on the same website. I'm going to show you how to add to Sitecore the feature to clone all the files of the Media Library into this folder. This action is going to happen just when you publish those items from the Master database to Web database. Also, you will have to accomplish some extra configurations on the Content Delivery servers to take them from the CDN servers.

I'm going to show you and I'm going to provide you a package to use Azure CDN with your Sitecore 7.* project hosted in Azure.

This package is already on the Sitecore Marketplace. Also, the source code is on Github too.

Step 1 - Create the CDN service on Azure.

Got to the App Services section. Press on the CDN option. Then press on the Quick Create button. On the Origin Domain dropdown select the URL of your website. (Note: CDN is going to search the images on a CDN folder in the root of your website). After the creation of the CDN service, I'll recommend to enable HTTPS on it.

Step 2 - Install the package and config it

You have to install the package on the site that you have selected on the dropdown when you create the Azure CDN service.

After the installation, you will see a new file in the folder /App_Config/Includes. The file is called SitecoreFromArg.CdnAzure.config and it has 2 parameters inside:

Enabled: This parameter allow you to disable this feature.

SourceDomain: You can use this parameter to change the name of the default folder where Azure is looking for the static files.

Step 3 - Republish media items

Go to Content Editor on the server where you have installed the package, and republish all the items of the Media Library folder.

Final step - Change the configurations on the Content Delivery servers

Set the parameter Media.AlwaysIncludeServerUrl in true:
<setting name="Media.AlwaysIncludeServerUrl" value="true" />

Add the following string in the parameter Media.MediaLinkPrefix:
<setting name="Media.MediaLinkPrefix" value="~/"/>

Add the CDN Domain on the parameter Media.MediaLinkServerUrl (this parameter isn't on Sitecore 6.*):
<setting name="Media.MediaLinkServerUrl" value=""/>

Finally, remove the attribute value="ashx" from the parameter Media.RequestExtension:
<setting name="Media.RequestExtension" />

There is the code of my package