Search code examples
phpnginxfastcgi

How to disable output buffering in nginx for PHP application


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 to the way nginx works (by default).

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


Solution

  • 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');