在从磁盘向支持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!
|
查看文件的访问时间
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27043155/viewspace-732982/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/27043155/viewspace-732982/