对于nginx的日志分析,awstats + jawstats 是一个很好的解决方案
最近有个任务需要对接口的调用做二次分析统计,于是用php写了个正则解析日志,用Highcharts报表,
nginx.conf 对log的默认格式是这样定义的
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
每行的记录大概如下
192.168.192.168 - - [31/Jul/2012:14:17:45 +0800] "GET /a/b/c/d.txt?device_id=BF771F68-6B0C-41D0-9F7E-3A24294B17DF HTTP/1.0" 200 1039 "-" "LifeStyleTiring-Room/1.0 CFNetwork/548.1.4 Darwin/11.4.0"
每天用crontab把记录切割并解析,每次读入100行,提取有用部分预处理存入sql,为报表做准备
目前数据量不多,还没有做性能测试
1. [代码]用php解析nginx的访问日志
<?php $s_line = '192.168.192.168 - - [31/Jul/2012:14:17:45 +0800] "GET /a/b/c/d.txt?device_id=BF771F68-6B0C-41D0-9F7E-3A24294B17DF HTTP/1.0" 200 1039 "-" "LifeStyleTiring-Room/1.0 CFNetwork/548.1.4 Darwin/11.4.0"'; $p = '/^(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})\s-\s(.*)\s\[(.*)\]\s"(.*)\"\s(\d{3})\s(\d+)\s"(.*)"\s\"(.*)\"(.*)$/u'; preg_match($p,$s_line,$a_match); var_dump($a_match); //还不知道ipv6的状况会如何