天天看點

php nginx日志分析,php解析nginx日志的執行個體代碼

本節内容:

php解析nginx日志

1,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"';

2,Nginx日志參數

server_name          : 虛拟主機的主機名稱

remote_addr          : 遠端用戶端的ip位址

remote_user          : 遠端用戶端使用者名稱

time_local           : 通路的時間與時區

status           : 記錄請求傳回的http狀态碼

body_bytes_sent      : 發送給用戶端的檔案主體内容的大小

http_referer             : 從哪個頁面連結通路過來

http_user_agent      : 用戶端浏覽器資訊

http_x_forwarded_for     : 用戶端的真實ip

3,Nginx日志分割符

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

4,根據關鍵字過濾檔案内容

需求

根據http的請求裡是否有“weibo”這個關鍵字提取檔案的内容

例子:

複制代碼 代碼示例:

function readFileContent ($filename)

{

$weibo_content = array();

$fh = @fopen($filename, 'r');

www.jbxue.com

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;

}