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

Bin*_*gic 14 ruby gzip rack ruby-on-rails

我目前有以下内容:

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
Run Code Online (Sandbox Code Playgroud)

我可能错了,但将Deflater移到顶端是没有意义的吗?这样任何和所有流量都被gzip压缩.

谢谢您的帮助.

Par*_*ert 26

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

require ::File.expand_path('../config/environment',  __FILE__)
use Rack::Deflater
run My::Application
Run Code Online (Sandbox Code Playgroud)

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

curl -i --head "Accept-Encoding: gzip,deflate" http://localhost:5000
Run Code Online (Sandbox Code Playgroud)

哪个应该返回标题:

Vary: Accept-Encoding
Content-Encoding: gzip
Run Code Online (Sandbox Code Playgroud)

还有一个精美的反应.

  • 使用选项"-I"(而不是"-i")使curl仅返回标题. (7认同)
  • 以上是资本我不是小写L. (3认同)
  • 这是一个糟糕的主意,因为它会GZip你所有的资产,包括你[你不想做]的图像文件(https://developers.google.com/speed/docs/best-practices/payload# GzipCompression).https://gist.github.com/2152663是首选. (3认同)
  • 这个答案有用的信息(+1),但实际上并没有回答问题(-1),这与在config.ru中的位置*有关,以及该行是否应该接近顶部.如果你把它添加到一个非常好的答案中,我很乐意提升.(我意识到这个问题原本可能有所不同,这可能会直接以原始形式回答.但是,更新你的答案以匹配它会很有用.) (2认同)

Pet*_*ich 6

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

# production.rb
config.middleware.insert_before ActionDispatch::Static, Rack::Deflater
Run Code Online (Sandbox Code Playgroud)

您可以使用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
Run Code Online (Sandbox Code Playgroud)