Nginx closes connection while PHP-Fpm is processing

fastcginginxPHPphp-fpmUbuntu

I have a PHP script that does some data processing. The time that it takes to the script to finish all the job is based on the user input. It's not unusual that it takes 5-15 minutes of processing.

On apache everything works fine, however on Nginx the connection just got closed. When the processing time is low, everything is fine. The problem comes on the long requests…

There is no error in none of the logs. I suspect that the browser closes the connection.

I sniffed the headers, and it appears that Nginx doesn't send any response, until the processing is done. Not a single header.

I tried sending headers from the script, but it seems that nginx caches them all.

Afterwards, I disabled PHP output buffering and enabled implicit flush. I disabled gzip compression and zlib. I minimize FAST_CGI_BUFFERS to the minimum allowed, which is:
fastcgi_buffers 2 1k;
fastcgi_buffer_size 1k;
fastcgi_busy_buffers_size 1k;
fastcgi_max_temp_file_size 0;

Still, it refuse to send headers…

So just to prove my case, in one of the loops inside the code, I did a var_export to one of the objects…

BOOM!!! – it works…

Can anyone think of a more elegant solution?

Thanks.

Update August 19th

Something crazy is going on. After I tried debugging it for 2 full days without any luck, I tried something I should've done before.

I tried this config on Linode, and it works!!!

So probably the problem is somewhere in Ubuntu config… Have no idea where to look.

Best Answer

You have to play around with the timeouts of the server. Nginx is closing the connection because it believes that it's finished and silently closes the connection. There are four directives that come to my mind which could do the trick for you.

client_body_timeout (default is 65)
client_header_timeout (default is 65)
keepalive_timeout (default is 65)
send_timeout (default is 65)

All values are in seconds. Please report back if any of these did the trick (personally I believe it should be keep alive, but please test).

Related Topic