PHP-FPM Slowlog for Slow Requests - Capture a Stack Snapshot, Not a Verdict
A request log can tell you that a PHP page took four seconds. It cannot, by itself, tell you whether the worker was building a large array, waiting for a database response, reading a file, or calling another service. Adding timers around every possible operation may eventually answer the question, but first you need a useful place to look.
PHP-FPM's slowlog can narrow that search. When a request remains in a pool worker longer than a configured threshold, FPM can write a PHP backtrace to a separate file. That trace is valuable precisely because it is limited: it is a snapshot of where execution was at one moment, not a profiler, a timeout, or a verdict about root cause.
What the slowlog actually records
The official FPM configuration documentation defines three relevant pool directives:
slowlogselects the log file for slow requests.request_slowlog_timeoutsets the elapsed request-serving time after which FPM dumps a PHP backtrace to that file. A value of0turns the trigger off.request_slowlog_trace_depthlimits the stack depth; its documented default is 20.
The trigger accepts time units such as seconds, minutes, hours, and days. It belongs to an FPM pool, so different pools can use different paths and thresholds. The example pool file in the official php-src repository also notes that a slowlog path is required when the timeout is enabled.
A backtrace answers a narrow question: where was the PHP call stack when this request crossed the threshold? If several traces repeatedly stop near the same database call, template function, filesystem operation, or HTTP client wrapper, that is a strong lead. It is still only a lead. The visible function may be waiting on a dependency, and the underlying delay may sit outside PHP.
Start with a small observation window
The following pool fragment demonstrates the documented syntax. The path, pool name, and three-second threshold are examples, not defaults or universal recommendations:
slowlog = /var/log/php-fpm/www-slow.log
request_slowlog_timeout = 3s
request_slowlog_trace_depth = 20
access.log = /var/log/php-fpm/www-access.log
access.format = "%{%Y-%m-%dT%H:%M:%S%z}t pid=%p method=%m uri=%r status=%s duration_ms=%{milliseconds}d memory_kb=%{kilobytes}M"
Choose a threshold from the application's own latency expectations. Three seconds may be far too generous for an interactive endpoint and far too strict for an intentionally long export. A threshold that catches ordinary healthy traffic creates noise; one above the failure users notice may miss the useful moment.
File locations and validation commands differ among distributions and PHP packages. Before reloading FPM, identify the pool file and binary used by the running service, validate the complete configuration with that installed binary or the package's documented procedure, and keep a separate administrative session available. A copied configuration fragment is not proof that the surrounding configuration is valid.
Pair the trace with an identity and a duration
A stack snapshot is easier to use when you can connect it to a particular request. FPM's optional access log can include the worker PID (%p), request duration (%d with a chosen precision), method, URI, response status, and peak PHP memory. The example above records enough context to compare a slowlog event with a completed request without putting the query string into that format.
The PID is especially useful when timestamps are close and several workers are active. The URI identifies the route or front controller path visible to FPM, while the duration establishes whether the request merely crossed the threshold or remained slow for much longer. Peak memory can add context, but high memory and high latency occurring together do not prove that one caused the other.
NGINX may provide another time measurement. Its $request_time documentation defines the interval from reading the first request bytes until the log write after the last response bytes are sent. FPM's duration concerns the request handled by the PHP pool. Those clocks answer related but different questions: web-server time can also include request intake and response delivery. Treat a difference between them as a clue about boundaries, not as a precise subtraction formula.
Read a stack snapshot without turning it into a story
It is tempting to read the last visible application frame and declare it guilty. A more cautious reading separates observation from interpretation:
- Observation: the request crossed the configured threshold, and the captured stack included a particular function chain.
- Interpretation: the worker may have been spending or waiting for time in that part of the application.
- Next evidence: inspect repeated traces, application logs, database slow-query data, upstream timing, filesystem health, or a purpose-built profiler as appropriate.
One trace can be unrepresentative. A scheduled task may briefly contend for disk access; a remote API may have one delayed response; a cache may be cold. Repeated traces with the same shape are more persuasive, but frequency still does not establish causality. Conversely, varied stacks can indicate several independent slow paths or simply show that requests were sampled at different moments.
The slowlog is therefore closer to a photograph taken when a timer rings than to a video of the entire request. It does not show how much time every function consumed. If the question is “which functions dominate CPU time across thousands of requests?”, sampling or instrumentation designed for profiling is a better fit.
Slow logging is not request termination
The similar names can hide an important operational distinction. request_slowlog_timeout produces diagnostic output after its threshold. It does not mean the worker is killed at that point.
request_terminate_timeout is the separate FPM directive that can terminate a worker serving one request after its configured limit. The PHP manual describes it separately, including its relationship to cases where PHP's max_execution_time does not stop execution.
Do not set a termination value merely because a slowlog threshold seems reasonable. Observation and forced termination have different failure consequences. Killing a worker can interrupt a write, leave an external operation uncertain, or turn a slow response into an error. Any termination policy needs application-specific analysis of transactions, idempotency, cleanup, queues, and upstream timeouts.
Use the FPM status page as aggregate context
The FPM status page includes a slow requests counter: the number of requests in that pool that have hit the configured slowlog threshold. It can help answer whether an event is isolated or recurring. The values are pool-specific and reset when FPM restarts, so the counter needs a time window and restart context before it means much.
Full status output can also expose process state, request duration, URI, script path, CPU, and memory information. PHP's documentation explicitly warns that the endpoint should be restricted to internal requests or known client addresses because it reveals request URLs and resource information. It should not become a public debugging page.
Logs create their own operational risks
Diagnostic visibility is not free. Slowlog traces reveal application structure and filesystem paths. Access logs can reveal routes, client information, and, if the format includes them, query strings or selected headers. Restrict file ownership and permissions, decide how long the data remains useful, include it in log rotation and disk monitoring, and avoid fields carrying credentials or sensitive parameters.
Logging also has a cost. The right response is not to assume that the cost is either negligible or disastrous, but to observe it on the actual workload. A narrow investigation window is often easier to reason about than enabling a very low threshold indefinitely. If continuous slow-request monitoring is needed, define retention, alerting, and review rules rather than accumulating a file nobody reads.
A cautious diagnostic sequence
- Define what “slow” means for the route being investigated and select an initial threshold accordingly.
- Choose a protected slowlog path, a suitable trace depth, and a minimal FPM access-log format for correlation.
- Validate the complete FPM configuration using the installed package's supported method before reloading it.
- Observe a bounded period or reproduce the problem with safe, representative traffic.
- Match threshold events by time, PID, URI, and duration. Look for repeated stack shapes rather than promoting the first frame to a conclusion.
- Gather evidence from the boundary implicated by the traces: database, filesystem, outbound network, application algorithm, or pool saturation.
- After the investigation, raise or disable the trigger if its continuing value does not justify the data and noise it produces.
This sequence leaves room for the slowlog to be inconclusive. That is not failure. Ruling out one layer, discovering that traces vary, or learning that NGINX time and FPM time diverge can still narrow the next question.
A snapshot is useful when its limits stay visible
PHP-FPM's slowlog fills a practical gap between “the request was slow” and full application profiling. It can capture the PHP stack at the moment a request crosses a chosen threshold, while the FPM access log, NGINX timing, status counters, and dependency-specific evidence provide context.
Its best use is modest: identify a promising place to investigate, collect repeated evidence, and remain willing to revise the explanation. A backtrace can point toward the room where the delay was observed. It does not tell you, on its own, who moved the furniture.
