If your Sitecore website is heavy on content from the media library you can offload your Sitecore instances by allowing images to be retrieved from a Content Delivery Network (CDN). If you use Microsoft Azure, you do not need to upload images to the CDN, as Azure support origin pull.
Origin pull is a mechanism where the CDN automatically retrieves any missing content item from an origin host if the content is missing. In Azure, even parameters to the content is supported, so if you scale images with ?w=100, these parameters are supported, and the Azure CDN will store the scaled image.
To set up origin pull in Azure CDN, you first go to your CDN profile:
Then you click the + sign to add an endpoint:
And add an endpoint with the type “Custom Origin”:
The “name” is the name of the endpoint. The “Origin hostname” is the URL to your public Sitecore website. And remember to specify the correct protocol. If your website is running HTTPS, the CDN should use HTTPS as well.
SETTING UP SITECORE:
The rest is configuration in Sitecore. You control the CDN properties using these settings, found in the Sitecore.config file:
<setting name="Media.MediaLinkServerUrl" value="https://myendpoint.azureedge.net" /> <setting name="Media.MediaLinkPrefix" value="-/media" /> <setting name="Media.AlwaysIncludeServerUrl" value="true" /> <setting name="MediaResponse.Cacheability" value="public" />
- Media.MediaLinkServerUrl = The URL to the Azure CDN, as defined when creating the Azure Endpoint
- Media.MediaLinkPrefix = The media library link URL. Together with the Media.MediaLinkServerUrl, the complete server URL is created. In the example, my url is https://myendpoint.azureedge.net/-/media/%5Bmedia library content]
- Media.AlwaysIncludeServerUrl = Tells Sitecore to always include the server URL in the media requets
- MediaResponse.Cacheability = Allows the cache settings of any item to be publicly available, allowing the Azure CDN to access the MaxAge, SlidingExpiration and VaryHeader parameters.
DRAWBACKS OF USING A CDN:
- Your website needs to be public. When developing and testing you need to disable the CDN settings as the Azure CDN cannot read from a non-public website. Testing is therefore in production as the website runs.
- Security settings on media library items cannot be used. Once a media library item is on the CDN it is public to everyone.
MORE TO READ:
- Integrating Sitecore with your content delivery networks by Nonlinear Digital
- Integrating a CDN into your Sitecore Solution by All things sitecore
- Setting up Azure CDN to deliver your Sitecore Media by JammyKam
Thanks Brian. I’m not super familiar with Azure and especially PaaS. Did you try this with PaaS, or something else? If something else, any guess at equivalent steps for PaaS?
It’s not tested on a PaaS solution, but I would be very surprised if it did not work on PaaS. Most of the work is done by the CDN. All I do is that I instruct Sitecore to prefix every Media Library URL with a different hostname/path, and to allow the cache settings to be publicly available.
I usually use an internally available url for testing these CDN settings. It is of course not ideal as I am not testing the end CDN service, but at least I am able to test that the CDN url is set correctly on all static resources and Sitecore media resources when building and testing on test environment.