简体   繁体   中英

Rails asset_host, cloudfront and heroku

I'm running a rails 4.0 app on heroku and for the life of me I can't get my asset urls using the host I've set in asset_host.

I believe my cloudfront setup is good because I can substitute in my cloudfront url for any of my asset urls and the file is picked up from heroku and cached on cloudfront.

So https://xxxxxxxxxxxx.cloudfront.net/assets/application-xxxxxxxx.js is caching https://myapp.com/assets/application-xxxxxxxxx.js correctly.

The issue seems to be my assets helper, such as javascript_include_tag, are never using the asset_host setting in staging.rb.

All I see when I load my page is all my js and css files being served from https://myapp.com/assets/

My staging setup looks like this:

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = true

  # Compress JavaScripts and CSS
  config.assets.compress = true

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = true

  # Generate digests for assets URLs
  config.assets.digest = true
  #config.assets.digest = false

  #config.assets.initialize_on_precompile = true

  config.static_cache_control = "public, max-age=31536000"

  # user Amazon Cloudfront for asset hosting
  config.action_controller.asset_host = "https://xxxxxxxxxxxx.cloudfront.net"

Is there a magic combination of config settings that has somehow eluded me?

Ok, finally figured out what was going on - my app was using the rails_api gem, which removes a whole bunch of middleware from the standard rails stack.

Changing my application controller to class ApplicationController < ActionController::Base added back in the required middleware, and asset_host started working immediately. I'll investigate further to determine which middleware it is later and decide whether I want to go back to rails_api.

One more issue I then discovered was that rack-mini-profiler rewrites cache headers to always revalidate, thus negating most of the benefit of the cdn. I've now disabled it in staging and prod and my app is running a lot more snappily!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM