D
D
Dmitry Straju2017-04-25 11:34:39
Nginx
Dmitry Straju, 2017-04-25 11:34:39

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;
    }

Everything works for me on the local OpenServer where Apache is used. I send the "Connection: close" and "Content-Length: $size" headers, my browser sees this, understands that there will be no more new data, closes the connection and starts processing the received data.
Apache response headers look like this:
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

The nginx response headers look like this:
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

Question. How to make nginx not ignore headers? Ideally, keep-alive should be left for other connections (after all, I read that it's cool and fast), but if I manually, i.e. I rigidly pass "Connection: close" and "Content-Length: $size" then you need to pass them, you don't need to trim them.

PS I'm sorry if I'm incompetent somewhere and incorrectly described the task, I'm a coder, but I really want to solve this problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2017-04-25
@BuriK666

You need fastcgi_finish_request

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question