Output buffering in php and apache

Output Buffering

Output buffering is processing mechanism where the output being generated by a program is held in a place till its size reaches a limit or the generation is complete. Only after, will the output be send to its destination.

In php for example, doing and echo generates some output. Now this output might be buffered. If its not, then the output will appear on your browser as soon as it is generated. If it is buffered then the output of all echo statements is collected and shown to you once the php script has finished execution.

Output buffering is done for various reasons, like speeding up page load time, doing special processing to the generated content etc. However there are many cases when we want to see the output of the program in realtime as it is being generated. For example to monitor the progress of some task.

Buffering Points

In a php+apache setup, output buffering can happen at many places and finding it out can take some time. The execution is done somewhat like this

Browser <===> Apache <===> Php handler <===> Php interpreter/process

Php

Now the output can get buffered at any of the 4 places shown above. We shall ignore the browser for now. The first place where the output can get buffered is php itself. Php has a setting called output_buffering. This can used to specify the amount of data to buffer before sending to it back to apache.

Apache/Webserver

Another place where the output can get buffered is apache or the webserver. Apache can buffer the output to compress it using mod gzip/deflate for example. Compresses reduces content size considerably and saves bandwidth and speeds up page load time by a large factor.

So to disable output buffering in apache, check if gzip or deflate are enabled and active or not. Generally deflate is kept enabled and the compression is controlled via the htaccess files. So check them accordingly.

Php Handlers

Php handlers are the secret hidden place where the content can buffered and the user might not notice ever. Yeah, php handlers like mod_fastcgi/mod_fcgid buffer the content too.

If you have turned off buffering in both apache and php entirely, but still the output is getting buffered, then most likely its the handlers where the output is getting buffered.

Mod fcgid has a directive called FcgidOutputBufferSize that can be used to specify the output buffer size. So if you are using fcgid then set this value to 0 to disable output buffering totally.







Mod fastcgi also buffers output by default and to turn it off use the "flush" option in the Fastcgi settings specified in vhosts block of apache.

Mod_php and Cgi do not buffer output so no issues there.

With nginx the HttpFastcgiModule also buffers content. Check out the documentation for more information.

Last Updated On : 18th June 2013

Subscribe to get updates delivered to your inbox

Leave a comment