Using CDN with Rails assets

March 25, 2013 · Posted in Development 

Using CDN (Content Delivery Network) server to serve assets (images and other static files like css, js) on Rails is very easy with assets pipeline.


Change config in production.rb:

# serve all assets from CDN server
config.action_controller.asset_host = ''
#or if your files in a folder on CDN server
config.action_controller.asset_host = ''
If you have an image at then it will be replaced on your HTML  page with URL (or for the second example).

To use CDN server only for images (but not for stylesheets or javascripts):

# serve only images
config.action_controller.asset_host = { |source|
if source =~ /\b(.png|.jpg|.gif)\b/i

Examples on github:


For more flexible settings you can use this gem:




  • Jiří Kratochvíl

    Great! Very useful. Thank you.