首先要明白mod_expires这个模块的作用是什么?它是控制服务器响应expires头和cache-contral头部的max-age指令.这些http头向客户端表明了文档的有效性和持久性.如果有缓存,客户端可以直接从缓存中读取,客户端考察缓存中的副本,看看是否已过期或失效.再决定是否从服务器端更新.Expires是浏览器Cache机制的一部分,浏览器的缓存取决于Header中的四个值: Cache-Control, Expires, Last-Modified, ETag。此模块安装后可以减少10%的重复请求.
(一)报错解决:
/usr/local/apache/bin/apxs -i -a -c /usr/local/src/httpd-2.0.55/modules/metadata/mod_expires.c
如果报错:
Warning! dlname not found in /usr/local/apache/modules/mod_expires.la.
Assuming installing a .so rather than a libtool archive.
chmod 755 /usr/local/apache/modules/mod_expires.so
chmod: 无法访问‘/usr/local/apache/modules/mod_expires.so’: 没有那个文件或目录
apxs:Error: Command failed with rc=65536
则执行: (引用张宴博客)
gcc -shared -o /usr/local/apache/modules/mod_expires.so /usr/local/src/httpd-2.0.55/modules/metadata/mod_expires.o
/usr/local/apache/bin/apxs -i -a -c /usr/local/src/httpd-2.0.55/modules/metadata/mod_expires.o
编译成功!
(二)具体设置:
- <IfModule mod_expires.c>
- ExpiresActive On
- ExpiresByType p_w_picpath/gif "access plus 1 month"
- ExpiresByType p_w_picpath/jpeg "access plus 1 month"
- ExpiresByType p_w_picpath/x-icon "access plus 1 month"
- ExpiresByType p_w_picpath/png "access plus 1 month"
- ExpiresByType text/html "access plus 30 minutes"
- ExpiresByType text/css "access plus 30 minutes"
- ExpiresByType text/js "access plus 30 minutes"
- ExpiresByType application/x-javascript "access plus 30 minutes"
- ExpiresByType application/x-shockwave-flash "access plus 30 minutes"
- </IfModule>
重启apache生效
#/usr/local/apache/bin/apachel stop
#/usr/local/apache/bin/apachel start