Where to insert Rack::Deflater in the rack?

邮差的信 提交于 2020-01-11 23:07:13

问题


I currently have the following:

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

I may be wrong, but wouldn't it make sense to move Deflater to the top? This way any and all traffic is gzipped.

Thanks for the help.


回答1:


The simplest way to insert it is directly in your config.ru:

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

To confirm it is working start up your app and hit it with curl:

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

Which should return the headers:

Vary: Accept-Encoding
Content-Encoding: gzip

And a beautifully gzipped response.




回答2:


I had to insert it pretty early (before ActionDispatch::Static), like this:

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

You can use rake middleware to confirm (although this will look at your development settings)

> 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:


In response to Maletor, for how to exclude certain files from being gzip'd, see:

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

tried it (in Sinatra) and works great.



来源:https://stackoverflow.com/questions/7236583/where-to-insert-rackdeflater-in-the-rack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!