Avoid dynamic HTML pages on Amazon CloudFront CDN

amazon-cloudfrontcdn

I am thinking of using CloudFront distribution as CDN to server my images, css and js. I have a CloudFront distribution setup to point to the same directory in the server where my code is, just changing the subdomain to cdn so it'd be cdn.domain.com.

My problem is, this CDN subdomain would sever the HTML and php links too which I don't want it to do. I just want to use Cloudfront images, css and js.

Can you please point me to proper ways to do this? Is there a way to do this within the cloudfront panel?

I think we can server 404 page from htaccess once the the call gets to origin server but I am looking for a better option that would actually prevent the request from reaching the origin server.

Best Answer

CloudFront is great for serving dynamic html. It's faster for users (especially distant users) even if the page isn't cached as it uses Amazon's backbone instead of the public internet, which is optimised for this job. It protects your server against layer 4 (TCP) and layer 7 (http) attacks. Using a single domain is faster if you use http/2, which CloudFront supports, as http/2 lets you send resources that are required to render the page even before the client knows it needs them.

Set your TTL to 0 for HTML, and obviously higher for static resources.

Have a look at something like this Re:Invent session for more information, or the excellent documentation.