【问题标题】:Where to insert Rack::Deflater in the rack?在机架中的何处插入 Rack::Deflater?
【发布时间】:2014-02-12 04:53:23
【问题描述】:

我目前有以下:

use Rack::Rewrite
use Rack::Cache, {:verbose=>true, :metastore=>"memcached://localhost:11211/rack-cache/meta", :entitystore=>"memcached://localhost:11211/rack-cache/body"}
use Rack::Rewrite
use Rack::Lock
use Rack::Deflater
use ActionController::Failsafe
use #<Class:0x007fb34be9ac90>
use ActionController::Session::DalliStore, #<Proc:0x007fb34bea3638@(eval):8 (lambda)>
use Rails::Rack::Metal
use ActionController::ParamsParser
use Rack::MethodOverride
use Rack::Head
use ActionController::StringCoercion
use Sass::Plugin::Rack
use Hassle
use ActiveRecord::ConnectionAdapters::ConnectionManagement
use ActiveRecord::QueryCache
run ActionController::Dispatcher.new

我可能错了,但是将 Deflater 移到顶部不是很有意义吗?这样所有流量都会被压缩。

感谢您的帮助。

【问题讨论】:

    标签: ruby-on-rails ruby gzip rack


    【解决方案1】:

    插入它的最简单方法是直接在您的 config.ru 中:

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

    要确认它正在运行,请启动您的应用并使用 curl:

    curl -i --head "Accept-Encoding: gzip,deflate" http://localhost:5000
    

    应该返回标题:

    Vary: Accept-Encoding
    Content-Encoding: gzip
    

    还有精美的 gzip 响应。

    【讨论】:

    • 使用选项“-I”(而不是“-i”)让 curl 只返回标题。
    • 以上是大写的,不是小写的L。
    • 这是个坏主意,因为它会 GZip 您的所有资产,包括您don't want to do 的图像文件。 gist.github.com/2152663 更可取。
    • @Maletor 您的所有资产,包括图像文件,最好由前端服务器(例如 Nginx)提供服务,对吧?那么这将不是问题。
    • 这个答案有有用的信息 (+1) 但实际上并没有回答所提出的问题 (-1),这与 config.ru within 的位置有关,以及那条线是否应该靠近顶部。如果您将其添加到其他出色的答案中,我将很乐意投票。 (我意识到这个问题最初可能有所不同,这可能直接以其原始形式回答了它。尽管如此,更新您的答案以匹配它会很有用。)
    【解决方案2】:

    我必须很早就插入它(ActionDispatch::Static 之前),像这样:

    # production.rb
    config.middleware.insert_before ActionDispatch::Static, Rack::Deflater
    

    您可以使用rake middleware 进行确认(虽然这会查看您的开发设置)

    > 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
    

    【讨论】:

    • 尽管如此,图像不应该被 Gzip 压缩as defined by Google PageSpeed。有什么方法可以跳过这些资产的 Deflator?
    • 这里只是一个提示:除非你有 config.serve_static_assets = true ;)
    • 不,您不想压缩大多数二进制文件,如 PDF、JPEG、PNG 等。它们已经被压缩,因此再次压缩它们几乎没有什么好处。
    【解决方案3】:

    针对 Maletor,关于如何排除某些文件被 gzip 压缩,请参阅:

    http://icelab.com.au/articles/wrapping-rack-middleware-to-exclude-certain-urls-for-rails-streaming-responses/

    尝试过(在 Sinatra 中),效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-15
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 2011-12-20
      • 1970-01-01
      相关资源
      最近更新 更多