5

I’m using nginx with PHP-FPM (APC is installed).

I need PHP’s flush() to work. Is this possible?

Things I’ve tried so far:

  • Disabling all output buffering in php.ini, as well as output compression.
  • Disabling gzip in nginx’s configuration.
  • Setting nginx’s fastcgi_* buffering settings and fastcgi_max_temp_file_size to zero.

I’m sure I must be missing something, since I’ve run across many posts here and elsewhere where people claimed they’ve got it working, but I’m having no luck, it would seem.

Abraham Vegh
  • 1,045
  • 5
  • 17
  • 26
  • BTW I noticed my php.ini, while the default value is "off" there was still an uncommented "output_buffering = 4096" in there. Anyway, great question! – PJ Brunet Nov 12 '13 at 17:53
  • 1
    Apparently it's possible to do this, without disabling gzip, as of Nginx 1.5.6 with the new "fastcgi_buffering" directive https://twitter.com/mdounin/status/400259731308032000 – PJ Brunet Nov 12 '13 at 17:56

4 Answers4

4

Sadly it's not possible with nginx. The nginx implementation of fastcgi requires a buffer to be in place, even if you set the buffering directive to 0 it will just cause it to buffer to disk instead of memory.

You'll have to figure out some alternative system such as a queue where you can poll for the status. (think ala gearman for instance)

Edit: This is since possible: http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_buffering

Martin Fjordvald
  • 7,749
  • 1
  • 30
  • 35
  • There’s really no way to completely disable the buffer? My reading of the nginx wiki/docs seems to suggest that the disk cache is the easiest thing to shut off, but the memory cache is still in play, despite various attempts to shut it down too. But maybe I’m reading it wrong… – Abraham Vegh Mar 18 '13 at 10:25
  • 1
    There is for pretty much everything but fastcgi. uwsgi has http://wiki.nginx.org/HttpUwsgiModule#uwsgi_buffering proxy has http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffering. but the same does not exist for fastcgi. – Martin Fjordvald Mar 18 '13 at 15:06
4

It's possible. Put this in the server section of your site's config file:

gzip off;
proxy_buffering off;
fastcgi_keep_conn on;
fastcgi_max_temp_file_size 0;
fastcgi_buffering off;

Put this in php.ini

output_buffering = Off
implicit_flush = On

Be careful. Disabling these buffers can have severe performance implications.

3

I wanted the same thing, and it turns out it is possible. All you need is this before anything is echo'ed:

header('Content-Encoding: none;');

Then to flush you do the flush as normal:

ob_end_flush();
flush();

Nginx seems to pick up on the encoding having been turned off and doesn't gzip.

Redzarf
  • 131
  • 3
  • If anyone gets progressive gzip working, please email me pj@pjbrunet.com or just reply here. But I can affirm this works (without gzip) wow! Now my css and js files load immediately. According to this http://www.phpied.com/progressive-rendering-via-multiple-flushes/ progressive gzip rendering is possible. But with Apache and not NginX? It's a fascinating link but lacks in-depth implementation details :-/ – PJ Brunet Nov 12 '13 at 13:37
  • Also I wonder if there's a "no-gzip" encoding option that could improve this answer? – PJ Brunet Nov 12 '13 at 17:55
0

It IS possible, look at this answer.

I tried it myself and it works.


Upgrade your nginx server {} config:

fastcgi_keep_conn on; # < solution

proxy_buffering off;
gzip off;
C. S.
  • 1