http keepalive 与 tcp keep alive

1. HTTP Keep-Alive
In the early days of http, each http request required the opening of a tpc socket connection, and the tcp connection was disconnected after one use.
Using keep-alive can improve this state, that is, multiple copies of data can be continuously sent in a single TCP connection without disconnection. By using the keep-alive mechanism, the number of tcp connection establishments can be reduced, which also means that the TIME_WAIT state connection can be reduced, thereby improving performance and improving the throughput of the httpd server (fewer tcp connections mean fewer system kernel calls, socket the accept() and close() calls).
However, keep-alive is not a free lunch, and long-term tcp connections can easily lead to ineffective use of system resources. Improperly configured keep-alives are sometimes more costly than reusing connections. Therefore, it is very important to set the keep-alive timeout time correctly.
keepalvie timeout
The Httpd daemon generally provides keep-alive timeout time setting parameters. For example, nginx's keepalive_timeout, and Apache's KeepAliveTimeout. This keepalive_timeout time value means that after a tcp connection generated by http has transmitted the last response, it needs to hold keepalive_timeout seconds before closing the connection.
When the httpd daemon sends a response, it should immediately close the corresponding tcp connection actively. After setting keepalive_timeout, the httpd daemon will want to say: "Wait a minute and see if the browser has any requests", and so on. , is the keepalive_timeout time. If the daemon has not received an http request from the browser during this waiting time, it will close the http connection.
2. TCP KEEPALIVE
After the link is established, if the application or upper-layer protocol has not sent data, or only sends data after a long time, how to determine whether the other party is still online when there is no data packet transmission in the link for a long time, whether it is dropped or there is no data. Transmission, whether the link needs to be maintained or not, this situation needs to be considered in the design of the TCP protocol.
The TCP protocol solves this problem in a clever way. After a period of time, TCP automatically sends a message with empty data to the other party. If the other party responds to this message, it means that the other party is still online and the link can continue to be maintained. If the other party does not return a message and retries many times, the link is considered lost, and there is no need to maintain the link.
3. http keep-alive and tcp keep-alive
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 timer supports three system kernel configuration parameters:
1 echo 1800 > /proc/sys/net/ipv4/tcp_keepalive_time
2 echo 15 > /proc/sys/net/ipv4/tcp_keepalive_intvl
3 echo 5 > /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.

4. Illustrate the keep-alive of http

Reference link address: http://www.bubuko.com/infodetail-260176.html

                           http://www.nowamagic.net/academy/detail/23350305

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326170948&siteId=291194637