Ruby on Rails CDN Integration

This guide shows how to complete a Ruby on Rails (RoR) CDN integration.

  1. Create a pull zone or a push zone before you start with the RoR CDN integration.
  2. 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 Zonealias needs to be created as well.

  3. 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).

  4. Once you have configured your server. Content can be delivered from KeyCDN like this:
  5. <%= 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.

Leave A Comment?