You can flushe all response data to the client and finishes the request to run time consuming tasks with no connection with brower client by fastcgi_finish_request(). This allows for time consuming tasks to be performed without leaving the connection to the client open.
ignore_user_abort(true);//not required set_time_limit(0); ob_start(); // do initial processing here echo $response; // send the response header('Connection: close'); header('Content-Length: '.ob_get_length()); ob_end_flush(); @ob_flush(); flush(); fastcgi_finish_request();//required for PHP-FPM (PHP > 5.3.3) // now the request is sent to the browser, but the script is still running // so, you can continue your post processing below . . . end; // a must especially if set_time_limit=0 is used and the task ends
Below may work without ob_*()
set_time_limit(0); // do initial processing here . . . header('Connection: close'); flush(); fastcgi_finish_request();//required for PHP-FPM (PHP > 5.3.3) // now the request is sent to the browser, but the script is still running // so, you can continue your post processing below . . . end; // a must especially if set_time_limit=0 is used and the task ends
In order to use fastcgi_finish_request(), you should install 'php-fpm' package. Below is the example to install php-fpm on CentOS 7
sudo yum -y install php-fpm
In addition, you should modify php.ini as following:
cgi.fix_pathinfo=0