In many of my blog posts, I have short, less-than-5-minute demo videos that I have recorded with Jing (a Techsmith product). These videos can range anywhere in size from 1 MB to 30 MB depending on the amount of movement recorded in the video (the more movement, the larger the file size). I've never liked having to stream these videos directly from my server - not only does it tie up one of the few parallel requests that a browser will make to the same domain, it also puts unnecessary load on the server itself. Since the URLs for these video are embedded in database-driven content, I figured there wasn't much that I could do about it; but then, over the weekend, I realized that I could use Mod-Rewrite to forward these "static" requests onto an Amazon S3 server.
When I upload a Jing video to my server using Jing's embedded FTP button, the URL for the uploaded video gets hard-coded to the "http://www.bennadel.com" domain. As such, I figured the only way to stop streaming from bennadel.com would be to go through all the blog posts and actually change the stored content - a task I was feeling very apathetic about.
I realized, however, that I could achieve a 90% solution by trapping those incoming SWF file requests and returning an HTTP-302 redirect header to Amazon S3. This way, the browser still has to make the initial requests to my server; but, at least it is very quickly forwarded onto another domain that doesn't take up the limited number of parallel requests.
To do this, I added a simple redirect rule to my server's mod-rewrite file. I use Apache mod_rewrite locally and IIS Mod-Rewrite in production:
# If the user is requesting a JING video, forward them to # S3 - no need to tie up the bandwidth on the local server. RewriteRule (resources/jing/[\d_-]+\.swf)$ http://blog.bennadel.com.s3.amazonaws.com/$1 [R,L]
This simply catches SWF files located in the Jing folder and redirects the request to the same file hosted on Amazon S3. In this rewrite rule, the [R] stands for "Redirect". The [L] stands for "Last." When you are performing URL rewrites, the updated URLs continue to fall through to the subsequent rewrites unless the [L] flag is used.
Amazon S3 is a pretty awesome service. Content delivery networks, in general, are a very powerful tool in the journey towards performant web sites. I am only just beginning to think in terms of the benefits of distributed delivery systems. If anyone has any tips or tricks, I'd love to hear them!
Want to use code from this post? Check out the license.