Home  >  Article  >  Backend Development  >  php脚本解析nginx日记

php脚本解析nginx日记

WBOY
WBOYOriginal
2016-06-13 11:13:04803browse

php脚本解析nginx日志

nginx日志格式


access_log日志格式

log_format  main  '$server_name$remote_addr$remote_user[$time_local]"$request"'                  '$status$body_bytes_sent"$http_referer"'                  '"$http_user_agent""$http_x_forwarded_for"';

日志参数

server_name 	  	 : 虚拟主机的主机名称remote_addr 		 : 远程客户端的ip地址remote_user 	 	 : 远程客户端用户名称time_local  		 : 访问的时间与时区status    		 : 记录请求返回的http状态码body_bytes_sent 	 : 发送给客户端的文件主体内容的大小http_referer	         : 从哪个页面链接访问过来 http_user_agent 	 : 客户端浏览器信息http_x_forwarded_for     : 客户端的真实ip

日志分割符

使用特殊的不可打印字符^A(ctrl+v,ctrl+a)作为日志分割符



根据关键字过滤文件内容


需求

根据http的请求里是否有“weibo”这个关键字提取文件的内容


php代码

    /**     * Description:按行读取文件内容进行过滤匹配     *     * @return array     */    function readFileContent ($filename)    {        $weibo_content = array();        $fh = @fopen($filename, 'r');                if ($fh) {            while (! feof($fh)) {                $row = fgets($fh, 4096);                $row_arr = explode("", $row);                if (isset($row_arr[3]) && preg_match('/weibo/', $row_arr[3])) {                    $weibo_content[] = $row_arr;                }            }        }        fclose($fh);                return $weibo_content;    }



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