热度 7 |
转载自:http://www.xmlchina.org/nginx-configuration-support-etags-module/
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.
下面就主要说说该模块的安装,从这里可以得到源代码和安装说明:
环境是Debian,我们需要安装git库.一般来说直接执行:'apt-get install git'就可以了,但是这样无法取得SVN上的资料.因为在debian稳定版中去除了git-core库.我们安装就是了,然后nginx方面,只是在原有配置文件上增加这个模块.要查询之前你配置的参数,可以执行:
$/usr/local/sbin/nginx -V
然后在后面增加这个第三方模块.关于第三方模块的使用,可以参考nginx wiki:
$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
$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";
}