NGINX+PHPFASTCGI flush all of a sudden output problem

Source: Internet
Author: User

Recently, due to business needs, you need to use the flush output cache refresh of PHP to handle the browser timeout problem.

The initial test code is as follows:

Ob_start ();//Open buffer
for ($i =10; $i >0; $i--)
{
echo $i. ' <br/> ';
Ob_flush ();
Flush ();
Sleep (1);
}
Ob_end_flush ();//output and turn off buffering
Die ();
I thought it would be 1 seconds to output a number, but in the nginx+php environment, the actual result is not the same as expected, the result is 10 seconds after the data output at once.

Previously operating normally in Apache, the browser output cache was dynamically refreshed every 1 seconds, because no gzip was turned on in Apache. Tangled up for a long time, the internet looked for a long time problem, the final solution is as follows:

Header ("content-encoding:none\r\n"); Ob_start ();//Open buffer for ($i =10; $i >0; $i-) {echo str_repeat ("", 1024*64); echo $ I. ' <br/> '; Ob_flush (); flush (); sleep (1);} Ob_end_flush ();//output and close buffered die;

  


Explain why this code is written like this:

First: Header ("content-encoding:none\r\n"), fix nginx in config file open gzip problem, if Nginx gzip configuration item is on, not add this line of code, The result is the same as the program after the completion of a one-time data output; in fact, you can not add this line of code, directly in the Nginx configuration file to turn off gzip, but not because of a small need to change the configuration of Nginx.

Second: Echo str_repeat ("", 1024*64) This line of code is also the focus, at first I did not add this line of code, the final implementation of the effect or a one-time output of the data, caused by Nginx in the fastcgi_buffer_size 64k;
Fastcgi_buffers 4 64k;

These two configurations indicate that Nginx will buffer the information of the PHP-FPM output, the buffer data will be sent to the client when it reaches 64k, because we only output a number, far less than the 64k limit, so before the program output with Echo str_repeat ("", 1024* 64) The 1024*64 value is modified according to the individual nginx configuration.

The last point: Ob_flush () and flush () are used in conjunction, and the flush is the real output before Ob_flush is flush.

NGINX+PHPFASTCGI flush all of a sudden output problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.