Some points of http long connection

  1. http short connection, generally the server side actively closes the connection (historical reasons  https://www.zhihu.com/question/24338653 )
  2. Whether keep-alive is required is specified by the Connection: header field in the http request header. Among them, http1.0 and http1.1 have different points about the Connection header
    1. http1.0 does not support keepalive, so the default header is closed (Connection: close). That is, after telling the server to send the request, the server will actively disconnect (so the server will have timewait). You need to add "Connection: Keep-Alive" to the http header to enable Keep-Alive
    2. The default Connection header of http1.1 is keep-alive (ie Connection: keep-alive), unless Connection: close is explicitly specified. But whether to establish a long connection depends on whether the server supports keep-alive. If nginx (or apache) turns off keepalive, a short connection is still established, and the server closes the connection after sending the request. If a long connection is established, which party should actively close the connection depends on the situation (see 4)
  3. Connection header specific value
    1. ask:
      1. close (tell the WEB server or proxy server to disconnect after completing the response to this request, and not to wait for subsequent requests of this connection).
      2. keepalive (tell the WEB server or proxy server to keep the connection after completing the response to this request and wait for subsequent requests of this connection).
    2. response:
      1. close (the connection has been closed).
      2. keep-alive (the connection is kept, waiting for subsequent requests for this connection).
      3. Keep-Alive: timeout. This value can cause some browsers to actively close the connection, so that the server does not have to close the connection . (see 4)
  4. The keepalive attribute of the Connection field in the http response response
    header keeps-Alive: timeout. This value can cause some browsers to actively close the connection, so that the server does not have to close the connection .
  5. Note how different browsers handle the "keep-alive" header
    1. MSIE and Opera ignore the "Keep-Alive: timeout=<N>" header.
    2. MSIE keeps connection for about 60-65 seconds, then sends TCP RST
    3. Opera maintains a permanent connection
    4. Mozilla keeps the connection alive for N plus about 1-10 seconds.
    5. Konqueror keep long connection for N seconds
  6. The difference between Http keepalive and tcp keepalive
    http keep-alive and tcp keep-alive are not the same thing and have different intentions. http keep-alive is to make tcp live longer, so that multiple http can be transmitted on the same connection and improve the efficiency of socket. The tcp keep-alive is a TCP preservation mechanism that detects the status of the TCP connection. The tcp keep-alive freshness timer supports three system kernel configuration parameters: 
    /proc/sys/net/ipv4/tcp_keepalive_time
    /proc/sys/net/ipv4/tcp_keepalive_intvl
    /proc/sys/net/ipv4/tcp_keepalive_probes

    keepalive is the TCP keepalive timer. After the TCP connection is established at both ends of the network, the idle idle (there is no data flow between the two sides) and tcp_keepalive_time, the server kernel will try to send a detection packet to the client to determine the TCP connection status (It is possible that the client crashed, the application was forcefully closed, the host was unreachable, etc.). If the other party's answer (ack packet) is not received, it will try to send the detection packet again after tcp_keepalive_intvl until the other party's ack is received. If the other party's ack has not been received, it will try tcp_keepalive_probes times in total. The intervals here are 15s, 30s, 45s, 60s, 75s respectively. If you try tcp_keepalive_probes and still do not receive the ack packet from the other party, the TCP connection will be discarded. The default idle time for TCP connections is 2 hours, and generally 30 minutes is sufficient.

    That is to say, only when the keepalive_timeout value of nginx is set higher than tcp_keepalive_time, and after the tcp_keepalive_time time has elapsed since the last http response transmitted by this tcp connection, the operating system will send a detection packet to decide whether to discard this TCP connection. This is generally not the case unless you need to.
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327069938&siteId=291194637