I see that CloudFront supports dynamic content.
Could anyone guide how I could go about doing this in Rails? For instance, I have, api.test.com/popular.json which I would like to be served from CloudFront preferably, but probably update itself every few hours.
For static assets, I have been able to do it using Rails asset_host setting. But I have not been able to find enough guides on dynamic content. Any pointers would be useful.
If you are serving dynamic content such as web applications or APIs directly from an Amazon Elastic Load Balancer (ELB) or Amazon EC2 instances to end users on the internet, you can improve the performance, availability, and security of your content by using Amazon CloudFront as your content delivery network.
CloudFront uses Edge Locations to cache content while Global Accelerator uses Edge Locations to find an optimal pathway to the nearest regional endpoint. CloudFront is designed to handle HTTP protocol meanwhile Global Accelerator is best used for both HTTP and non-HTTP protocols such as TCP and UDP.
CloudFront always caches responses to GET and HEAD requests. You can also configure CloudFront to cache responses to OPTIONS requests. CloudFront does not cache responses to requests that use the other methods.
You don't need to integrate your rails app with CloudFront. Instead, you can set up CloudFront to cache your website:
myexistingdomain.com
You can leave all defaults as they are. Minimum/Default/Maximum TTL options can be set to enforce a certain cache time, and might be necessary depending on your server settings.
myexistingdomain.com
, you must set Access-Control-Allow-Origin
header to include the domain name of your Cloudfront distribution.As you probably already have experienced, most updates on CloudFront, or adding an origin usually takes ~20 minutes, so be patient.
Also, if you need to use query parameters, cookies or special headers at your rails app, you'll have to include those in cache settings at your CloudFront distribution. Note that this will reduce the efficiency of the distribution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With