Nginx默认有Expires模块,但是却没有Etags模块.按照Nginx作者Igor Sysoev的观点,他认为在对静态文件处理上,还看不出Etags比Last-Modified的好处.
但是也有人说Nginx加了Etags模块会好很多,如这个模块的作者说的那样:
I see the complete lack of Etag support as an oversight. It’s more granular than Last-Modified, which is only accurate to the second, and only measures change along the axis of time; touching a file doesn’t change it’s content, but would force a cache miss when the cache is based on nothing but timestamp. Etags, on the other hand, are content-based identifiers that provide a mechanism for confirmation that the content of the file you’re reading is accurate, regardless of inconsequential fiddling or deployment on the server.
详细内容请访问:
http://mikewest.org/2008/11/generating-etags-for-static-content-using-nginx.
下面就主要说说该模块的安装,从这里可以得到源代码和安装说明:
http://wiki.github.com/mikewest/nginx-static-etags
环境是Debian,我们需要安装git库.一般来说直接执行:'apt-get install git'就可以了,但是这样无法取得SVN上的资料.因为在debian稳定版中去除了git-core库.我们安装就是了,然后nginx方面,只是在原有配置文件上增加这个模块.要查询之前你配置的参数,可以执行:
$/usr/local/sbin/nginx -V
然后在后面增加这个第三方模块.关于第三方模块的使用,可以参考nginx wiki:
http://wiki.nginx.org/Nginx3rdPartyModules
$apt-get install git git-core $curl -O http://sysoev.ru/nginx/nginx-0.7.63.tar.gz $tar -zxvf ./nginx-0.7.63.tar.gz $git-clone git://github.com/mikewest/nginx-static-etags.git /usr/src/nginx-static-etags $cd nginx-0.7.63/ $./configure --add-module=/usr/src/nginx-static-etags \ ...(你原有配置信息) $make |
在这个步骤,出错通常会显示这个信息:
/usr/src/nginx-static-etags/ngx_http_static_etags_module.c:168:2: error: no newline at end of file make[1]: *** [objs/addon/nginx-static-etags/ngx_http_static_etags_module.o] Error 1 make[1]: Leaving directory `/usr/src/nginx-0.7.63' make: *** [build] Error 2 |
出错的原因是这个第三方模块的c文件的最后一行没有用空白行隔开.我们编辑一下这个c文件,在最后一行(也就是168行)增加一个空行就可以了.然后再执行make命令.
$vi /usr/src/nginx-static-etags/ngx_http_static_etags_module.c $... $make #复制到sbin位置就可以了. $mv /usr/local/sbin/nginx /usr/local/sbin/nginx.old $cp objs/nginx /usr/local/sbin/ |
最后重启nginx.
配置方面,一般将所有静态内容都配置Etags就可以了(参数说明).如下:
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|xml|txt|flv|swf|mid|doc|cur|xls|pdf|txt|mp3|wma)$ { expires 7d; FileETag on; etag_format "%X%X"; } |
Nginx增加Etag模块
环境:
CentOS5.6_x64
Nginx 1.0.0
官网地址:
nginx: http://nginx.org/
Etag源地址:https://github.com/mikewest/nginx-static-etags
Etag下载地址:https://nodeload.github.com/mikewest/nginx-static-etags/tarball/master
安装步骤:
1.下载Etag 文件名 mikewest-nginx-static-etags-25bfaf9.tar.gz 路径/root/soft/mikewest-nginx-static-etags-25bfaf9.tar.gz
2.重新编译nginx,添加Etag模块
a.
nginx -V //获取nginx的编译参数 --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module |
b.
cd /root/soft tar zxvf mikewest-nginx-static-etags-25bfaf9.tar.gz echo '' >> /root/soft/ mikewest-nginx-static-etags-25bfaf9/ngx_http_static_etags_module.c //bug 增添一空行,否则编译不过 cd /root/soft/nginx1.0.0 ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/soft/mikewest-nginx-static-etags-25bfaf9 //注意后面添加的路径要对 make //这里确记不要make install,否则有可能把当前的配置文件覆盖,影响当前业务。 cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak //备份个比较安全,出错马上还原回去 cp objs/nginx /usr/local/nginx/sbin/nginx.tmp //替换成新的nginx rm -rf /usr/local/nginx/sbin/nginx && mv /usr/local/nginx/sbin/nginx.tmp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx -t kill -USR2 `cat /usr/local/nginx/logs/nginx.pid` //让nginx把nginx.pid改成nginx.pid.oldbin 并启动新的nginx kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin` //习惯使用nginx -s reload来重启,发现不能生效,以为我配置错了花一个早上时间找问题。 |
c. 修改nginx.conf
location ~ .*/.(gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|xml|txt|flv|swf|mid|doc|cur|xls|pdf|txt|)$ { FileETag on; etag_format "%X%X"; expires 30d; } |
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
然后用curl测试看看
curl --head http://www.***.com/css/style.css