Apache配置——日志切割
切割自动归档(避免大文件生成,且定期删除旧的日志)## 主配置文件中指定日志格式
# vim /usr/local/apache2/conf/httpd.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
解释说明:
Referer父请求
User-Agent浏览器标识
combined 日志格式名称,可自定义
http://note.youdao.com/yws/res/3546/WEBRESOURCEcaf9673bb5315d7411b119c4af16468a
## 虚拟主机配置文件中配置使用主配置文件的日志格式
# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
ErrorLog "logs/test.com-error_log"
CustomLog "logs/test.com-access_log" combined
解释说明:
logs这是相对路径,是放在apache2路径下
http://note.youdao.com/yws/res/4786/WEBRESOURCE7faa8411886d912b4802a4ccab776478
## 检查并重新加载
# /usr/local/apache2/bin/apachectl -t
Syntax OK
# /usr/local/apache2/bin/apachectl graceful
## 首先访问下网页,再查看生成的日志文件
# cd /usr/local/apache2/logs/
# ls
access_logerror_loghttpd.pid test.com-access_logtest.com-error_log
## 查看日志文件内容
# cat test.com-access_log
192.168.219.1 - - [04/Nov/2016:07:44:52 +0800] "GET /forum.php?mod=ajax&action=forumchecknew&fid=2&time=1478215305&inajax=yes HTTP/1.1" 200 64 "http://www.test.com/" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)"
%h 来源ip,客户端ip
%l %u(用户) 在这两个不存在,就用- -代替了
%t 时间
%r 动作
Referer 访问地址的时候,它从哪里来的
User-Agent浏览器标识
## 日志切割
ErrorLog "logs/test.com-error_log"
CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test.com-access_%Y%m%d_log 86400" combined
解释说明:
|/usr/local/apache2/bin/rotatelogs -l 日志切割工具rotatelogs,此文件是一个可执行文件,-l时间格式参数
/usr/local/apache2/logs/test.com-access_%Y%m%d_log 最好是绝对路径
86400 一天多少秒钟
combined 自定义的访问日志格式
http://note.youdao.com/yws/res/4794/WEBRESOURCE16cf6bd599ccfb52938bacde23fb4769
## 检查并重新加载
# /usr/local/apache2/bin/apachectl -t
Syntax OK
# /usr/local/apache2/bin/apachectl graceful
## 首先访问下网页,再查看生成的日志文件
# ls /usr/local/apache2/logs/
access_logerror_loghttpd.pidtest.com-access_20161104_logtest.com-access_logtest.com-error_log
页:
[1]