【问题标题】:NGINX error log gets disabled when PHP error log is enabled启用 PHP 错误日志后,NGINX 错误日志将被禁用
【发布时间】:2017-07-05 23:20:04
【问题描述】:

我遇到了一个奇怪的行为。我正在使用 PHP-FPM 运行 NGINX。

当我在php.ini 中启用 PHP 错误日志时:

error_log = /var/www/logs/php-scripts.error.log
log_errors = on

错误日志按预期写入:

...
[15-Feb-2017 19:35:28 Etc/UTC] PHP Parse error:  syntax error, unexpected end of file, expecting ',' or ';' in /var/www/html/index.php on line 7

然而,与此同时,我的 NGINX vhost 中配置的错误日志根本没有写入(这是 NGINX 配置片段):

server {
  listen 80;
  server_name  vhost.com;

  access_log   /var/log/nginx/access.log main;
  error_log    /var/log/nginx/error.log; <- this is always empty
}

现在令我惊讶的是,当我禁用 php.ini 中的错误日志时:

; error_log = /var/www/logs/php-scripts.error.log <-- commented out
log_errors = on

NGINX 错误日志写入

...
2017/02/16 12:01:44 [error] 13#13: *27 FastCGI sent in stderr: "PHP message: PHP Parse error:  syntax error, unexpected end of file, expecting ',' or ';' in /var/www/html/index.php on line 7" while reading response header from upstream, client: 172.19.0.2, server: domain.com, request: "GET / HTTP/1.1", upstream: "fastcgi://172.19.0.3:9000", host: "domain.com:8080"

我完全不理解这种行为。两个测井系统之间这种干扰的来源是什么?为什么我不能在php.ini 中启用错误日志,同时写入 NGINX 的错误日志?目前它是一个或另一个。是否有可能克服这个问题?

【问题讨论】:

    标签: php logging nginx


    【解决方案1】:

    PHP 将错误报告到日志文件、syslog(3) 或 stderr,这取决于是否设置了 error_log 以及如何设置。详情请见this document

    nginx 将记录它通过 FastCGI 标准错误流接收到的消息。

    所以,这并不是什么奇怪的行为。

    【讨论】:

    • 如果我理解正确,关键是如果我将 PHP 错误重定向到文件,错误不会发送到 stderr,因此,nginx 将没有任何要读取的流及其错误日志将是空的..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2017-06-10
    • 2012-05-14
    • 2015-04-08
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多