在从磁盘向支持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% 的带宽
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27043155/viewspace-734232/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/27043155/viewspace-734232/