Answer the question
In order to leave comments, you need to log in
How to pass header("Connection: close") in nginx when keep-alive is enabled?
Good afternoon, I have a php code that receives a POST request, performs approximately the following actions:
1. Check the validity of the incoming data
2. Enter the data into the database
3. Send out 5 letters (administrators, managers) saying that a new one has arrived application
4. Also sends something to amoCRM
5. Returns JSON data for a pop-up window, saying "Your application was successfully accepted".
The problem is that steps 3 and 4 will take about 5-8 seconds to complete, and after clicking on the "Submit form" button, the user sees the preloader for 5-8 seconds and only then the pop-up. I tried to rework the php logic like this:
1. Check the validity of the incoming data
2. Enter the data into the database
3. Returns JSON data for a pop-up window, saying "Your application has been successfully accepted."
4. Sends out 5 letters (administrators, managers) saying that a new application has arrived
5. Sends something to amoCRM too I
did it using the solution I found here:
stackoverflow.com/questions/138374/close-a -connect...
Works fine on Apace, doesn't work on nginx
My new PHP code:
private function send_response($array = ['response' => 'response', 'success' => true])
{
ob_end_clean();
header("Connection: close");
ignore_user_abort(true);
ob_start();
echo json_encode($array);
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
}
public function buyAction()
{
// Save callback
// это были быстрые действия, дальше отправка ответа
$this->send_response([
'response' => $popup->popup_text,
'type' => 3,
'title' => $popup->popup_title,
'code' => $this->code,
'success' => true
]);
// Send E-Mail и другие длинные действия
die;
}
HTTP/1.1 200 OK
Date: Tue, 25 Apr 2017 08:24:47 GMT
<b>Server: Apache</b>
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
<b>Connection: close</b>
<b>Content-Length: 942</b>
Content-Type: text/html; charset=UTF-8
HTTP/1.1 200 OK
<b>Server: nginx</b>
Date: Tue, 25 Apr 2017 07:39:27 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
<b>Connection: keep-alive</b>
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Vary: Accept-Encoding
Content-Encoding: gzip
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question