How to disable output buffering in nginx for PHP application

Leagsaidh Gordon picture Leagsaidh Gordon · Aug 28, 2012 · Viewed 16.1k times · Source

We have code similar to this:

<?php
    ob_implicit_flush(true);
    ob_end_flush();

    foreach ($arrayOfStrings as $string) {
        echo time_expensive_function($string);
    }
?>

In Apache, this would send each echo to the browser as it was output. In nginx/FastCGI however, this doesn't work due tot he way nginx works (by default).

Is it possible to make this work on nginx/FastCGI, and if so, how?

Answer

mad picture mad · Jul 29, 2014

First php has to correctly flush everything :

@ob_end_flush();
@flush();

Then, I found two working solutions:

1) Via Nginx configuration:

fastcgi_buffering off;

2) Via HTTP header in the php code

header('X-Accel-Buffering: no');