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

时间:2011-08-29 22:08:12

标签: ruby-on-rails ruby gzip rack

我目前有以下内容:

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

感谢您的帮助。

3 个答案:

答案 0 :(得分:26)

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

还有一个精美的回应。

答案 1 :(得分:6)

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

答案 2 :(得分:1)

作为对Maletor的回应,有关如何排除某些文件的gzip,请参阅:

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

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