【问题标题】:Rails asset_host, cloudfront and herokuRails assets_host、cloudfront 和 heroku
【发布时间】:2014-04-29 13:33:40
【问题描述】:

我在 heroku 上运行一个 rails 4.0 应用程序,在我的一生中,我无法使用我在 assets_host 中设置的主机获取我的资产 URL。

我相信我的云端设置很好,因为我可以用我的云端 url 替换我的任何资产 url,并且文件从 heroku 中提取并缓存在云端。

所以https://xxxxxxxxxxxx.cloudfront.net/assets/application-xxxxxxxx.js 正在正确缓存https://myapp.com/assets/application-xxxxxxxxx.js

问题似乎是我的资产助手(例如 javascript_include_tag)从未使用 staging.rb 中的资产主机设置。

我在加载页面时看到的只是我的所有 js 和 css 文件都来自 https://myapp.com/assets/

我的暂存设置如下所示:

  # 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"

是否有某种神奇的配置设置组合让我不知所措?

【问题讨论】:

  • 您是否尝试过从asset_host 值中删除https://
  • 感谢 Rich 的建议,但我已经发现了问题并在下面进行了描述。
  • 很好 - 很高兴你修复了它!我为你投票:)
  • 我也建议不要使用staging.rb devcenter.heroku.com/articles/…
  • 感谢@Schneems 的提示 - 这听起来很明智且易于实施。

标签: ruby-on-rails heroku assets amazon-cloudfront


【解决方案1】:

好的,终于搞明白了——我的应用程序使用的是 rails_api gem,它从标准 rails 堆栈中删除了一大堆中间件。

将我的应用程序控制器更改为类 ApplicationController

我后来发现的另一个问题是 rack-mini-profiler 会重写缓存标头以始终重新验证,从而否定了 cdn 的大部分好处。我现在在 staging 和 prod 中禁用了它,我的应用程序运行得更快了!

【讨论】:

    猜你喜欢
    • 2016-02-18
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 2015-06-20
    • 2016-10-05
    • 2012-01-31
    相关资源
    最近更新 更多