Using CDN with Rails assets

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:



One thought on “Using CDN with Rails assets”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>