Skip to content

gzip

请求头 Accept-Encoding: gzip 响应头 Content-Encoding: gzip Nginx 中可以配置 gzip 的作用域有 http, server, location

配置项

  • gzip on;

    开关,默认关闭。

  • gzip_buffers 16 8k; / gzip_buffers 32 4k;

    压缩时能使用的内存缓冲区大小,16 8k 代表能使用 16 个缓冲块,每个块的大小是 8k。

  • gzip_comp_level 1;

    压缩等级,数字越大压缩比越高,范围为 1-9。

  • gzip_http_version 1.1;

    使用 gzip 的 HTTP 最低版本

  • gzip_min_length 256;

    设置将被 gzip 压缩的文件的 Content-Length 需要满足的最小长度。

  • gzip_proxied any;

    • off

      不做限制,效果和 any 一样。

    • any

      无条件启用压缩

    以下选项是 Nginx 作为反向代理服务器,针对上游服务器响应头信息决定是否压缩。

    • expired

      启用压缩,如果响应头头中包含 Expires 头信息

    • no-cache

      启用压缩,如果响应头头中包含 Cache-Control:no-cache 头信息

    • no-store

      启用压缩,如果响应头头中包含 "Cache-Control:no-store 头信息

    • private

      启用压缩,如果响应头头中包含 Cache-Control:private 头信息

    • auth

      启用压缩,如果响应头头中包含 Authorization 头信息

    • no_last_modified

      启用压缩,如果响应头头中不包含 Last-Modified 头信息

    • no_etag

      启用压缩,如果响应头头中不包含 ETag 头信息

  • gzip_vary on;

    增加一个响应头 Vary: Accept-Encoding 用于适配老的浏览器

  • gzip_types text/plain text/css application/x-javascript;

    哪些 mime 类型的文件进行压缩

    text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml text/javascript application/javascript application/x-javascript text/x-json application/json application/x-web-app-manifest+json text/css text/plain text/x-component font/opentype application/x-font-ttf application/vnd.ms-fontobject image/x-icon;

  • gzip_disable "MSIE [1-6].(?!.*SV1)";

    禁止某些浏览器使用 gzip

conf

nginx
server {
    listen       80;
    server_name  192.168.44.101;
    gzip on;
    gzip_types text/html text/css application/javascript;

    location / {
      proxy_pass http://httpds;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

静态压缩

动态压缩比较消耗 CPU 资源。另外,如果使用 gzip,则 sendfile 零拷贝技术无法使用。 为进一步提高 Nginx 的性能,我们可以使用静态 gzip 压缩,提前将需要压缩的文件压缩好并 部署到 Nginx 中,当请求到达时,直接发送压缩好的 .gz 文件,提高了性能。

安装

./configure --with-http_gzip_static_module
make
systemctl stop nginx
cp ./objs/nginx /usr/local/nginx/sbin/
systemctl start nginx
/usr/local/nginx/sbin/nginx -V

配置

nginx
server {
    listen       80;
    server_name  192.168.44.101;
    gzip on;
    # gzip_static 优先级高于 gzip,如果gzip_static 失效,如缺少 .gz,则 gzip 会生效。
    # 当客户端不支持 gzip 解压时,发送原始文件。
    gzip_static  on;

    # 不考虑客户端是否支持 gzip 解压,Nginx 都将发送 .gz 文件,前提是有 .gz 文件,如
    # 果没有,还是会发送源文件。
    # gzip_static: always 

    gzip_types text/html text/css application/javascript;

    location / {
        proxy_pass http://httpds;
    }

    location ~*/(js|css|img) {
        root html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

ngx_http_gunzip_module

帮助不支持 gzip 的客户端解析 .gz 文件,这样服务器就可以只保存 .gz 文件,不用保存 源文件了。

./configure --with-http_gzip_static_module --with-http_gunzip_module
make
systemctl stop nginx
cp ./objs/nginx /usr/local/nginx/sbin/
systemctl start nginx
/usr/local/nginx/sbin/nginx -V
nginx
server {
    listen       80;
    server_name  192.168.44.101;
    gzip on;
    # gunzip 要和 gzip_static always 配合使用
    gzip_static always;
    gunzip on;

    gzip_types text/html text/css application/javascript;

    location / {
        proxy_pass http://httpds;
    }

    location ~*/(js|css|img) {
        root html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}