Curl: transfer closed with outstanding read data remaining
When curl returns a "transfer closed with outstanding read data remaining" error, it is signaling a mismatch between what the server promised and what it actually delivered. This specific error usually manifests when using HTTP/1.1 with chunked encoding or large payloads. The client has received a response header indicating that more data is coming (Transfer-Encoding: chunked), but the connection abruptly terminates before the full body arrives.
Looking at your logs, the server responds with HTTP/1.1 200 OK, sets Content-Type: text/html; charset=utf-8, and explicitly includes Transfer-Encoding: chunked. This tells curl to expect a stream of data where the length is unknown until the final empty chunk arrives. However, the error nread <= 0, server closed connection indicates that the TCP socket was reset or closed by the peer while curl was waiting for these subsequent chunks.
The root cause in your specific scenario lies in how PHP's curl extension handles timeouts versus the application logic running on the server side. You have configured extremely aggressive timeouts:
curl_setopt($connection, CURLOPT_CONNECTTIMEOUT ,550000000);
curl_setopt($connection, CURLOPT_TIMEOUT, 5500000000);
These values (5.5 billion seconds) effectively disable time-based disconnects on the client side. While this prevents curl from giving up due to network latency, it leaves the request hanging indefinitely if the server decides to drop the connection for any reason.
The server logs show Server: nginx/1.8.0. Nginx, by default, has a proxy_read_timeout or fastcgi_read_timeout (depending on whether you are proxying or calling a backend) that defaults to 60 seconds. If your PHP script is generating the HTML dynamically and takes longer than this server-side timeout threshold to execute, Nginx will close the connection abruptly.
When the client (curl/PHP) tries to read the next chunk after the server closes the socket, the underlying file descriptor returns an error or EOF immediately. Since curl expects more data based on the chunked transfer encoding, it flags the remaining expected bytes as "outstanding" and aborts with the specific error you are seeing. The partial delivery of content confirms that the header was received, but the body stream was interrupted mid-transmission.
Handling Chunked Responses Safely
When dealing with large responses or server-side timeouts, relying on Transfer-Encoding: chunked without a defined length can be risky if the upstream connection is unstable. If you control the client logic, you should consider forcing curl to treat the response as having a known size or handle the incomplete read gracefully.
You can disable the automatic handling of chunked encoding and force curl to return the raw data it received, which allows your application to detect exactly where the transfer failed