• 背景
  • 解决
  • 增加nginx服务器自动解压
  • 优劣势
  • 首页
  • 归档

nginx先解压gzip再返回给客户端

  • yuc
  • 2022-03-09
  • 2022-03-23
  • 浏览:824
  • 0

背景

现代浏览器以及测试工具对服务端返回的结果有很好的兼容性,会自动解压内容再展示给用户。但是仍然有一些http库或者命令行工具(如curl)不会解压,如果在有大批量这种类型用户时,该如何直接返回明文内容给客户端呢?如下图:

  • 服务端返回压缩,客户端不自动解压,显示乱码
  • 服务端返回压缩,客户端手动解压

解决

解决方案如下:

  • 服务端直接返回未压缩内容
  • 增加nginx服务器,在nginx中解压后再返回给客户端

增加nginx服务器自动解压

此模块默认不会开启,需要在编译安装时增加

The ngx_http_gunzip_module module is a filter that decompresses responses with “Content-Encoding: gzip” for clients that do not support “gzip” encoding method. The module will be useful when it is desirable to store data compressed to save space and reduce I/O costs.

This module is not built by default, it should be enabled with the --with-http_gunzip_module configuration parameter.

配置示例:

location /storage/ {
    gunzip on;
    ...
}

配置段:

Syntax: gunzip on | off;
Default:    
gunzip off;
Context:    http, server, location

优劣势

在nginx端解压对于有大量客户端的场景下能节省客户端维护代码的代价,但是对于服务端出口流量以及cpu/内存资源是一种额外消耗,所以需要酌情使用

© 2023
Theme by Wing
  • {{ item.name }}
  • {{ item.name }}