Enable slow logging configuration in nginx php fpm (to detect slow execution of PHP scripts)

  • 2020-05-06 12:18:50
  • OfStack

Many webmasters suffer from 500,502 problems after switching to nginx+ php-fpm. When nginx receives the above error code, it can be determined that there is something wrong with php-fpm parsing php at the back end, for example, an execution error, an execution timeout.

The php-fpm.conf configuration file has a parameter request_slowlog_timeout which is described as


; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_slowlog_timeout = 0

When request_slowlog_timeout is set to a specific second, request_slowlog_timeout =5, this means that if a script takes more than 5 seconds to execute, the script is logged to

in a slow log file

request_slowlog_timeout =0 means that slow log output is turned off.

The slow log file location defaults to the log folder in the php installation directory and can be specified by changing the slowlog = log/$pool.log.slow parameter.

An example of slow logging is php-fpm. Slow logging records the process number, the script name, and the specific file and line of code that took too long to execute.

[21-Nov-2013 14:30:38] [pool www] pid 11877
script_filename = /usr/local/lnmp/nginx/html/www.quancha.cn/www/fyzb.php
[0xb70fb88c] file_get_contents() /usr/local/lnmp/nginx/html/www.quancha.cn/www/fyzb.php:2
[21-Nov-2013 14:15:23] ERROR: [pool www] 'slowlog' must be specified for use with 'request_slowlog_timeout'

request_slowlog_timeout and slowlog need to be set at the same time. When request_slowlog_timeout is turned on, slowlog
needs to be turned on

[21-Nov-2013 14:16:27] ERROR: Unable to create or open slowlog(/usr/local/lnmp/php/log/www.log.slow): No such file or directory (2)

The slow log path requires manual creation of

Specific steps to open php-fpm slow logging:


cd /usr/local/lnmp/php

vi etc/php-fpm.conf
 To get rid of request_slowlog_timeout  , slowlog Prefix semicolon ';' To set up request_slowlog_timeout =5 ; 
:wq
 Save the exit 
 Create a slow log directory 
mkdir log
 restart php-fpm
kill -INT `cat var/run/php-fpm.pid
sbin/php-fpm


Related articles: