Compress a JSON response from a Rails application

We have an application that requests location for a client. We get to the point where some customers can have more than 10,000 seats. The JSON response for this can be quite large, sometimes more than 1 MB.

I first wonder how best to squeeze it. We have apache in front of the Rails app running in trinidad with JRuby. Can I just set mod_deflate to always compress any responses that are application / json? How can i do this?

Further, what is gzip'd json browser support? When I gzip a 200k response sample, it drops to 30k. This is a significant savings. We really would like to minimize the size of this answer without minimizing the number of places returned.

+3
source share
4 answers

If the browser supports gzip'd / deblated data, then JSON will go through it just fine. AJAX data is just a normal HTTP request that was made on behalf of a script, not a person. At the HTTP level, there is absolutely zero difference between passing some HTML or a JSON string - it's just data.

+5
source

In general, for newer versions of Rails, you can do this by adding

use Rack::Deflater

before the line "run" in the config.ru file . This works great with gzip enabled browsers / clients. We use it in production on major websites.

JRuby: , Rails Rack, JRuby. JRuby-Rack Warbler "" "".

+11

... [ Apache 2.2.16 IE6]

JSON Content-Encoding = gzip , mod_deflate.conf, :

 AddOutputFilterByType DEFLATE application/json

Firefox/Firebug/Net

+1

, apache mod_deflate, .

a2enmod deflate

, apache. , .

service apache2 restart

apache2.conf /etc/apache2, . , .

Include mod_deflate.conf

, mod_deflate.conf :

SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \
    \.(?:exe|t?gz|zip|bz2|sit|rar)$ \
    no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary

#Skip browsers with known problems
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

gzipping exes, gzs, pdfs ..

, .

Finally restart apache again

service apache2 restart

The settings were copied from the link below: http://www.howtoforge.com/apache2_mod_deflate

0
source

Source: https://habr.com/ru/post/1787990/


All Articles