【问题标题】:How to pass nginx HTTP status codes (in access logs) to php.如何将 nginx HTTP 状态代码(在访问日志中)传递给 php.ini
【发布时间】:2016-09-19 23:59:14
【问题描述】:

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

【问题讨论】:

  • 您应该(最好)使用搜索引擎 + cron 作业(用于索引),从日志中索引数据,例如:ElasticSearch。这个github.com/elastic/elasticsearch-php 有一个 php 库
  • 但是在处理您的 php 代码时连接没有关闭。只有当它发送响应,然后再发送给客户端时,连接才会关闭,在它生成之前如何访问它?

标签: php laravel http nginx web


【解决方案1】:

您必须使用 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.
}

?>

确保当前脚本具有读取访问日志文件的权限。

【讨论】:

    猜你喜欢
    • 2020-12-14
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    相关资源
    最近更新 更多