nginx php-fpm响应慢排查

目录

方法一:通过 nginx 日志记录响应时间

修改 nginx.conf,在日志格式中加入 $request_time$upstream_response_time

log_format access '$remote_addr - $remote_user [$time_local] "$request" '
                  '$request_time $upstream_response_time '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" $http_x_forwarded_for';
  • $request_time:nginx 从接收请求到发送响应的总耗时
  • $upstream_response_time:后端 PHP-CGI 的响应耗时(一般更短)

若两者接近,说明慢点在 PHP;若前者明显更长,可能是上传数据量大或网络问题。

方法二:php-fpm 慢查询日志

PHP 5.3.3 之后在 php-fpm.conf 中配置:

request_slowlog_timeout = 1s
slowlog = /usr/local/php/log/php-fpm.log.slow

也可设置执行超时:

request_terminate_timeout = 10s

慢查询日志会输出堆栈信息,帮助定位问题,例如可能发现程序死锁(flock 互等)。

方法三:性能监测工具

可使用 xhprof 等工具进行精准定位,具体见php性能监测模块XHProf安装与测试

#nginx #php