Skip to content

Instantly share code, notes, and snippets.

@huanle0610
Created October 7, 2015 12:57
Show Gist options
  • Save huanle0610/c844708a92f1a4150c20 to your computer and use it in GitHub Desktop.
Save huanle0610/c844708a92f1a4150c20 to your computer and use it in GitHub Desktop.
parse nginx log to get primary script unknow requests
<?php
$file = '/var/log/nginx/YOURDOMAIN.error.log';
$fd = fopen ($file, "r");
while (!feof ($fd))
{
$buffer = fgets($fd, 4096);
$lines[] = $buffer;
}
fclose ($fd);
var_dump(count($lines));
$urls = array('items' => array());
$list_all = array();
foreach($lines as $one)
{
if(preg_match('/"Primary script unknown"(?:.*?)request: "(.*?) (.*?) (.*?)"/', $one, $match))
{
$list_all[] = array(
'method' => $match[1],
'url' => $match[2],
'protocol' => $match[3],
);
}
}
//$list = array_unique($list_all);
$list = $list_all;
$urls['totalCount'] = count($list);
$urls['items'] = $list;
//var_dump($urls);
file_put_contents('hk.json', json_encode($urls));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment