【问题标题】:Why is the gzip minimum length directive not being respected?为什么不遵守 gzip 最小长度指令?
【发布时间】:2016-01-21 01:09:53
【问题描述】:

如果我理解正确,最好不要压缩小资源,因为它们实际上可能会变得更大,同时仍然会对 CPU 造成性能影响。 所以使用 gzip_min_length 指令是一个明显的解决方案。 但是,在运行 REST API 的服务器上尝试此操作时,我正在处理此操作似乎不起作用。 当我收到一个空的 json 响应或一个非常小的响应时,Content-Encoding 标头仍然存在并读取“gzip”。

HTTP Response headers

我的问题是为什么 NginX 不尊重此设置,我该怎么做才能修复它?

API 建立在 Lumen 微框架之上。

我已在我的 nginx.conf 中附加了我正在使用的 Gzip 设置:

  # Compression

  # Enable Gzip compressed.
  gzip on;

  # Enable compression both for HTTP/1.0 and HTTP/1.1.
  gzip_http_version  1.1;

  # Compression level (1-9).
  # 5 is a perfect compromise between size and cpu usage, offering about
  # 75% reduction for most ascii files (almost identical to level 9).
  gzip_comp_level    5;

  # Don't compress anything that's already small and unlikely to shrink much
  # if at all (the default is 20 bytes, which is bad as that usually leads to
  # larger files after gzipping).
  gzip_min_length    1000;

  # Compress data even for clients that are connecting to us via proxies,
  # identified by the "Via" header (required for CloudFront).
  gzip_proxied       any;

  # Tell proxies to cache both the gzipped and regular version of a resource
  # whenever the client's Accept-Encoding capabilities header varies;
  # Avoids the issue where a non-gzip capable client (which is extremely rare
  # today) would display gibberish if their proxy gave them the gzipped version.
  gzip_vary          on;

  # Compress all output labeled with one of the following MIME-types.
  gzip_types
    application/atom+xml
    application/javascript
    application/json
    application/rss+xml
    application/vnd.ms-fontobject
    application/x-font-ttf
    application/x-web-app-manifest+json
    application/xhtml+xml
    application/xml
    font/opentype
    image/svg+xml
    image/x-icon
    text/css
    text/plain
    text/x-component;
  # text/html is always compressed by HttpGzipModule

【问题讨论】:

  • 你确定是 nginx 压缩而不是你的应用?
  • 是的,很确定... :-)
  • 我刚刚遇到了同样的行为,并认为这是由于NGINX gzip module documentation 中的注释说明“长度仅由“Content-Length”响应头字段确定。”跨度>

标签: nginx server gzip httpresponse lumen


【解决方案1】:

确认我上面的注释,这似乎与NGINX gzip module documentation 中的注释相对应,说明“长度仅由“Content-Length”响应头字段确定。”

使用gzip_min_length 1000;,我的 JSON 响应被 gzip 压缩,即使它们只有 100 个字节。

我更改了我的应用程序以添加 Content-Length: 100 标头,NGINX 发送 JSON 响应而不使用 gzip 编码。

如果我将配置更改为具有相同 100 字节内容长度的 gzip_min_length 80;,则 NGINX 会按预期应用 gzip 编码。

简短的故事:您需要为 NGINX 应用 Content-Length 标头以正确处理 gzip_min_length 检查。

【讨论】:

    猜你喜欢
    • 2018-08-12
    • 1970-01-01
    • 2021-03-08
    • 2016-01-22
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 2014-12-12
    • 2015-03-11
    相关资源
    最近更新 更多