I'm trying to execute a PHP script which during execution provides the current status/percentage of completed actions. For do that I call the script with AJAX and I use the ob_flush for send the output to the client during script execution, it worked on Apache but now I'm porting the project on Nginx and I need compatibility for both. On Nginx I use PHP-FPM for handle PHP files, here's the test script which I'm trying to run correctly:
//SET CORRECT CONTENT-TYPE (THEY WILL BE JSON STRINGS SEPARED BY BREAKLINE)
header('Content-type: text/plain; charset=utf-8');
//DISABLE GZIP FOR THE SCRIPT
ini_set('zlib.output_compression', 'Off');
ini_set('output_buffering', 'Off');
ini_set('output_handler', '');
ob_end_clean();
set_time_limit(0);
for ( $i = 0 ; $i < 5 ; $i++ ){
echo "{\"code\":" . $i . "}\n";
//SEND OUTPUT TO CLIENT
flush();
ob_flush();
sleep(1);
}
The problem is that when I run this I get the output just when the script ends the execution, so after about 5 seconds. Someone can tell me what I'm doing wrong?
str_repeat(' ', 4096 * 8), it's a very inefficient and inelegant way to use flushing but it appears to be the only one with NGINX, anyway I have to give a look to thefastcgi_keep_conn on;" directive in the server configuration block in NGINX configuration file, maybe it can allows me to use normal PHP flush.