lb的keepalive问题

lb的keepalive问题

0. keepalive

大家都很清楚他的用意了,就是为了减少3次握手,设置一个timeout,比如说20s ,在20s内不请求,连接还是保持着,这时候请求过来,不需要重新经过tcp的三次握手,如果超过了就会断掉,重连的话就要3次握手。

一个正常的keepalive回复头有2个参数:

  1. timeout 超时
  2. max 最多能处理的请求
    max的详细解释如下:
Sets the maximum number of requests that can be served through one keepalive connection. After the maximum number of requests is made, the connection is closed. 

就是一个连接上面的请求数,如果超了连接也会断。

一个例子如下:
请求头:

Connection: keep-alive
Connection: close

回复头:

HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Date: Thu, 11 Aug 2016 15:23:13 GMT
Keep-Alive: timeout=5, max=1000
Last-Modified: Mon, 25 Jul 2016 04:32:39 GMT
Server: Apache

(body)

1. web server实现

nginx

apache httpd

request:

Host: 119.3.62.49
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0

response:

HTTP/1.1 200 OK
Date: Sat, 21 Sep 2019 11:09:02 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Sat, 21 Sep 2019 11:07:58 GMT
ETag: "f-5930e3301e037"
Accept-Ranges: bytes
Content-Length: 15
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

httpd 2.4为例,分别有3个参数可以指定keepalive的行为:

KeepAlive 默认是打开的:

Description:	Enables HTTP persistent connections
Syntax:	KeepAlive On|Off
Default:	KeepAlive On
Context:	server config, virtual host
Status:	Core
Module:	core

默认超时是5s:

Description:	Amount of time the server will wait for subsequent requests on a persistent connection
Syntax:	KeepAliveTimeout num[ms]
Default:	KeepAliveTimeout 5
Context:	server config, virtual host
Status:	Core
Module:	core

MaxKeepAliveRequests 默认是100个,对应http协议是 max=100

Description:	Number of requests allowed on a persistent connection
Syntax:	MaxKeepAliveRequests number
Default:	MaxKeepAliveRequests 100
Context:	server config, virtual host
Status:	Core
Module:	core

详细参考官网 https://httpd.apache.org/docs/2.4/mod/core.html

nginx

对某些浏览器关闭keepalive,默认是对老的ie浏览器disable,如果设置成none的话就全开:

Syntax: 	keepalive_disable none | browser ...;
Default: 	

keepalive_disable msie6;

Context: 	http, server, location

Disables keep-alive connections with misbehaving browsers. The browser parameters specify which browsers will be affected. The value msie6 disables keep-alive connections with old versions of MSIE, once a POST request is received. The value safari disables keep-alive connections with Safari and Safari-like browsers on macOS and macOS-like operating systems. The value none enables keep-alive connections with all browsers. 

 Syntax: 	keepalive_requests number;
Default: 	

keepalive_requests 100;

Context: 	http, server, location

This directive appeared in version 0.8.0.

Sets the maximum number of requests that can be served through one keep-alive connection. After the maximum number of requests are made, the connection is closed. 
Syntax: 	keepalive_timeout timeout [header_timeout];
Default: 	

keepalive_timeout 75s;

Context: 	http, server, location

The first parameter sets a timeout during which a keep-alive client connection will stay open on the server side. The zero value disables keep-alive client connections. The optional second parameter sets a value in the “Keep-Alive: timeout=time” response header field. Two parameters may differ.

The “Keep-Alive: timeout=time” header field is recognized by Mozilla and Konqueror. MSIE closes keep-alive connections by itself in about 60 seconds. 

基本上跟httpd的差不多
详细参考官网 https://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_disable

2. 云厂商的7层负载均衡设置

云厂商的lb参数一般没法调,keepalive的值我确认了一下,举国内厂商为例:

  1. 阿里云,默认keepalive_timeout 15s ,无法关闭。
  2. 华为云,默认keepalive_timeout 300s,无法关闭。

均无法关闭,而且华为云的timeout值有点过大了。

3. keepalive_timeout过大造成的影响

就是用户端会长时间停留在该lb上面,如果你只有1个lb,一个机房,那其实没啥影响,但是如果你有多机房部署,会造成用户到不了其他地方的问题。

原文地址:https://www.cnblogs.com/gqdw/p/11563086.html