Ruby on Rails CDN Integration
This guide shows how to complete a Ruby on Rails (RoR) CDN integration.
- Create a Pull Zone or a Push Zone before you start with the RoR CDN integration.
To set your asset host in Rails, you need to change config.action_controller.asset_host in config/production.rb
config.action_controller.asset_host = 'yourzone-ab12.kxcdn.com'
There’s no need to specify the protocol (http:// or https://) in the config file. It’s also possible to work with CNAMEs (e.g. cdn.yourdomain.com). In that case a Zone Alias needs to be created as well.
You can also set this value through an environment variable in order to make running a staging copy of your site easier.
config.action_controller.asset_host = ENV['CDN_HOST']
For this to work, CDN_HOST needs to be set on your server (pointing to your Zone e.g. yourzone-ab12.kxcdn.com).
Once you have configured your server. Content can be delivered from KeyCDN like this:
<%= asset_path('file.png') %>
Instead of returning a path such as /assets/file.png. The URL generated will have the full path to your KeyCDN Zone such as
http://yourzone-ab12.kxcdn.com/assets/smile.png
.
That’s it! Your Ruby on Rails CDN integration is completed. Let us know if you have questions or issues.