本文共 2137 字,大约阅读时间需要 7 分钟。
当用户访问网站的时候一些图片会自动缓存在电脑浏览器缓存中,当下一次访问的时候就不必要再次加载了。当超时或更新时,会重新请求加载。
1、配置虚拟主机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | [root@juispan ~] # vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/www/abc.com" ServerName abc.com ServerAlias www.abc.com ErrorLog "logs/abc.com-error_log" <IfModule mod_expires.c> ExpiresActive on ##打开功能开关 ExpiresByType image /gif "access plus 1 days" ##定义失效时间 ExpiresByType image /jpeg "access plus 24 hours" ExpiresByType image /png "access plus 24 hours" ExpiresByType text /css "now plus 2 hour" ExpiresByType application /x-javascript "now plus 2 hours" ExpiresByType application /javascript "now plus 2 hours" ExpiresByType application /x-shockwave-flash "now plus 2 hours" ExpiresDefault "now plus 0 min" < /IfModule > SetEnvIf Request_URI ".*\.gif$" img SetEnvIf Request_URI ".*\.jpg$" img SetEnvIf Request_URI ".*\.png$" img SetEnvIf Request_URI ".*\.bmp$" img SetEnvIf Request_URI ".*\.swf$" img SetEnvIf Request_URI ".*\.js$" img SetEnvIf Request_URI ".*\.css$" img CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/abc.com-access_%Y%m%d.log 86400" combined env =!img < /VirtualHost > |
2、检查重新加载
1 2 3 4 5 6 7 8 | [root@juispan ~] # /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@juispan ~] # /usr/local/apache2.4/bin/apachectl -M |grep -i expire [root@juispan ~] # vi /usr/local/apache2.4/conf/httpd.conf LoadModule expires_module modules /mod_expires .so ##取消注释启用 [root@juispan ~] # /usr/local/apache2.4/bin/apachectl -M |grep -i expire expires_module (shared) [root@juispan ~] # /usr/local/apache2.4/bin/apachectl graceful |
3、验证效果
1 2 3 4 5 6 7 8 9 10 11 | [root@juispan abc.com] # curl -x127.0.0.1:80 abc.com/123.gif -I HTTP /1 .1 200 OK Date: Fri, 21 Jul 2017 11:19:21 GMT Server: Apache /2 .4.27 (Unix) PHP /7 .1.6 Last-Modified: Fri, 21 Jul 2017 11:19:21 GMT ETag: W/ "8c5-555b2e6023fc0" Accept-Ranges: bytes Content-Length: 2245 Cache-Control: max-age=86400 ##最大老化时间86400秒,也就是1天 Expires: Sat, 22 Jul 2017 11:19:21 GMT ##过期时间 Content-Type: image /gif |