Php – Nginx + FastCGI + PHP (php-fpm) not logging caught errors/warnings

fastcginginxPHP

FastCGI doesn't want to log PHP errors properly. Well, that's not entirely true: it logs errors fine, with a little fiddling; it just won't log anything else, such as warnings.

The notorious FastCGI -> Nginx log bug isn't an issue, necessarily. Errors and warnings from php-fpm go straight to Nginx–but only if they're uncaught. That is, if set_error_handler successfully intercepts an error, no log entry is appended. This means that I can see parse errors, but that's about it.

php-fpm doesn't log PHP errors by itself (separate from nginx) without a bit of a hack. php-fpm's instance configuration file includes these two lines by default:

php_admin_value[error_log] = /mnt/log/php-fpm/default.log
php_admin_flag[log_errors] = on

I changed the error_log path, obviously. I had to add the following line to get it to actually log anything:

php_admin_value[error_reporting] = E_ALL & ~E_DEPRECATED & ~E_STRICT

Version note: the E_STRICT part is unnecessary, as I'm using PHP 5.3.27, but I plan on upgrading to 5.4 at some point. With this line, it logs errors–and only errors–to /mnt/log/php-fpm/default.log. Now, this sets error_reporting to the same value that I have set in php.ini, so something is obviously wrong here. In addition, it doesn't log caught errors: the behavior is identical to that of the nginx log. I tried using the numeric value (22527) instead, but still no luck.

I don't care in which log file the entries end up (nginx versus php-fpm), but I do need caught errors to be logged somewhere. I could resort to injecting my own error and exception handlers, but that's a bit hackish, so I'd rather avoid that.

Best Answer

I use this directive in the pool configuration file for PHP-FPM:

catch_workers_output = yes

Related Topic