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

php脚本解析nginx日记

WBOY
Release: 2016-06-13 12:55:00
Original
867 people have browsed it

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"';
Copy after login

日志参数

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

日志分割符

使用特殊的不可打印字符^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;
    }
Copy after login



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template