11.22 访问日志不记录静态文件
网站大多元素为静态文件,如图片、css、js等,这些元素可以不用记录 ,否则会使日志文件过于庞大,占用较多的磁盘空间。
编辑虚拟主机配置文件
[root@linux-5 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
ErrorLog "logs/def.com-error_log"
SetEnvIf Request_URI ".*\.gif$" tupian //将图片格式设置为标签tupian
SetEnvIf Request_URI ".*\.jpg$" tupian //将图片格式设置为标签tupian
SetEnvIf Request_URI ".*\.png$" tupian //将图片格式设置为标签tupian
SetEnvIf Request_URI ".*\.bmp$" tupian //将图片格式设置为标签tupian
SetEnvIf Request_URI ".*\.swf$" tupian //将图片格式设置为标签tupian
SetEnvIf Request_URI ".*\.js$" tupian //将图片格式设置为标签tupian
SetEnvIf Request_URI ".*\.css$" tupian //将图片格式设置为标签tupian
CustomLog "logs/def.com-access_log" combined env=!tupian //访问日志记录排除带有标签为tupain的内容
效果测试
[root@linux-5 ~]# curl -x 192.168.88.5:80 def.com/ceshi.jpg
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /ceshi.jpg was not found on this server.</p>
</body></html>
[root@linux-5 ~]# tail /usr/local/apache2.4/logs/def.com-access_log
192.168.88.5 - - [30/May/2018:12:00:16 +0800] "GET HTTP://def.com/ HTTP/1.1" 401 381
192.168.88.5 - lem [30/May/2018:12:05:41 +0800] "GET HTTP://def.com/ HTTP/1.1" 200 7
192.168.88.5 - lem [30/May/2018:12:19:28 +0800] "GET HTTP://def.com/admin.php HTTP/1.1" 200 5
192.168.88.5 - - [30/May/2018:12:19:54 +0800] "GET HTTP://def.com/ HTTP/1.1" 200 7
192.168.88.5 - - [30/May/2018:12:26:27 +0800] "GET HTTP://def.com/admin.php HTTP/1.1" 401 381
192.168.88.5 - - [30/May/2018:13:28:45 +0800] "HEAD HTTP://www.def.com/ HTTP/1.1" 301 -
添加过滤脚本后,对于静态文件没有日志产生。
[root@linux-5 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@linux-5 ~]# curl -x 192.168.88.5:80 def.com/ceshi.jpg
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /ceshi.jpg was not found on this server.</p>
</body></html>
[root@linux-5 ~]# tail /usr/local/apache2.4/logs/def.com-access_log
192.168.88.5 - - [30/May/2018:12:00:16 +0800] "GET HTTP://def.com/ HTTP/1.1" 401 381
192.168.88.5 - lem [30/May/2018:12:05:41 +0800] "GET HTTP://def.com/ HTTP/1.1" 200 7
192.168.88.5 - lem [30/May/2018:12:19:28 +0800] "GET HTTP://def.com/admin.php HTTP/1.1" 200 5
192.168.88.5 - - [30/May/2018:12:19:54 +0800] "GET HTTP://def.com/ HTTP/1.1" 200 7
192.168.88.5 - - [30/May/2018:12:26:27 +0800] "GET HTTP://def.com/admin.php HTTP/1.1" 401 381
192.168.88.5 - - [30/May/2018:13:28:45 +0800] "HEAD HTTP://www.def.com/ HTTP/1.1" 301 -
192.168.88.5 - - [31/May/2018:16:09:36 +0800] "GET HTTP://def.com/ceshi.jpg HTTP/1.1" 404 207 "-" "curl/7.29.0"
删除过滤脚本后,日志记录了有关静态文件的日志。
11.23 访问日志切割
日志一直记录总有一天会把整个磁盘占满,所以有必要让它自动切割,并删除老的日志文件 。并且日志切割后可以用特定的格式进行命名,更加方便查找与管理。
修改虚拟主机配置文件
CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined env=!img
|/usr/local/apache2.4/bin/rotatelogs 是Apache服务自带的日志切割工具
-l选项是使切割工具的时间与本机同步,否则会按照默认时区(UTC 美国)进行切割,中国是CST
logs/123.com-access_%Y%m%d.log 86400 是日志文件的存储路径格式
%Y代表年份
%m代表月份
%d代表日期
86400 是秒钟,换算后是24小时,代表日志文件每24小时切割一次
效果测试
[root@linux-5 ~]# curl -x 192.168.88.5:80 def.com
def.com[root@linux-5 ~]# ls /usr/local/apache2.4/logs/
abc.com-access_log access_log def.com-access_log error_log
abc.com-error_log def.com-access_20180531.log def.com-error_log httpd.pid
修改完配置文件后,发现生成新的访问日志时,日志目录中生成了新的日志文件。
想要保持日志文件对于磁盘空间的合理使用,还需在系统中针对访问日志增加任务计划,周期性的删除陈旧的访问日志,以保证访问日志对磁盘空间的合理使用。
11.24 静态元素过期时间
浏览器访问网站的图片时会把静态的文件缓存在本地电脑里,这样下次再访问时就不用去远程下载了,同样可以设置这些静态文件的过期时间,超出后会重新下载。
修改虚拟主机配置文件
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<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>
开启expires_module模块
测试结果
出现Cache-Control,说明配置已经生效。
标签:tupian,HTTP,静态,88.5,访问,日志,com,def From: https://blog.51cto.com/u_13622854/5754607