How to enable compression in Ruby on Rails?
Enable compression
Add it to config/application.rb:
module YourApp
class Application < Rails::Application
config.middleware.use Rack::Deflater
end
end
Source: http://robots.thoughtbot.com/content-compression-with-rack-deflater
HTTP-Compression in rails not working for JSON-responses
Fixed by moving compressing configurations from rails to nginx configs.
I added to <my_site>.conf
:
# Enable Gzip
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_proxied any;
gzip_types
# text/html is always compressed by HttpGzipModule
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/json
application/xml
application/rss+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;
gzip_static on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
Thanks @Alexey Ten for help.
It works, but compressing are not visible in IE.
Some security programs on Windows catch "gzipped" HTTP-responses, extract it from archive, check for viruses and also remove Content-Encoding: gzip
from header of response. IE as usual excelled :)
How enable gzip in puma server
rack deflater will work with puma
add use Rack::Deflater
to your config.ru file
Take a look at this question/answer: Where to insert Rack::Deflater in the rack?
How to enable output compression (gzip) on everything that goes out of my Ruby on rails on Mongrel/WEBrick Server?
You should be able to do this using the Mongrel GZipFilter.
rails json response with gzip compression
For the response to be in gzip format we don't have to change the render
method call.
If the request has the header Accept-Encoding: gzip
, Rails will automatically compress the JSON response using gzip.
If you don't want the user to send a request with preset header., you can add the header to the request manually in the controller before rendering the response:
request.env['HTTP_ACCEPT_ENCODING'] = 'gzip'
render :json => response.to_json()
How to turn on GZIP compression on ruby THIN server (Rails 3.1) on windows
This tutorial will help you set up Nginx and Thin:http://www.funonrails.com/2010/03/nginx-and-thin-installation-and.html
And from what I understand, you can turn on gzip through Nginx configuration file. This shows the Nginx HttpGzipModule: http://wiki.nginx.org/HttpGzipModule
gzip compression of assets on heroku doesn't work with custom domain
You'll want to add use Rack::Deflater
to your config.ru in order to get Heroku to serve compressed assets.
Your config.ru would look something like this:
require ::File.expand_path('../config/environment', __FILE__)
use Rack::Deflater
run Rails.application
There is a good discussion on either using Rack::Deflater
or some alternatives in order to serve compressed assets on Heroku, but it seems people keep coming back to Rack::Deflater
.
Related Topics
Does Begin . . . End While Denote a 'Block'
Has Anyone Figured Out a Way to Run the Same Cucumber Scenario on Multiple Browsers/Web Drivers
Differences Between Literals and Constructors? ([] VS Array.New and {} VS Hash.New)
How to Embed Dynamic Ruby Code to "Javascript" Section in Slim Templates
Ruby Code Beautification, Split Long Instructions on Multiple Lines
How to Embed Ruby in JavaScript (Rails + .Html.Erb File)
How to Set a Request.Referrer Inside My Rspec
How to Set the Httponly Flag on a Cookie in Ruby on Rails
How to Use the Ruby "Self" Keyword
Gem Install Mongrel Fails with Ruby 1.9.1
Document Model Attributes with Yard
Ruby Gems in Stand-Alone Ruby Scripts
Ruby on Rails User Registration Using Rest API Call and Devise