Linux中国 | Linux.cn - 我们的Linux中文社区

 找回密码
 加入

QQ登录

QQ登录

搜索

Nginx配置支持Etags模块

热度 7已有 2723 次阅读2009-11-16 16:02 |关键词:模块 etags nginx

转载自: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.

下面就主要说说该模块的安装,从这里可以得到源代码和安装说明:

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";
}

发表评论 评论 (2 个评论)

回复 石头 2009-11-22 10:29
可惜的是,这个模块到现在还不是内置模块。
回复 fuhualiang 2011-1-14 16:27

facelist

你需要登录后才可以评论 登录 | 加入

无觅相关文章插件