何处将Rack :: Deflater插入机架?

我目前有以下内容:

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 # use ActionController::Session::DalliStore, # 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移到顶端是没有意义的吗? 这样任何和所有流量都被gzip压缩。

谢谢您的帮助。

插入它的最简单方法是直接在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 

还有一个精美的gzipped响应。

我必须很早地插入它(在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 # 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 

为了回应Maletor,关于如何排除某些文件被gzip压缩,请参阅:

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

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