数据库教程:一篇文章读懂nginx的gzip_static模块

nginx支持静态和动态两种包体gzip压缩方式,分别对应模块ngx_http_gzip_static,ngx_http_gzip。我们知道gzip是cpu密集型的应用,实时动态压缩比较消耗cpu资源

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 --compressed  <!doctype html>  <html lang="en">  <head>  <title>stream ssl test!</title>  <style>      body {          width: 35em;          margin: 0 auto;          font-family: tahoma, verdana, arial, sans-serif;      }  </style>  </head>  <body>  <h1>stream ssl test!</h1>  <p>if you see this page, the nginx web server is successfully installed and  working. further configuration is required.</p>    <p>for online documentation and support please refer to  <a href="https://nginx.org/" rel="external nofollow"   >nginx.org</a>.<br/>  commercial support is available at  <a href="https://nginx.com/" rel="external nofollow"   >nginx.com</a>.</p>    <p><em>thank you for using nginx.</em></p>  </body>  </html>    #因为没有启用压缩,所以.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: <file>" to save to a file.  [root@test01 html]#   

chrome中也可以通过控制accept-encoding的发送,仿真是否需要响应的包体压缩,看下图:

一篇文章读懂nginx的gzip_static模块

总之,gzip_static是对gzip的补充,通过简单的设置,就能使nginx提供更好的性能。

参考:gzip_static

相关文章:

总结

到此这篇关于一篇文章读懂nginx中gzip_static模块的文章就介绍到这了,更多相关nginx gzip_static模块内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多数据库技术:一篇文章读懂nginx的gzip_static模块,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/1093029.html

(0)
上一篇 2022年5月14日
下一篇 2022年5月14日

精彩推荐