Use Fastcgi_finish_request to improve page response speed

Source: Internet
Author: User
Tags nginx server

When PHP is running in fastcgi mode, PHP FPM provides a method called Fastcgi_finish_request. As the document says, this method can increase the processing speed of requests, which can be used if some processing can be done after the page has been generated.

Example

Echo ' Fastcgi_finish_request test '; fastcgi_finish_request (); /*  */echo ' request end '; // will not be output /*  */ile_put_contents (' log.txt ', ' Survival or destruction, this is a question ');

This script is accessed through a browser, and the result is that the corresponding string is not output, but the corresponding file is generated. This means that after calling Fastcgi_finish_request, the client response is over, but the server script continues to run!

Reasonable use of this feature can greatly enhance the user experience:

Example

Echo ' Fastcgi_finish_request test 'file_put_contentsdate(' y-m-d h:i:s '). "Upload video \ n",sleep(1); file_put_contents Date (' y-m-d h:i:s '). "Convert format \ n",sleep(1); file_put_contents Date (' y-m-d h:i:s '). "Extract picture \ n", file_append);

The code uses sleep to simulate some time-consuming operations, browsing without being blocked, the program is executed, the specific look at the log.

Disadvantages of the Fastcgi_finish_reques () function:
1.PHP FastCGI process is limited, processing the php-cgi process of asynchronous operation, unable to process the new request;
2. If the number of concurrent accesses is large, the php-cgi process is full, and the new access request is not php-cgi to be processed. Nginx server will appear: 502 Bad Gateway.

Use Fastcgi_finish_request to improve page response speed

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.