本文介绍了Rails中的HTTP压缩不适用于JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有rails 3.2.1应用程序和nginx.

I have rails 3.2.1 application and nginx.

在nginx配置中,我设置为gzip on;,然后对页面,css,js文件进行压缩.

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

但是它不适用于JSON响应.我发现Rails的解决方案是在 .rb中添加config.middleware.use Rack::Deflater.

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.

它有帮助:在响应之前为45Kb,现在已接近8Kb.

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

但是,现在我发现压缩仅在Mac Chrome,Mac Firefox和Windows Chrome中有效.

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

对于IE 10,IE 11和Windows Firefox,它不起作用:

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

  • 我在请求标题中看到了Accept-Encoding: gzip, deflate
  • 我在响应标题中看不到Content-Encoding: gzip
  • 响应大小仍为45Kb.
  • I see Accept-Encoding: gzip, deflate in request-header,
  • I don't see Content-Encoding: gzip in response-header,
  • response size is still 45Kb.

请帮助.

推荐答案

已通过将压缩配置从rails移到nginx config来解决.我添加到了<my_site>.conf:

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;

感谢@Alexey Ten的帮助.

Thanks @Alexey Ten for help.

它可以工作,但是压缩在IE中不可见.Windows上的某些安全程序会捕获压缩"的HTTP响应,从存档中提取该HTTP响应,检查是否有病毒,还从响应的标头中删除Content-Encoding: gzip. IE照常表现出色:)

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

这篇关于Rails中的HTTP压缩不适用于JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:57