So that CDN is not performing as well as you had hoped? The issue could be how your Apache server is configured. Incorrect headers, expires, and compression settings can impact Content Delivery Network (CDN) performance. Here are a few Apache directives that need to be configured to get the best performance from your CDN.
CDN Performance
In most cases, the web site performance benefits of using a CDN come from offloading HTTP requests from your primary server to a more scalable hosting infrastructure. If you noticed, I mentioned nothing about multiple geographic locations. While CDNs do put your data closer to your end user. I rarely see this as the primary benefit. With most of the sites we work on at rackAID, the primary audience is US-based. In this case, where in the US the data is cached is less important than simply offloading HTTP requests. I’ve seen sites where as much as 95% of all HTTP requests required to a load a page were for static content, such as images, css, and js files. By moving this static content to a CDN, you can significantly reduce the HTTP requests coming into your web server and dramatically improve performance.
HTTP Headers
With “origin pull” CDN deployments, the CDN fetches data from your primary server. We prefer this approach since you don’t have to separately upload files to your CDN. The CDN relies on header data sent by your Apache server to determine how to cache the file being requested. If your server sends the wrong headers, the performance of your CDN deployment can suffer.
While there are many tools to check HTTP headers, one of my favorites is RexSwain’s HTTP Viewer. This tool will spit out a detailed header which is key to assuring you have Apache configured correctly to get the most from your CDN.
Apache Configuration
There are several directives in Apache than can be used to control how a CDN processes data. There are 3 headers that I want to focus on. Sure there are others but these are the most critical ones to get right:
- Expires Header
- Cache Control
- Vary Header
Expires Header
The Expires header tells the client (in this case the user’s web browser and CDN) how long to cache a file before it needs to be requested again. To use this setting you must have mod_expires loaded into Apache.
For content that does not change often, say your logo. I recommend using far future expires header. You can set a header to expire a year or even years from now. If you ever have to update that element, then just change the name of the file. If you put version numbers as part of your file names, it makes this easy. A sample expires configuration for Apache may look like this:
ExpiresActive On
ExpiresDefault “access plus 300 seconds”
<directory "="" <span="" class="hiddenSpellError" pre="">myProject/webResources">
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
ExpiresByType text/html “access plus 1 day”
ExpiresByType text/css “access plus 1 week”
ExpiresByType text/javascript “access plus 1 day”
ExpiresByType image/gif “access plus 1 year”
ExpiresByType image/jpg “access plus 1 day”
ExpiresByType image/png “access plus 1 day”
ExpiresByType application/x-shockwave-flash “access plus 1 day”
Here you can see setting explicit expires dates based on access type. The expiration date is based on the time of access plus one year. This setting will add an expires header to your HTTP request. If you use the HTTP header viewer, you should see a couple of lines:
Cache-Control:·max-age=3600(CR)(LF)
Expires:·Fri,·19·Aug·2011·15:35:33·GMT(CR)(LF)
If you do not see these lines or the dates to not matched the expected time, then you have an issue with your Expires Headers. You will need to inspect your Apache configuration and be on the look out for .htaccess files that may be overriding your settings. Without an appropriate expires settings, the CDN will not know how long to cache your file. It could use some default or simply request the file every time it gets a request. In either case, the CDN performance will suffer.
More to Come
I will discuss the other 2 headers in follow-up posts next week. If you have any questions on Expires headers and CDNs, just let me know.