lingering_close important concepts of Nginx

What is lingering_close?

  lingering_close a deferred shutdown, Nginx to close the connection when not immediately close the connection, but turn off the write tcp connection, and then wait for the reading to close the connection after a period of time.

Why should lingering_close?

  Nginx when receiving a client request, if the client or server errors, Nginx to immediately respond with an error message to the client, in response to an error message after it, in most cases need to close the current connection.

  Nginx been executed write () system call to send an error message to the client, write () system call returns success does not mean that the data has been sent to the client, there may still tcp connection write buffer , if executed directly close () system call to close tcp connection, tcp kernel will check the read buffer , there is no client data sent me to stay in kernel mode is not user mode process reads. If the client has sent the message RST (Reset the connection is connected to a reset error occurs due to some reason, and is also used to reject illegal data and request) to close the connector dropped tcp write buffer in the data ,, if none wait write_buffer the data has been sent four times before waving the packet disconnected.

  From the foregoing, if the write buffer in the data in the write () system call after the close () system call is not sent before completion, and the read buffer , there is no data to read, it will cause the client to receive a RST packet and will not get error messages sent by the server. This scene is because the server to the client sends a RST packet data sent by itself result in the client being ignored.

  Solution is as follows, we turn off the write tcp connection, read normal, then re-send the data to the client, it will not be subject to RST. Nginx also sets a timeout lingering_timeout , this time has not exceeded the received data is connected to turn off, and turn off the normal client also connected to the read data.

lingering_time

  The total read time of connection set Nginx is set by lingering_time, this time closing time Nginx is reserved socket after writing, the client needs to send all the data within this time, otherwise Nginx after this time will be directly Turn off the connection.

The advantages and disadvantages of lingering_close

  The client can maintain better compatibility, but will consume more additional resources (such as connection would have been occupied).

The above summary reference links https://mp.weixin.qq.com/s/bXtI45d7M-XjkJH3ARZiMQ

Guess you like

Origin www.cnblogs.com/smallzhen/p/12650652.html