如何通过nginx HTTP状态码(在访问日志)到php


How to pass nginx HTTP status codes (in access logs) to php.

我试图找到客户端关闭浏览器连接(499 HTTP状态)的时间。我可以看到那些在nginx访问日志,但我不知道如何从php访问。

你必须用PHP脚本解析你的nginx访问日志文件,并使用cron或像守护程序一样运行。

示例如下:

<?php
$handle = fopen("/path/to/your/access.log", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
      if(strstr($line, 'HTTP/1.1" 499')){
        echo $line;
      }
    }
    fclose($handle);
} else {
    // error opening the file.
}
?>
请确保当前脚本具有读取访问日志文件的权限。