Right now I'm working on an application which is experiencing lots of interest - and therefore lots of load! We needed to look at ways of trying to bring down the pressure on the servers, and decided to use a CDN (Content Delivery Network) for our image files. It was surprisingly painless to implement once I got into it so here it is in case it's helpful.
The way it works is that you point your image URLs to Amazon, and Amazon knows how to map those onto the images which continue to live where they are on your webserver and to be accessible by their own URLs. If Amazon gets a request for an image it doesn't have, it grabs it from your server and serves it - but then caches them all so the next request (or 10,000 requests!) will hit Amazon's servers and not yours. It also helps a bit because the domain is different and there is a limit on how many parallel requests browsers will make to any given domain, so you get more parallel requests on your page too.
Configurable Image Paths
I was able to switch out my image paths very easily because all images in the system had a path like $config['productImagePath']
- mostly this can help on dev platforms if they're not all the same, but also it helps if you need to move your images at a later date, as I had known could happen (and it did!). If this isn't already in place, you need to go find every image and make its URL configurable.
Set up Amazon Cloud Front
Head over to your Amazon Web Services console https://console.aws.amazon.com (it'll prompt you to sign up or log in as appropriate) and look for "Amazon CloudFront" in the "Storage" section.
Click the "create distribution" button and configure a few things:
- Delivery method: web
- Origin domain name:
[domain where the images are served from right now]
- Price class: choose which regions you need your images served from, it affects the cost
Then you need to wait for a few minutes while the cogs turn and it all gets created.
Testing Your Setup
You will get a URL for your CloudFront that looks like http://uttergobbledegook.cloudfront.net
and you use that in place of the source domain you set earlier. So if your images were at http://mygreatsite.com/product_images/whisk.jpg
now you should be able to request http://uttergobbledegook.cloudfront.net/product_images/whisk.jpg
in your browser. Once it's cached, the response time should be very quick too.
Now go back and where your configuration settings once pointed to a URL on your own site, they will now point to the path under the CloudFront domain ... you probably need different distributions for your various staging/live platforms, and these can be enabled and disabled differently.
I found this was a very painless way of getting a CDN into place - no changes to the development platforms, minor config changes to the staging/live platforms, and the images stay in the same place that they always lived!
Whatever Next?
There are actually a few things you can do from here:
- Use a
cname
alias to make a nicer URL such ashttp://static.mygreatsite.com
- Tweak the caching rules to make sure they make sense for your application, they are very VERY configurable
- If the images you're serving are assets rather than upload files, consider deploying them straight to S3 and setting up your CloudFront to server from there
Did this work for you? What did I miss? And did Amazon update their interface since I wrote this post (this happens often). Please leave a comment :)
Lorna is an independent web development consultant, author and trainer, available for work (interesting projects only). This post was originally published at LornaJane
more
{ 0 comments... » Quickly add Amazon Cloudfront as a CDN read them below or add one }
Post a Comment