Rails CDN integration in 2025

Why Use CDN in Ruby on Rails?

Ruby on Rails is a powerful, open-source web development framework written in Ruby. It emphasizes simplicity and speed for developers, making it a popular choice for building scalable web applications. By using a CDN with Ruby on Rails, you can improve site performance by distributing static resources like images, JavaScript, and CSS files across multiple global servers. This reduces the load on your server, decreases latency, and accelerates content delivery. Implementing a CDN with Ruby on Rails ensures faster website loading times, enhanced reliability, and better user experience for a global audience.

Before you start

  • Before you take any steps please back up your files and database.
  • In the following, we will integrate a CDN service using the CDN domain cdn.mycompany.com Please visit the Services/How-To section to obtain your CDN domain.
  • To integrate a CDN service on https:// website the CDN service must have SSL enabled.
  • If your website embeds custom fonts then please first enable CORS for them.
  • Before you take any steps please make sure that your CDN domain is ready-to-use here.

Create CDN Static service

Please refer to Creating a CDN Static service for more details.

Enable CDN in Ruby on Rails

Define CDN in config

Add the following to the Rails's config/environments/production.rb file.

config.action_controller.asset_host = 'https://cdn.mycompany.com'

Use the asset_url helper in views

Use the asset_url helper to reference your assets.

<%= asset_url('image.jpeg') %>

will output

https://cdn.mycompany.com/image.jpeg

Precompile Assets for Production

In production, make sure to precompile your assets by running:

rake assets:precompile

Configuring Different CDN Domains for Different Environments

If you need to use a different CDN domain for other environments (like staging), you can conditionally set the asset host in config/environments/production.rb or other environment files:

config.action_controller.asset_host = Rails.env.production? ? 'https://cdn.mycompany.com' : 'https://staging-cdn.mycompany.com'

Notes

  • View HTML source code of your web pages to verify that you are using CDN, you should see source attribute of your images, CSS, JavaScript, etc. beginning with your CDN domain.
  • Don't see your CDN domain in the source code of your web pages? If your website is using any cache plug-in/mechanism then you might want to clear/flush its cache.
  • Having trouble with custom fonts? Please refer to Using custom fonts with CDN - setting CORS for more details.
  • Still having trouble? Check your CDN URLs using our content check tool or please refer to Debugging a CDN service for more hints.
  • Can you improve this guide or write a new one for software we haven't covered? Help us expand our CDN integrations section and earn $50 in CDN credit! Contact us to contribute.

Contact Us

  _  _     ______    ______  
 | \| ||  /_   _//  /_____// 
 |  ' ||   -| ||-   `____ `  
 | .  ||   _| ||_   /___//   
 |_|\_||  /_____//  `__ `    
 `-` -`   `-----`   /_//     
                    `-`