Ruby on Rails Active Storage there is posibility to configure CDN.

Set up Rails

# config/environments/staging.rb
Rails.application.configure do
  # ...
  config.x.cdn_host = 'https://myapp-stg.b-cdn.net'
  config.active_storage.resolve_model_to_route = :cdn_proxy
  # ...
# config/environments/production.rb
Rails.application.configure do
  # ...
  config.x.cdn_host = 'https://myapp-prod.b-cdn.net'
  config.active_storage.resolve_model_to_route = :cdn_proxy
  # ...
# config/environments/test.rb
# ..
# no changes
# ..
# config/environments/development.rb
# ..
# no changes
# ..
# config/routes.rb
# ...
  direct :cdn_proxy do |model, options|
    if model.respond_to?(:signed_id)
      route_for(
        :rails_service_blob_proxy,
        model.signed_id,
        model.filename,
        options.merge(host: Rails.configuration.x.cdn_host)
      )
    else
      signed_blob_id = model.blob.signed_id
      variation_key  = model.variation.key
      filename       = model.blob.filename
      route_for(
        :rails_blob_representation_proxy,
        signed_blob_id,
        variation_key,
        filename,
        options.merge(host: Rails.configuration.x.cdn_host)
      )
    end
  end
# ...

Tested and works with Rails 6.1

Set up CDN

Point your CDN origin URL to point to root of your apps URL.

E.g. if your app is https://www.myapp.com CDN origin will point to https://www.myapp.com/

  • https://myapp-prod.b-cdn.net/rails/active_storage/representations/proxy/xxxxxx/example.jpg -> https://www.myapp.com/rails/active_storage/representations/proxy/xxxxxx/example.jpg
  • https://myapp-stg.b-cdn.net/rails/active_storage/representations/proxy/xxxxxx/example.jpg -> https://staging.myapp.com/rails/active_storage/representations/proxy/xxxxxx/example.jpg

Sources