Home >php教程 >PHP源码 >用php写了个正则解析nginx的access log

用php写了个正则解析nginx的access log

PHP中文网
PHP中文网Original
2016-05-25 17:10:351763browse

对于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 = &#39;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"&#39;;
					 
$p = &#39;/^(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})\s-\s(.*)\s\[(.*)\]\s"(.*)\"\s(\d{3})\s(\d+)\s"(.*)"\s\"(.*)\"(.*)$/u&#39;;

preg_match($p,$s_line,$a_match);
var_dump($a_match); 

//还不知道ipv6的状况会如何

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn