服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - 一篇文章读懂nginx的gzip_static模块

一篇文章读懂nginx的gzip_static模块

2022-08-08 10:00乘风破浪2021 Nginx

gzip是针对于请求实时进行压缩,cpu开销大,gzip_static 完全可以在编译后使用压缩工具搞出来,下面这篇文章主要给大家介绍了如何通过一篇文章读懂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模块并未启用,需要重新编译。

?
1
2
#注:根据需要自行添加其它参数
./configure --with-http_gzip_static_module

准备.gz文件:所有待压缩的文件,需要保留源文件和.gz文件,在相同WEB目录。如下,以index.html为例。

?
1
2
3
4
5
#压缩保留源文件的方法:
[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缓存中使用很广泛,同步二者时间,目的是保持缓存过期判断的一致性。

?
1
touch index.html.gz -r index.html

添加配置文件:

?
1
gzip_static on;

gzip_static优先级高于gzip,$gzip_ratio对于gzip_static不生效,如果gzip_static失效,如缺少.gz,则gzip会生效。

gzip_static生效时,和gzip不同,Content-Encoding和Cotent-Length可以同时存在,因为响应在发送前已经明确其大小。

实际执行的效果:

?
1
2
3
4
5
6
7
8
9
10
[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参数

?
1
gzip_static always;

always的语义是不考虑客户端是否支持gzip解压【注:依据是客户端发送的Accept-Encoding】,Nginx都将发送.gz文件,而on则是当客户端不支持gzip解压时,则发送原始文件。

下面是gzip_static on,curl启用压缩和不启用压缩的对比,可以看到仅当curl启用压缩才发送.gz文件。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[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文件。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
[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="http://nginx.org/" rel="external nofollow"   >nginx.org</a>.<br/>
Commercial support is available at
<a href="http://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

总结

到此这篇关于一篇文章读懂nginx中gzip_static模块的文章就介绍到这了,更多相关nginx gzip_static模块内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_45462681/article/details/123086969

延伸 · 阅读

精彩推荐
  • Nginxnginx利用referer指令实现防盗链配置

    nginx利用referer指令实现防盗链配置

    nginx模块ngx_http_referer_module通常用于阻挡来源非法的域名请求,我们应该牢记。下面这篇文章主要介绍了nginx利用referer指令实现防盗链配置的相关资料,需要...

    LoyaChen3682019-11-24
  • NginxNginx 启用 BoringSSL的配置方法

    Nginx 启用 BoringSSL的配置方法

    这篇文章主要介绍了Nginx 启用 BoringSSL的配置方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    南琴浪博客5112019-12-14
  • Nginx详解阿里云LINUX服务器配置HTTPS(NGINX)

    详解阿里云LINUX服务器配置HTTPS(NGINX)

    本篇文章主要介绍了阿里云LINUX服务器配置HTTPS(NGINX) ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    风的姿态5222019-11-23
  • NginxNginx实现Nacos反向代理的项目实践

    Nginx实现Nacos反向代理的项目实践

    在日常的web网站部署中,经常会用到nginx反向代理,本文主要介绍了Nginx实现Nacos反向代理的项目实践,Nginx实现Nacos反向代理的项目实践...

    烟火缠过客4242022-08-03
  • NginxNginx if语句加正则表达式实现字符串截断

    Nginx if语句加正则表达式实现字符串截断

    这篇文章主要介绍了Nginx if语句加正则表达式实现字符串截断功能,特殊场合下可能会需要这个功能,NGINX的奇淫技巧之一,需要的朋友可以参考下 ...

    Nginx教程网10862019-10-26
  • Nginx常见的Nginx配置误区

    常见的Nginx配置误区

    对很多人而言,配置Nginx+PHP无外乎就是搜索一篇教程,然后拷贝粘贴。听上去似乎也没什么问题,可惜实际上网络上很多资料本身年久失修,漏洞百出,如...

    Nginx配置网5682019-10-16
  • Nginxnginx禁止某个IP访问站点的设置方法

    nginx禁止某个IP访问站点的设置方法

    近期发现博客遭到某些人的恶意灌水,频繁地利用发帖机器人发表评论,给博客的管理带来诸多不便,搜索了一下资料,可以利用nginx的ngx_http_access_module...

    nginx技术网5612019-10-10
  • NginxNginx隐藏index.php和Pathinfo模式配置例子

    Nginx隐藏index.php和Pathinfo模式配置例子

    这篇文章主要介绍了Nginx隐藏index.php和Pathinfo模式配置例子,需要的朋友可以参考下 ...

    Nginx配置网2882019-10-18