Enable slow logging configuration in nginx php fpm (for detecting slow execution of PHP scripts)
- 2020-05-10 23:11:23
- OfStack
Many webmasters who switch to nginx+ php-fpm suffer from 500,502 problems. When nginx receives the above error code, it can be determined that something is wrong with php-fpm parsing php at the back end, for example, execution error, execution timeout.
The configuration file for php-fpm.conf has a parameter request_slowlog_timeout as described below
; 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 1 concrete second, request_slowlog_timeout =5, indicating that if any script takes more than 5 seconds to execute, the script will be logged to a slow log file
request_slowlog_timeout =0 means 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.
php-fpm example of slow logging. Slow logging records the process number, the script name, and the specific file and line of code which function 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, while request_slowlog_timeout needs to be 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)
Slow log paths need to be created manually
Specific steps to start 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