nginx静态压缩模块ngx_http_gzip_static_module配置及使用

作者:袖梨 2022-06-30

nginx静态压缩和apache gzip压缩类似,原理也差不多,本文我们来介绍一下nginx静态压缩模块ngx_http_gzip_static_module的一些使用方法。

在搭建squid网页加速的时候,对于大的css 或者js要进行压缩,然后再进行缓存,这样能够提高减小下载量提高页面响应速度。如果你用的是squid 3.0以前的版本并且用的是 ngnix server的话可能会碰到如下问题: 不用squid直接打开页面则客户端返回的是压缩的状态,如果启用squid加速会发现下载下来的页面不是压缩状态。这里面主要是没有启动ngnix 的静态缓存模块(ngx_http_gzip_static_module)导致。

打开静态缓存问题就解决了
nginx编译选项
./configure --with-http_gzip_static_module
配置nginx
gzip_static on;
gzip_http_version 1.1;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6] .";
gzip_vary on;
#找不到预压缩文件,进行动态压缩
gzip on;
gzip_min_length 1000;
gzip_buffers 4 16k;
gzip_comp_level 5;
gzip_types text/plain application/x-javascript text/css application/xml;
#gzip公共配置
gzip_http_version 1.1
gzip_proxied expired no-cache no-store private auth;
gzip_vary on说明

对于支持gzip的请求反向代理缓存服务器将返回gzip内容,不支持gzip的客户端返回原始内容。
其他说明

gzip_static配置优先级高于gzip
开启nginx_static后,对于任何文件都会先查找是否有对应的gz文件
gzip_types设置对gzip_static无效


HttpGzipStaticModule Nginx压缩传输

在从磁盘向支持gzip的客户端提供一个文件时,这个模块将会在同样的目录(或者叫位置)中查找同请求文件名相同的、以".gz"格式结尾的文件,这个文件被称为文件的“预压缩格式”,之所以称为“预压缩格式”,是因为Nginx不会去对该文件进行压缩,即使是该文件被访问之后也不会产生".gz"格式的文件,因此需要我们自己压缩。那么这种机制的作用是什么呢?很简单,这么做的原因在于避免每次请求都将对同一个文件进行压缩。

ngx_http_gzip_static_module从nginx 0.6.24版本开始提供,但是在默认安装中它是不会被编译安装,因此,在编译时需要指定--with-http_gzip_static_module选项。

配置示例

gzip_static on;

gzip_http_version 1.1;

gzip_proxied expired no-cache no-store private auth;

gzip_disable "MSIE [1-6].";

gzip_vary on;

指 令

指令名称:gzip_static

功 能:启用该模块。需要注意的是,确定压缩版本和非压缩版本的时间戳要匹配,以便提供最新的内容。

语 法: gzip_static on|off

默 认 值: gzip_static off

使用环境: http, server, location


以下命令参考NginxHttpGzipModule模块:

指令名称:gzip_http_version

指令名称:gzip_proxied

指令名称:gzip_disable

指令名称:gzip_vary


使用实例


在下面的例子中我们先为现有的网页index.html生成一个".gz"格式的文件,即index.html.gz,然后测试访问;在对index.html文件进行修改,然后再访问测试。


添加配置

gzip on;

gzip_types text/plain application/xml;

gzip_static on;

访问测试

生成index.html文件的另一个格式index.html.gz:



[root@mfsmaster html]# ls

index.html

[root@mfsmaster html]#cat index.html





Welcome to nginx!





Welcome to nginx! 哈哈!!!







[root@mfsmaster html]# gzip -c index.html > index.html.gz

[root@mfsmaster html]# ls

index.html index.html.gz


确定文件的访问时间:


[root@mfsmaster html]# stat index.*

File: ‘index.html’

Size: 167 Blocks: 8 IO Block: 4096 一般文件

Device: fd00h/64768d Inode: 5394667 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2011-08-18 16:45:20.339995192 +0800

Modify: 2011-08-18 16:44:16.746662848 +0800

Change: 2011-08-18 16:44:16.746662848 +0800

File: ‘index.html.gz’

Size: 151 Blocks: 8 IO Block: 4096 一般文件

Device: fd00h/64768d Inode: 5394635 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2011-08-18 16:45:20.338995344 +0800

Modify: 2011-08-18 16:45:20.339995192 +0800

Change: 2011-08-18 16:45:20.339995192 +0800


访问该文件:


查看文件的访问时间



[root@mfsmaster html]# stat index.*

File: ‘index.html’

Size: 167 Blocks: 8 IO Block: 4096 一般文件

Device: fd00h/64768d Inode: 5394667 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2011-08-18 16:45:20.339995192 +0800

Modify: 2011-08-18 16:44:16.746662848 +0800

Change: 2011-08-18 16:44:16.746662848 +0800

File: ‘index.html.gz’

Size: 151 Blocks: 8 IO Block: 4096 一般文件

Device: fd00h/64768d Inode: 5394635 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2011-08-18 16:59:01.040229792 +0800

Modify: 2011-08-18 16:45:20.339995192 +0800

Change: 2011-08-18 16:45:20.339995192 +0800

我们比较以下这两个文件的访问时间戳,肯定的说,我们的访问是有‘index.html.gz’文件提供的。


下面将对index.html文件进行修改:


[root@mfsmaster html]# vi index.html







Welcome to nginx!





Welcome to nginx! 哈哈!!!哈哈!!!







查看文件的访问时间

[root@mfsmaster html]# stat index.*

File: ‘index.html’

Size: 183 Blocks: 8 IO Block: 4096 一般文件

Device: fd00h/64768d Inode: 5394671 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2011-08-18 18:02:40.022656216 +0800

Modify: 2011-08-18 18:02:40.022656216 +0800

Change: 2011-08-18 18:02:40.023656064 +0800

File: ‘index.html.gz’

Size: 151 Blocks: 8 IO Block: 4096 一般文件

Device: fd00h/64768d Inode: 5394635 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2011-08-18 16:59:01.040229792 +0800

Modify: 2011-08-18 16:45:20.339995192 +0800

Change: 2011-08-18 16:45:20.339995192 +0800


再次访问该网页:


得到的页面和原来的一样,我们再次查看文件的访问时间戳(如果你也是在做测试,那么你需要将IE浏览器的缓存清除):


[root@mfsmaster html]# stat index.*

File: ‘index.html’

Size: 183 Blocks: 8 IO Block: 4096 一般文件

Device: fd00h/64768d Inode: 5394671 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2011-08-18 18:02:40.022656216 +0800

Modify: 2011-08-18 18:02:40.022656216 +0800

Change: 2011-08-18 18:02:40.023656064 +0800

File: ‘index.html.gz’

Size: 151 Blocks: 8 IO Block: 4096 一般文件

Device: fd00h/64768d Inode: 5394635 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2011-08-18 18:09:11.569132104 +0800

Modify: 2011-08-18 16:45:20.339995192 +0800

Change: 2011-08-18 16:45:20.339995192 +0800

相信你一定看清楚了,是由文件‘index.html.gz’来提供访问的,Nginx并没有提供最新时间的‘index.html’文件。你要还不信,那就将文件‘index.html.gz’删除再访问,网页绝对是最新版本,在此就不再举例了。


我们看一下,以下访问情况:

[root@mfsmaster html]# ll

总用量 52

-rw-r--r-- 1 root root 152 8月 18 19:07 index.html

-rw-r--r-- 1 root root 151 8月 18 16:45 index.html.gz.old

-rw-r--r-- 1 root root 12376 8月 19 08:22 xx.html

-rw-r--r-- 1 root root 4032 8月 19 12:12 xx.html.gz


在这里为了说明访问情况,我们访问http://www.***xx.com/xx.html,页面就不再截取了,看捕获包的情况:

字节数:1260*3+252=4032,绝对访问的是xx.html.gz页面!

说了这么多,其实我们要明白的是压缩传输的好处,绝对的节省带宽,我们再算一下,看下面的算式:

(12376-4032)/12376=67.42%

(12376-4324)/12376=65.06%

从上面可以看出,我们经过压缩的网页,直接减小了大约60%的带宽,这也可以为服务器支付节约不少成本了。

相关文章

精彩推荐