15

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.

John Bachir
  • 22,495
  • 29
  • 154
  • 227
Binary Logic
  • 2,562
  • 7
  • 31
  • 39

3 Answers3

26

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.

iconoclast
  • 21,213
  • 15
  • 102
  • 138
Parker Selbert
  • 1,546
  • 13
  • 13
  • 7
    Use option "-I" (as opposed to "-i") to have curl return only headers. – mshafrir Nov 18 '11 at 21:23
  • 3
    The above is a capital i not a lowercase L. – Chris Bui Aug 14 '12 at 23:38
  • 3
    This is a bad idea because it will GZip all of your assets, including your image files, which you [don't want to do](https://developers.google.com/speed/docs/best-practices/payload#GzipCompression). https://gist.github.com/2152663 is preferable. – maletor Sep 09 '12 at 06:36
  • @Maletor All of your assets, including image files would preferably be served by a frontend server, such as Nginx, right? Then this would not be an issue. – mlangenberg Jun 15 '13 at 12:55
  • True, but not if you are on a platform like Heroku where you use `ActionDispatch::Static`. – maletor Jun 15 '13 at 17:49
  • 2
    This answer has useful info (+1) but doesn't actually answer the question asked (-1), which has to do with placement *within* the config.ru, and whether that line should go near the top or not. I'd gladly upvote if you add that to an otherwise excellent answer. (I realize the question may have originally been different, and this might have directly answered it in its original form. Nonetheless, updating your answer to match it would be useful.) – iconoclast Feb 11 '14 at 20:55
  • Proper command is now `curl -I -H 'Accept-Encoding: gzip,deflate' http://localhost:5000` – bcackerman Mar 04 '14 at 15:24
  • I doubt whether it really answer the question. – Weihang Jian Oct 30 '21 at 02:22
5

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
Peter Ehrlich
  • 6,969
  • 4
  • 49
  • 65
  • 1
    Still though, images should not be Gzip'ed [as defined by Google PageSpeed](https://developers.google.com/speed/docs/best-practices/payload#GzipCompression). Any method to skip Deflator on those assets? – maletor Nov 15 '12 at 00:15
  • 1
    Just a hint here: this wont work unless you have config.serve_static_assets = true ;) – santuxus Sep 02 '13 at 14:38
  • No, you don't want to compress most of binary files like PDF, JPEG, PNG, etc. They are already compressed so you would gain little benefit by compressing them again. – Weihang Jian Oct 30 '21 at 02:24
1

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.

Damon Mannion
  • 324
  • 2
  • 3