How to disable output buffering in PHP | Nginx when using RunCloud?

How to disable output buffering

How to disable output buffering in PHP 

Hello, If your website contains functions that must be run without buffering, you may need to disable PHP's output buffering option. 

If you are still confused regarding output buffering then check here

"Output buffering is a mechanism in which instead of sending a response immediately to browser we buffer it somewhere so that we can send it at once when whole content is ready."


Step 1 

You should make the adjustments listed below on the RunCloud side. First, we must remove it from your PHP version's main php.ini file (for example, /etc/php74rc/php.ini). Please ensure that the values for "output buffering" in the php.ini file are commented as shown below. 

cat /etc/php74rc/php.ini | grep output_buffering ; output_buffering ;output_buffering = 4096 ; performance, enable output_buffering in addition.

Step 2

Make sure to restart php-fpm like below

service php74rc-fpm restart
 

Step 3

To prevent buffering, we must add the following Nginx configuration as the type location.main. 

location ~ .php$ { 
include fastcgi_params; 
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_pass unix:/var/run/notboda-forresthatfield-com.sock;
fastcgi_buffer_size 1k;  
fastcgi_buffers 128 1k; # up to 1k + 128 * 1k
fastcgi_max_temp_file_size 0; 
gzip off; 
}

Remember to replace WEBAPP NAME with the name of your web application. It should be operating normally as of right now.

Verification

To verify that output buffering has been successfully disabled, you can use the following code snippet:

 <?php
echo 'Testing output buffering...';
flush(); // Flush the output buffer
ob_end_flush(); // Disable output buffering
echo 'Output buffering is disabled!';
?>

When you run this code, you should see the output "Testing output buffering...Output buffering is disabled!" displayed immediately without any delay. This indicates that output buffering has been disabled and the output is sent directly to the browser as it is generated.

Remember to remove or comment out this verification code once you have confirmed that output buffering is disabled, as it is only intended for testing purposes.

I hope this was helpful :)

Comments