NginX issues HTTP 499 error after 60 seconds despite config. (PHP and AWS)
If you are running a PHP application on Nginx, specifically on an AWS instance, you may have encountered a frustrating scenario where requests hang indefinitely before returning an HTTP/1.1 499 error. This status code indicates that the server closed the connection without receiving a response from the upstream FastCGI process (PHP-FPM). While increasing timeouts seems like the obvious fix, simply configuring fastcgi_read_timeout or max_execution_time often fails to resolve the underlying issue.
The problem stems from a hard timeout enforced by Nginx itself, distinct from the PHP execution time. Even if your PHP script runs for 10 minutes and outputs data successfully, Nginx will terminate the connection after its default keepalive read timeout (typically 60 seconds). When this happens, Nginx logs the error in example.error.log with a 499 status, signaling to the client that the upstream timed out.
To understand why standard configuration tweaks fail, consider the flow:
- Nginx accepts the request and waits for output from PHP-FPM.
- PHP-FPM executes the script. Even if
max_execution_timeis set to 360 seconds, Nginx's internal clock keeps ticking. - The Disconnect: If Nginx has not received any data (even a single byte) within 60 seconds, it closes the socket by default.
This behavior can be replicated on fresh AWS Micro instances using PHP 5.4.x or later. A simple test script that sleeps for 70 seconds will trigger this exact failure. You might observe that the script actually completes execution (proven by writing to a log file after the sleep), yet the browser receives a 499 error because Nginx dropped the connection before PHP could send the final output.
Configuring Timeouts: Why It Often Fails
The typical approach involves editing /etc/nginx/nginx.conf and site-specific files to increase timeouts. Developers often set fastcgi_read_timeout, client_header_timeout, and send_timeout to values like 360 seconds. Similarly, the PHP-FPM configuration (php-fpm.d/) is updated with request_terminate_timeout = 120s.
However, as observed in testing environments, simply increasing these values does not always prevent the 499 error if the default Nginx behavior regarding connection draining is active. The critical setting often overlooked is fastcgi_ignore_client_abort (or its variant proxy_ignore_client_abort).
When set to on, this directive tells Nginx: "Do not close the client connection if the upstream process takes longer than expected." Without this, Nginx assumes the request is broken once the read timeout expires and sends the 499 response immediately.
Implementing a Robust Fix
To resolve the issue, you must explicitly instruct Nginx to ignore client aborts during the FastCGI phase. Add the following directives to your location block in the server configuration:
fastcgi_read_timeout 360;
fastcgi_send_timeout 360;
fastcgi_connect_timeout 1200;
fastcgi_ignore_client_abort on;
keepalive_timeout 360;
Setting fastcgi_ignore_client_abort on is the pivotal change. It ensures that Nginx keeps the connection open long enough for PHP to finish its work, even if the client initially appears to have timed out or the upstream is slow. Note that while this prevents the immediate 499 error in logs, you must ensure your browser or proxy (like Apache HAProxy) also handles long-running requests gracefully, as some clients may close their TCP connection before the script finishes.
Verification and Trade-offs
You can verify that PHP settings are being picked up by checking phpinfo(), but remember that Nginx configuration takes precedence regarding connection lifecycles. If you switch from Apache to Nginx, be aware that Apache handles these timeouts differently, often relying on mod_proxy or PHP's internal execution limits alone.
Additionally, be mindful of the double-request issue sometimes seen with Firefox when scripts exceed 60 seconds. This is not a bug in your code but a client-side behavior where the browser resends the request upon detecting a timeout. Ensuring that Nginx does not prematurely close the connection mitigates this by keeping the stream alive until the actual response is generated.
Finally, if you encounter similar issues with Laravel applications or other frameworks hosted on Nginx, ensure your php-fpm.conf and nginx.conf are aligned. While LaravelCompany offers resources for optimizing Laravel deployments, the fundamental principle remains: configure Nginx to be forgiving of long-running PHP processes rather than enforcing a strict 60-second cutoff that breaks valid user workflows.
