Search code examples
ruby-on-railsrubyhttpnginxhttp-compression

HTTP-Compression in rails not working for JSON-responses


I have rails 3.2.1 application and nginx.

In nginx configurations I set gzip on;, and compressing works for pages, css, js files.

But it does not work for JSON responses. As I found the solution for rails is to add: config.middleware.use Rack::Deflater into application.rb.

And it helps: before response was 45Kb, now near 8Kb.

But, now I found that compression works only in Mac Chrome, Mac Firefox and Windows Chrome.

For IE 10, IE 11 and Windows Firefox - it does not work:

  • I see Accept-Encoding: gzip, deflate in request-header,
  • I don't see Content-Encoding: gzip in response-header,
  • response size is still 45Kb.

Please, help.


Solution

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