【问题标题】:How to get Gzip and Expires Header on a Rails 3.1.1 app on Heroku Cedar?如何在 Heroku Cedar 上的 Rails 3.1.1 应用程序上获取 Gzip 和 Expires Header?
【发布时间】:2011-12-09 05:01:53
【问题描述】:

我正在 Heroku Cedar 上运行 Rails 3.1.1 应用程序。默认情况下,此堆栈不 Gzip 并设置 Expires Headers on assets。 有一些关于此的文档,但不是很清楚:http://devcenter.heroku.com/articles/http-routing

谁能给我一段代码来激活它?

非常感谢

【问题讨论】:

    标签: heroku ruby-on-rails-3.1 gzip expires-header cedar


    【解决方案1】:

    Cedar 不使用 Nginx,因此您必须自己使用 Rack::Deflater 压缩资源,如下所示:

    # config.ru
    require ::File.expand_path('../config/environment',  __FILE__)
    use Rack::Deflater
    run YourApp::Application
    

    您还可以直接在应用中设置静态文件的标题:

    # config/environments/production.rb
    config.static_cache_control = "public, max-age=3600"
    

    最后,您最好设置 Rack::Cache 来替换 Varnish 缓存。请参阅this blog post 了解更多信息。

    【讨论】:

    • 这是我通过这些更改获得的资产响应标头:HTTP/1.1 304 Not Modified Cache-Control: no-cache, private Date: Mon, 17 Oct 2011 11:36:48 GMT Server: thin 1.2.11 codename Bat-Shit Crazy X-Rack-Cache: miss X-Runtime: 0.001256 X-Ua-Compatible: IE=Edge,chrome=1 Connection: keep-alive
    • 事实上,你是对的!只需要激活serve_static_assets使用config.serve_static_assets = true
    • 这是在 Heroku 开发中心讨论的地方,把我带到这里:devcenter.heroku.com/articles/http-routing 这个答案真的帮了我大忙!
    • @Camille 我想知道如何找出某个站点的过期标头是什么,例如 google.com。我怎样才能知道呢?
    【解决方案2】:

    ActionDispatch::Static之前尽早包含中间件很重要

    #production.rb
    config.middleware.insert_before ActionDispatch::Static, Rack::Deflater
    
    > rake middleware
    use Rack::Deflater
    use ActionDispatch::Static
    use Rack::Lock
    use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007f8e18455e90>
    use Rack::Runtime
    use Rack::MethodOverride
    use ActionDispatch::RequestId
    use Rails::Rack::Logger
    use ActionDispatch::ShowExceptions
    use ActionDispatch::DebugExceptions
    use ActionDispatch::RemoteIp
    use ActionDispatch::Reloader
    use ActionDispatch::Callbacks
    use ActionDispatch::Cookies
    use ActionDispatch::Session::CookieStore
    use ActionDispatch::Flash
    use ActionDispatch::ParamsParser
    use Remotipart::Middleware
    use ActionDispatch::Head
    use Rack::ConditionalGet
    use Rack::ETag
    use ActionDispatch::BestStandardsSupport
    use Warden::Manager
    use Rack::Mongoid::Middleware::IdentityMap
    use Rack::Pjax
    run MyApp::Application.routes
    

    【讨论】:

      【解决方案3】:

      无耻插件 - 我创建了一个可以压缩但避免压缩图像的 gem。

      https://github.com/romanbsd/heroku-deflater

      【讨论】:

      • 我尝试安装您的 gem,但找不到我编译的资产。这里有什么帮助吗?
      • 不知道,第一次听说这种事。
      • 哦,抱歉,这是一个错误配置的 staging.rb 文件。我的资产再次得到正确服务。但我没有在我的 yslow 报告中看到过期标题。它只是应该“工作”还是我必须进行更多配置?
      • 对于由 rails 应用程序提供的资产,您应该看到 Cache-Control 标头。如果它由 S3 或 cloudfront 提供服务,那么您应该在那里进行配置。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 2011-10-26
      • 2013-01-08
      • 2012-02-01
      相关资源
      最近更新 更多