Reputation: 131
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?
Upvotes: 2
Views: 3882
Reputation: 61
You should set HTTP response header 'X-Accel-Buffering' to 'no' when you don't want the Nginx to buffer the response from FastCGI server.
http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_buffering
Upvotes: 6