In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to use nginx keepalive, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use nginx keepalive. Let's take a look at it.
The request header of the default http1.1 protocol enables keepalive by default, as shown in the figure:
Then what is keepalive? What is the function?
Keepalive is a mechanism that can detect dead connections in tcp. Its function is to keep socket persistent connections from being disconnected. It belongs to the function of tcp layer and does not belong to the application layer.
How does the tcp layer maintain a long connection?
Let's first look at the usage of keepalive: there are three parameters, which are open to the application layer.
Sk- > keepalive_probes: number of probes, number of retries sk- > heartbeat interval of keepalive_time probe, number of seconds after which tcp link has no data message transmission start probe message sk- > keepalive_intvl detection interval, time interval of retry when no reply is received
Default configuration View:
[* @ *] $cat / proc/sys/net/ipv4/tcp_keepalive_time7200 [* * @ *] $cat / proc/sys/net/ipv4/tcp_keepalive_intvl75 [* @ *] $cat / proc/sys/net/ipv4/tcp_keepalive_probes9
How to use it:
Int keepalive = 1; / enable keepalive attribute int keepidle = 60; / / if the connection does not have any data transactions within 60 seconds, probe int keepinterval = 5; / / the time interval for sending packets is 5 seconds int keepcount = 3; / / the number of probe attempts. If the first probe packet receives a response, the last two packets will not be sent again. And zero the count setsockopt (rs, sol_socket, so_keepalive, (void*) & keepalive, sizeof (keepalive)); setsockopt (rs, sol_tcp, tcp_keepidle, (void*) & keepidle, sizeof (keepidle)); setsockopt (rs, sol_tcp, tcp_keepintvl, (void*) & keepinterval, sizeof (keepinterval)); setsockopt (rs, sol_tcp, tcp_keepcnt, (void*) & keepcount, sizeof (keepcount))
After the application layer is set up in this way, the default configuration will be overwritten and the configuration will be set manually.
For an established tcp connection. If there is no data packet transmission between the two parties within the keepalive_time time, the end that turns on the keepalive function will send the keepalive data heartbeat packet. If it does not receive a reply, it will send the packet again every keepalive_intvl time, sending keepalive_probes times. If no reply has been received, a rst packet is sent to close the connection. If a reply is received, the timer will be cleared to zero.
Grab the packet to verify the content of tcp heartbeat packet
Continue to analyze the heartbeat packets sent and replied by keepalive according to the captured packets:
The source code of tcp header structure is:
Typedef struct _ tcp_header {short massissource; / / source port number 16bit short mroomsdestination; / / destination port number 16bit unsigned int mroomuisequnum; / / req field serial number 32bit unsigned int mroomuisources genum; / / ack field confirmation number 32bit short mroomsheaderlenanddestination; / / first 4 digits: tcp header length; middle 6 digits: reserved The last 6 bits: flag bit short massiswindowsize; / / win field window size 16bit short massively schecksum; / / verification and 16bit short mendacentpos; / / emergency data offset 16bit} _ attribute__ ((packed)) tcp_header, * ptcp_header
Look at the contents of the heartbeat packet sent:
0000 d4 6d 50 f5 02 7f f4 5c 89 cb 35 29 08 00 cb / Mac head 14 Bytes: 45 00Compare / ip first 20 bytes: 0010 00 28 10 f4 0000 40 06 5b dd ac 19 42 76 0a b30020 14 bd e4 1f 7c 32 7e 7a cb 4c bc 55 08 50 10 / / tcp first 20 bytes 0030 10 00 3f 0000 0000 / analyze tcp quality content e4 4a / source port number 16bit 10 : 58442 1f 7c / / destination port number 16bit 10 is: 8060 327e 7a cb / / req field serial number 32bit 10 is 4c bc 5508 / / ack field confirmation number 32bit 5 / / the first 4 digits: tcp header length 5x4 = 20 bytes no problem 0 10 / / 6 digits: reserved The last 6 bits: the mark bit 10 represents the penultimate digit 1, and the logo is changed from tcp package to ack confirmation package 0030 10 00 3f 00 00
Continue to look at the heartbeat package of the reply:
0000 f4 5c 89 cb 35 29 d4 6d 50 f5 02 7f 08 00 45 00 0010 00 34 47 28 40 00 36 06 ef 9c 0a b3 14 bd ac 19 0020 42 76 / / No interpretation of the previous data 1f 7ce4 4a4c bc 55 0832 7e 7a cc8// tcp header length 8 * 4 = 32 in addition to the header there is option data 12 bytes 010 / 6 bits in the middle: reserved The last 6 bits: the mark bit 10 represents the penultimate bit 1, indicating that the tcp packet is ack confirmation package 0030 01 3f / / win field window size 16bit4e 0d / / Inspection and 16bit00 00 / / Emergency data offset 16bit01 01 08 0a 00 59 be 1c 39 13 0040 cf 12 / / option data 12 bytes
As can be seen from the above, the heartbeat packet in which tcp maintains a long connection is that the browser sends an ack packet to the server first, and then the server replies with an ack packet with option data.
What does nginx do with keepalive requests and what does it do?
The first thing to do is to determine the version: if the http protocol version is less than 1.1, the keepalive of the link is set to 0if (r-> http_version).
< ngx_http_version_11) { r->Keepalive = 0; if there is a keep-alive in the ngx_http_request_t in the ngx_http_process_connection function, mark the changed link if (ngx_strcasestrn (h-> value.data, "keep-alive", 10-1)) {r-> headers_in.connection_type = ngx_http_connection_keep_alive } ngx_http_handler function determines r-> headers_in.connection_type and assigns r-> keepalive to 1 switch (r-> headers_in.connection_type) {case ngx_http_connection_keep_alive: r-> keepalive = 1; in the break;} ngx_configure_listening_sockets function, when keepalive is 1, keepalive is enabled for the connection, and then the tcp bottom layer will detect the dead connection mechanism for the connection fd, keep the persistent connection and keep it open. If (ls[ I] .keepalive) {value = (ls[ I] .keepalive = 1)? 1: 0; if (setsockopt (ls[ I] .fd, sol_socket, so_keepalive,// enable keepalive function (const void *) & value, sizeof (int)) =-1)}
When will the nginx long connection be disconnected?
After nginx opens keepalive through setsockopt (ls[ I] .fd, sol_socket, so_keepalive, (const void *) & value, sizeof (int)), it always maintains a persistent connection with the client, so a very serious problem arises. The number of connections that can be maintained by each woker is limited (ep = epoll_create (cycle- > connection_n / 2). Cycle- > connection_n / 2 is the upper limit of fd that epoll can manage. In this way, the number of connections will be quickly exhausted. What should nginx do at this time?
To find out, let's take a look at nginx's two configuration parameters for keeoalive
Keepalive_timeout
Keepalive_timeout timeout [header_timeout]
First parameter: set the timeout value at which keep-alive client connections remain open on the server side (default 75s); a value of 0 disables keep-alive client connections
The second parameter: optional, set a value of "keep-alive: timeout=time" in the header field of the response; usually you don't have to set it
Note: keepalive_timeout defaults to 75s
Keepalive_requests
The keepalive_requests directive is used to set the maximum number of requests that can be served on a keep-alive connection. When the maximum number of requests is reached, the connection is closed, and a value of 0 also disables the keep-alive client connection. The default is 100.
The answer is obvious: manage persistent connections through keepalive_timeout keepalive_requests
When a tcp connection lives longer than keepalive_timeout, it will be dropped by close. The specific implementation of nginx is done through a timer.
When the maximum number of love letters connected to a tcp exceeds keepalive_requests, it will also be dropped by close.
These two mechanisms are used to ensure that the number of connections per worker does not exceed the number of connections that epoll can manage.
This is the end of the article on "how to use nginx keepalive". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use nginx keepalive". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.