Apache访问日志不记录静态文件,访问日志切割,静态元素过期时间
温馨提示:这篇文章已超过874天没有更新,请注意相关的内容是否还可用!
访问日志不记录静态文件
网站大多元素为静态文件,如图片、css、js等,这些元素可以不用记录
把虚拟主机配置文件改成如下:
<VirtualHost *:80> ServerName www.ahao1.com DocumentRoot "/data/wwwroot/ahao1.com" <Directory "/data/wwwroot/ahao1.com"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> ErrorLog "logs/ahao1.com-error_log" 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 "logs/ahao1.com-access_log" combined env=!img </VirtualHost>
验证是否正确 /usr/local/httpd/bin/apachectl -t
重新加载配置文件
# /usr/local/httpd/bin/apachectl graceful
# cd /data/wwwroot/ahao1.com
# wget http://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png
# curl -x127.0.0.1:80 -I ahao1.com/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png
[root@localhost ahao1.com]# tail /usr/local/httpd/logs/ahao1.com-access_log
tail: cannot open ‘/usr/local/httpd/logs/ahao1.com-access_log’ for reading: No such file or directory
访问日志切割
日志一直记录总有一天会把整个磁盘占满,所以有必要让它自动切割,并删除老的日志文件
把虚拟主机配置文件改成如下:
<VirtualHost *:80> ServerName www.ahao1.com DocumentRoot "/data/wwwroot/ahao1.com" <Directory "/data/wwwroot/ahao1.com"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> ErrorLog "logs/ahao1.com-error_log" 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/httpd/bin/rotatelogs -l logs/ahao1.com-access_%Y%m%d.log 86400" combined env=!img </VirtualHost>
验证是否正确 /usr/local/httpd/bin/apachectl -t
重新加载配置文件
# /usr/local/httpd/bin/apachectl graceful
# ll /usr/local/httpd/logs/ahao1.com-access*
静态元素过期时间
浏览器访问网站的图片时会把静态的文件缓存在本地电脑里,这样下次再访问时就不用去远程下载了
站点配置文件增加配置
<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>
验证是否正确 /usr/local/httpd/bin/apachectl -t
报错
Invalid command 'ExpiresActive', perhaps misspelled or defined by a module not included in the server configuration
解决方法:
在httpd.conf中取消这一行注释
#LoadModule expires_module modules/mod_expires.so
重新加载配置文件
# /usr/local/httpd/bin/apachectl graceful
# curl -x127.0.0.1:80 -I ahao1.com/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png