Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable output buffering in nginx for PHP application

Tags:

php

nginx

fastcgi

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?

like image 739
Leagsaidh Gordon Avatar asked Aug 28 '12 19:08

Leagsaidh Gordon


People also ask

What is output buffering in PHP INI?

Output buffering is a mechanism for controlling how much output data (excluding headers and cookies) PHP should keep internally before pushing that data to the client. If your application's output exceeds this setting, PHP will send that data in chunks of roughly the size you specify.


1 Answers

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');
like image 183
mad Avatar answered Sep 28 '22 16:09

mad