1、HTTP Keep-Alive
In the early days of http, each HTTP request required a TPC socket connection to be opened, and the TCP connection was disconnected after one use.
The use of keep-alive improves this state by continuing to send multiple data at one TCP connection without disconnecting. By using the keep-alive mechanism, you can reduce the number of TCP connection setups, which also means you can reduce the TIME_WAIT state connectionThen, to improve performance and throughput of the httpd server (fewer TCP connections means fewer system kernel calls, socket accept () and close () calls).
However, keep-alive is not a free lunch, and long TCP connections can easily lead to ineffective usage of system resources. Misconfigured keep-alive is sometimes more costly than reusing connections. So, set up keep-alive Tim correctly.Eout time is very important.
keepalvie timeout
HttpdDaemons generally provide keep-alive timeout time settings parameters. For example, nginx’s keepalive_timeout, and Apache’s KeepAliveTimeout. This keepalive_tThe imout time value means that an http-generated TCP connection does not start closing until the last response has been delivered and the keepalive_timeout second has to be held.
When the httpd daemon sends a response, it should take the initiative to close the corresponding TCP connection immediately. After setting keepalive_timeout, the httpd daemon will want to say, “Wait a minute to see if the browser has requested it yet,” and so on. This is KeEpalive_timeout time. If the daemon has not received an HTTP request from the browser during this wait time, the HTTP connection is closed.
2、TCP KEEPALIVE
After the link is established, if the application or the upper layer protocol does not send data all the time, or sends data once every long time, how to determine if the other party is still online when the link has not been transmitted for a long time, whether it is offline or there is no data transmission, the link still needs to be maintained. This is the case in T.CP protocol design needs to be considered.
TCPThe protocol addresses this problem in a clever way. After more than a period of time, TCP automatically sends an empty message to the other party. If the other party responds to the message, it means that the other party is still online, and the link can be maintained. If the other party does not return the message and retries it many times, it recognizes the message.For links missing, there is no need to keep links.
3、http keep-aliveAnd TCP keep-alive
http keep-aliveUnlike TCP keep-alive, the intention is different. HTTP keep-alive is designed to make TCP live longer so that multiple HTTPS can be transmitted over the same connection to improve the efficiency of sockets. And TCP keep-alIve is a preservation mechanism for TCP to detect TCP connectivity. TCP keep-alive fresh-keeping 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
keepaliveIt’s a TCP freshness timer. When a TCP connection is established at both ends of the network and idle tcp_keepalive_time is left unused, the server kernel tries to send a detection packet to the client to determine the status of the TCP connection.It can crash the client, force the application closed, the host is not reachable, and so on. If you don’t receive an ACK packet from the other party, you try sending the detection packet again after tcp_keepalive_intvl until you receive the ACK from the other party, if you haven’t received the other party’s aCk. Tcp_keepalive_probes are tried a total of times. The intervals here are 15s, 30s, 45s, 60s, 75s. If you try tcp_keepalive_probes, you still haven’t received the right answer.The ACK package of the party will discard the TCP connection. TCP connection default idle time is 2 hours, usually set to 30 minutes is enough.
That is, only if nginx’s keepalive_timeout value is set higher than tcp_keepalive_time, and the last HTTP response to this TCP connection is passed over tcp_keepalive_timeAfter that, the operating system sends the detection packets to decide whether to dispose of the TCP connection. This is not usually the case unless you need to do so.
4、Illustration of HTTP’s keep-alive
Reference link address: http://www.bubuko.com/infodetail-260176.html
http://www.nowamagic.net/academy/detail/23350305