关于 nginx 的 gzip ,可以分为两种:

  1. nginx 动态压缩,对每个请求先压缩再输出。
  2. nginx 静态压缩,使用现成的扩展名为 .gz 的预压缩文件。

nginx 动态压缩

开启 nginx 动态压缩只需要在 nginx.conf 文件中做如下修改即可:

  1. # 开启和关闭gzip模式
  2. gzip on;
  3. # gizp压缩起点,文件大于1k才进行压缩
  4. gzip_min_length 1k;
  5. # 设置压缩所需要的缓冲区大小,以4k为单位,如果文件为7k则申请2*4k的缓冲区
  6. gzip_buffers 4 16k;
  7. # 设置gzip压缩针对的HTTP协议版本
  8. gzip_http_version 1.0;
  9. # gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间
  10. gzip_comp_level 2;
  11. # 进行压缩的文件类型
  12. gzip_types text/plain application/javascript text/css application/xml;
  13. # 是否在http header中添加Vary: Accept-Encoding,建议开启
  14. gzip_vary on;

nginx 静态压缩

nginx 静态压缩需要使用 ngx_http_gzip_static_module 模块,先简单看一下介绍:

ngx_http_gzip_static_module 模块允许发送扩展名为 .gz 的预压缩文件,而不是常规文件。

默认情况下未构建此模块,应使用 --with-http_gzip_static_module 配置参数启用它 。

在安装包目录编译安装:

  1. ./configure --with-http_gzip_static_module

例如 Nginx 安装配置 安装 Nginx 第4步编译安装,在第一步./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35 之后加上 --with-http_gzip_static_module

然后修改 nginx.conf 配置文件:

  1. gzip_static on;
  2. gzip_proxied expired no-cache no-store private auth;

nginx 动态压缩 和 静态压缩结合使用会更好:

  1. gzip_static on;
  2. gzip_proxied expired no-cache no-store private auth;
  3. gzip on;
  4. gzip_min_length 1k;
  5. gzip_buffers 4 16k;
  6. gzip_http_version 1.0;
  7. gzip_comp_level 2;
  8. gzip_types text/plain application/javascript text/css application/xml;
  9. gzip_vary on;

首先尝试使用静态压缩,如果有则返回 .gz 的预压缩文件,否则尝试动态压缩。