一篇文章读懂nginx的gzip_static模块
Nginx支持静态和动态两种包体gzip压缩方式,分别对应模块ngx_http_gzip_static,ngx_http_gzip。
我们知道gzip是CPU密集型的应用,实时动态压缩比较消耗CPU资源。另外,如果使用gzip,则sendfile零拷贝技术无法使用。为进一步提高Nginx的性能,我们可以使用静态gzip压缩,提前将需要压缩的文件压缩好,当客服请求到达时,直接发送压缩好的.gz文件,如此就减轻了服务器CPU的压力,提高了性能。缺省ngx_http_gzip_static模块并未启用,需要重新编译。
#注:根据需要自行添加其它参数 ./configure --with-http_gzip_static_module
准备.gz文件:所有待压缩的文件,需要保留源文件和.gz文件,在相同WEB目录。如下,以index.html为例。
#压缩保留源文件的方法: [root@test01 html]# gzip -c index.html > index.html.gz [root@test01 html]# ll index.* -rw-r--r--. 1 root root 620 Jun 23 2021 index.html -rw-r--r--. 1 root root 401 Jun 23 2021 index.html.gz
使用touch同步源文件和.gz文件的修改时间。文件修改时间对应Last-Modified响应字段,HTTP缓存中使用很广泛,同步二者时间,目的是保持缓存过期判断的一致性。
touch index.html.gz -r index.html
添加配置文件:
gzip_static on;
gzip_static优先级高于gzip,$gzip_ratio对于gzip_static不生效,如果gzip_static失效,如缺少.gz,则gzip会生效。
gzip_static生效时,和gzip不同,Content-Encoding和Cotent-Length可以同时存在,因为响应在发送前已经明确其大小。
实际执行的效果:
[root@test01 html]# curl test --compressed -I HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Wed, 23 Feb 2022 04:14:02 GMT Content-Type: text/html Content-Length: 401 Last-Modified: Wed, 23 Jun 2021 06:31:52 GMT Connection: keep-alive ETag: "60d2d558-191" Content-Encoding: gzip
也可以考虑用always参数
gzip_static always;
always的语义是不考虑客户端是否支持gzip解压【注:依据是客户端发送的Accept-Encoding】,Nginx都将发送.gz文件,而on则是当客户端不支持gzip解压时,则发送原始文件。
下面是gzip_static on,curl启用压缩和不启用压缩的对比,可以看到仅当curl启用压缩才发送.gz文件。
[root@test01 html]# curl test --compressed -I HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Wed, 23 Feb 2022 07:27:43 GMT Content-Type: text/html Content-Length: 401 Last-Modified: Wed, 23 Jun 2021 06:31:52 GMT Connection: keep-alive ETag: "60d2d558-191" Content-Encoding: gzip [root@test01 html]# curl test -I HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Wed, 23 Feb 2022 07:27:49 GMT Content-Type: text/html Content-Length: 620 Last-Modified: Wed, 23 Jun 2021 06:31:52 GMT Connection: keep-alive ETag: "60d2d558-26c" Accept-Ranges: bytes
下面是设置为gzip_static always,curl启用压缩和不启用压缩的对比,可以发现无论curl是否启用压缩,都将发送.gz文件。
[root@test01 html]# curl test -I HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Wed, 23 Feb 2022 07:32:56 GMT Content-Type: text/html Content-Length: 401 Last-Modified: Wed, 23 Jun 2021 06:31:52 GMT Connection: keep-alive ETag: "60d2d558-191" Content-Encoding: gzip #客户端没启用压缩,返回的内容仍然是gzip压缩的 [root@test01 html]# curl test --compressed -I HTTP/1.1 200 OK Server: nginx/1.20.1 Date: Wed, 23 Feb 2022 07:33:05 GMT Content-Type: text/html Content-Length: 401 Last-Modified: Wed, 23 Jun 2021 06:31:52 GMT Connection: keep-alive ETag: "60d2d558-191" Content-Encoding: gzip [root@test01 html]# curl test --compressedstream ssl test! stream ssl test!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.
#因为没有启用压缩,所以.gz文件无法解压,将被当做二进制文件输出,有Warning提示。 [root@test01 html]# curl test Warning: Binary output can mess up your terminal. Use "--output -" to tell Warning: curl to output it to your terminal anyway, or consider "--output Warning:" to save to a file. [root@test01 html]#
Chrome中也可以通过控制Accept-Encoding的发送,仿真是否需要响应的包体压缩,看下图:

总之,gzip_static是对gzip的补充,通过简单的设置,就能使Nginx提供更好的性能。
参考:gzip_static
您可能感兴趣的文章
- 01-07Tomcat部署Bolo动态博客
- 01-07图文详解nginx日志切割的实现
- 01-07如何解决swarm docker 端口不通问题
- 01-07详解nginx location指令
- 01-07如何解决docker http 500错误问题
- 01-07windows server 2008安装配置DNS服务器
- 01-07Linux上搭载Nginx负载均衡配置使用案例详解
- 01-07NGINX 权限控制文件预览和下载的实现原理
- 01-07Nginx防盗链与服务优化配置的全过程
- 01-07使用 Apache 反向代理的设置技巧


阅读排行
推荐教程
- 12-23linux中ftp无法访问怎么办
- 12-11docker存储目录迁移示例教程
- 12-10docker start启动容器后仍然exit状态的解决
- 12-10Linux下如何安装Logstash
- 01-05Shell脚本去重的几种方法实例
- 12-22kvm虚拟机配置NAT端口转发的实现方法
- 12-19Zabbix SAML SSO 登录绕过漏洞的操作流程
- 12-15Docker-Compose搭建Spark集群的实现方法
- 12-14Docker Desktop无法正常启动解决(failed to start...)
- 12-14k8s 与docker空间使用分析与清理方法




