I want to use Cloudfront as a CDN for my CSS, JS, images etc... I've set up a distribution on AWS with an origin domain name as mywebsite.com. The distribution has an alternate domain name as cdn.mywebsite.com. If I user accesses cdn.mywebsite.com they are served the whole website. I don't want this to be possible. My CNAME is set-up to point to the Cloudfront distribution (i.e. ***.cloudfront.net).

Is there a way of stopping this behaviour?

1 Answers

cpt_guy On Best Solutions

I think you're mixing up 2 different ways of using a CDN in your setup.

  1. If you had all your images for example hosted in an S3 bucket, you would point your CDN to that bucket, call it cdn.images.com. Then on your website you would reference all your images as cdn.mywebsite.com/potato.jpeg. The CDN would then store your images on your the edge locations. Your website's dns record (www.mywebsite.com) still points directly to your server - not to your CDN, but your images are pulled from your CDN.

  2. The other option is if all your content, website, everything is on one server - and you're pointing your CDN to that server. So your website is www.mywebsite.com, and because you only want to store jpegs on the CDN and everything else must go back to the origin, you set that up in your caching. Your DNS record for www.mywebsite.com gets pointed to your CDN

You seem be expecting to do option one, but your setup is more like option 2. Since it seems like all your content is stored on your digital ocean server - I would just have a URL for www.mywebsite.com and point that to your digital ocean server. Your cache behaviour in the CDN config will then just have *.jpeg, *.css, *.etc...