tcp keepalive与http keep-alive

1. Long connection
HTTP long connection and short connection are essentially TCP connections. HTTP is an application layer protocol based on request/response mode, while TCP is a transport layer protocol. Only the layer responsible for transmission needs to establish a connection.
In the case of a long connection, multiple HTTP requests can reuse the same TCP connection, saving the consumption of TCP connection establishment and disconnection;

2. http keep-alive
HTTP protocol adopts request/response mode. When using non-KeepAlive mode, each request/response client and server must create a new connection, and disconnect the TCP connection immediately after completion; when using Keep-Alive mode, you can Reusing the same TCP connection, the Keep-Alive function avoids re-establishing the TCP connection.
Keep-alive common parameters:
keepalive_timeout;
keepalive_requests;

3. tcp keepalive
TCP keepalive is used to detect whether there is application packet transmission after the TCP connection is established. If the parameters tcpkeepalivetime and tcpkeepaliveprobes are excluded, the TCP connection will be disconnected;
in the Linux system, there is no global option to enable TCP's KeepAlive. TCP socket is opened separately;
Linux Kernel has three options that affect the behavior of KeepAlive:
1.net.ipv4.tcpkeepaliveintvl = 75
2.net.ipv4.tcpkeepaliveprobes = 9
3.net.ipv4.tcpkeepalivetime = 7200
tcpkeepalivetime indicates that the TCP connection is in After how many seconds there is no data packet transmission to start the detection message;
tcpkeepaliveintvl is in seconds, which means the time interval between the previous detection message and the next detection message;
tcpkeepaliveprobes means the number of detections;
TCP socket also has three options and The kernel corresponds to
TCPKEEPCNT: Override tcpkeepaliveprobes
TCPKEEPIDLE: Override tcpkeepalivetime
TCPKEEPINTVL: Override tcpkeepalive_intvl

4、nginx keepalive与keep-alive
tcp keepalive与http keep-alive

5, nginx fast fault-tolerant network
connection is established
tcp_sys_retries
tcp_sysack_retries
close connection
fin_timeout
tcp_retries1
tcp_retries2
lingering
6, nginx optimization of
the number of nginx process
worker_rlimit_nofile: better be consistent with the value of ulimit -n
worker_connections: each process to allow the maximum number of connections
net.ipv4.tcp_max_tw_buckets : The number of timewait, the default is 180000
net.ipv4.tcp_tw_recycle: timewait quickly recycles
net.ipv4.tcp_tw_reuse: TIME-WAIT sockets are reused for new TCP connections
net.ipv4.ip_local_port_range: The range of ports that the system is allowed to open
net.ipv4. tcp_synack_retries: the number of SYN+ACK packets sent before the kernel gives up the connection
net.ipv4.tcp_keepalive_time: how often TCP sends keepalive messages

7、补充
upstream中的keepalive的解释
1.The connections parameter sets the maximum number of idle keepalive connections to upstream servers connections
2.When this number is exceeded, the least recently used connections are closed
3.It should be particularly noted that the keepalive directive does not limit the total number of connections to upstream servers that an nginx worker process can open

Guess you like

Origin blog.51cto.com/2198640/2562910