Nginx 长连接keep_alive详解

如何开启并支持长连接

当使用nginx作为反向代理时,为了支持长连接,需要做到两点:

  1. 从client到nginx的连接是长连接
  2. 从nginx到server的连接是长连接

        从HTTP协议的角度看,nginx在这个过程中,对于客户端它扮演着HTTP服务器端的角色。而对于真正的服务器端(在nginx的术语中称为upstream)nginx又扮演着HTTP客户端的角色。

客户端(Client)和 Nginx 保持长连接,两个要求:

  1. client发送的HTTP请求要求keep alive
  2. nginx设置上支持keep alive

一、客户端 与 Nginx 长连接配置

        默认情况下,nginx已经自动开启了对client连接的keep alive支持。一般场景可以直接使用,但是对于一些比较特殊的场景,还是有必要调整个别参数。

keepalive_timeout 指令

keepalive_timeout指令的语法:

Syntax:    keepalive_timeout timeout [header_timeout];
Default:    keepalive_timeout 75s;
Context:    http, server, location

第一个参数设置keep-alive客户端连接在服务器端保持开启的超时值。值为0会禁用keep-alive客户端连接。可选的第二个参数在响应的header域中设置一个值“Keep-Alive: timeout=time”。这两个参数可以不一样。

注:默认75s一般情况下也够用,对于一些请求比较大的内部服务器通讯的场景,适当加大为120s或者300s。第二个参数通常可以不用设置。

keepalive_requests 指令

keepalive_requests指令用于设置一个keep-alive连接上可以服务的请求的最大数量。当最大请求数量达到时,连接被关闭。默认是100。

这个参数的真实含义,是指一个keep alive建立之后,nginx就会为这个连接设置一个计数器,记录这个keep alive的长连接上已经接收并处理的客户端请求的数量。如果达到这个参数设置的最大值时,则nginx会强行关闭这个长连接,逼迫客户端不得不重新建立新的长连接。

这个参数往往被大多数人忽略,因为大多数情况下当QPS(每秒请求数)不是很高时,默认值100凑合够用。但是,对于一些QPS比较高(比如超过10000QPS,甚至达到30000,50000甚至更高) 的场景,默认的100就显得太低。

简单计算一下,QPS=10000时,客户端每秒发送10000个请求(通常建立有多个长连接),每个连接只能最多跑100次请求,意味着平均每秒钟就会有100个长连接因此被nginx关闭。同样意味着为了保持QPS,客户端不得不每秒中重新新建100个连接。因此,如果用netstat命令看客户端机器,就会发现有大量的TIME_WAIT的socket连接(即使此时keep alive已经在client和nginx之间生效)。

因此对于QPS较高的场景,非常有必要加大这个参数,以避免出现大量连接被生成再抛弃的情况,减少TIME_WAIT。

keepalive_requests 指令

默认值1000,单个连接中处理的最大请求数,超过这个数,连接销毁。

keepalive_disable 指令

不对某些浏览器建立长连接,默认:msie6

send_timeout 指令

两次向客户端写操作之间的间隔 如果大于这个时间则关闭连接 默认60s

此处有坑,注意耗时的同步操作有可能会丢弃用户连接

该设置表示Nginx服务器与客户端连接后,某次会话中服务器等待客户端响应超过10s,就会自动关闭连接。

http {
    include       mime.types;
    default_type  application/octet-stream;


    sendfile        on;


    keepalive_timeout  65 65; #超过这个时间 没有活动,会让keepalive失效 
    keepalive_time 1h; # 一个tcp连接总时长,超过之后 强制失效
  
    send_timeout 60;# 默认60s  此处有坑!! 系统中 若有耗时操作,超过 send_timeout 强制断开连接。 注意:准备过程中,不是传输过程


    keepalive_requests 1000;  #一个tcp复用中 可以并发接收的请求个数

二、Nginx 与 服务端长连接配置

2.1 在 upstream 中配置

keepalive 100;

向上游服务器的保留连接数(线程池的概念)

keepalive_timeout 65

连接保留时间(单位秒)

keepalive_requests 10000

一个tcp复用中 可以并发接收的请求个数

2.2 在 server 中配置

proxy_http_version 1.1;
配置http版本号
默认使用http1.0协议,需要在request中增加”Connection: keep-alive“ header才能够支持,而HTTP1.1默认支持。
proxy_set_header Connection "";
清楚close信息

三、压力测试

3.1 【客户端】 直连 【Nginx】

Server Software:        nginx/1.21.6
Server Hostname:        192.168.44.102
Server Port:            80

Document Path:          /
Document Length:        16 bytes

Concurrency Level:      30
Time taken for tests:   13.035 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      25700000 bytes
HTML transferred:       1600000 bytes
Requests per second:    7671.48 [#/sec] (mean)
Time per request:       3.911 [ms] (mean)
Time per request:       0.130 [ms] (mean, across all concurrent requests)
Transfer rate:          1925.36 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0      12
Processing:     1    3   1.0      3      14
Waiting:        0    3   0.9      3      14
Total:          2    4   0.9      4      14

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      4
  75%      4
  80%      4
  90%      5
  95%      5
  98%      6
  99%      7
 100%     14 (longest request)

3.2 【客户端】 连接 【Nginx】 反向代理 【Nginx】

Server Software:        nginx/1.21.6
Server Hostname:        192.168.44.101
Server Port:            80

Document Path:          /
Document Length:        16 bytes

Concurrency Level:      30
Time taken for tests:   25.968 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      25700000 bytes
HTML transferred:       1600000 bytes
Requests per second:    3850.85 [#/sec] (mean)
Time per request:       7.790 [ms] (mean)
Time per request:       0.260 [ms] (mean, across all concurrent requests)
Transfer rate:          966.47 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0      13
Processing:     3    8   1.4      7      22
Waiting:        1    7   1.4      7      22
Total:          3    8   1.4      7      22

Percentage of the requests served within a certain time (ms)
  50%      7
  66%      8
  75%      8
  80%      8
  90%      9
  95%     10
  98%     12
  99%     13
 100%     22 (longest request)

3.3【客户端】 直连 【Tomcat】

Server Software:        
Server Hostname:        192.168.44.105
Server Port:            8080

Document Path:          /
Document Length:        7834 bytes

Concurrency Level:      30
Time taken for tests:   31.033 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      804300000 bytes
HTML transferred:       783400000 bytes
Requests per second:    3222.38 [#/sec] (mean)
Time per request:       9.310 [ms] (mean)
Time per request:       0.310 [ms] (mean, across all concurrent requests)
Transfer rate:          25310.16 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0      15
Processing:     0    9   7.8      7     209
Waiting:        0    9   7.2      7     209
Total:          0    9   7.8      7     209

Percentage of the requests served within a certain time (ms)
  50%      7
  66%      9
  75%     11
  80%     13
  90%     18
  95%     22
  98%     27
  99%     36
 100%    209 (longest request)

3.4 【客户端】 连接 【Nginx】 反向代理 【Tomcat】并开启keepalive

Server Software:        nginx/1.21.6
Server Hostname:        192.168.44.101
Server Port:            80

Document Path:          /
Document Length:        7834 bytes

Concurrency Level:      30
Time taken for tests:   23.379 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      806500000 bytes
HTML transferred:       783400000 bytes
Requests per second:    4277.41 [#/sec] (mean)
Time per request:       7.014 [ms] (mean)
Time per request:       0.234 [ms] (mean, across all concurrent requests)
Transfer rate:          33688.77 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       9
Processing:     1    7   4.2      6     143
Waiting:        1    7   4.2      6     143
Total:          1    7   4.2      6     143

Percentage of the requests served within a certain time (ms)
  50%      6
  66%      7
  75%      7
  80%      7
  90%      8
  95%     10
  98%     13
  99%     16
 100%    143 (longest request)

3.5 【客户端】 连接 【Nginx】 反向代理 【Tomcat】不开启keepalive

Server Software:        nginx/1.21.6
Server Hostname:        192.168.44.101
Server Port:            80

Document Path:          /
Document Length:        7834 bytes

Concurrency Level:      30
Time taken for tests:   33.814 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      806500000 bytes
HTML transferred:       783400000 bytes
Requests per second:    2957.32 [#/sec] (mean)
Time per request:       10.144 [ms] (mean)
Time per request:       0.338 [ms] (mean, across all concurrent requests)
Transfer rate:          23291.74 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       9
Processing:     1   10   5.5      9     229
Waiting:        1   10   5.5      9     229
Total:          1   10   5.5      9     229

Percentage of the requests served within a certain time (ms)
  50%      9
  66%     10
  75%     11
  80%     11
  90%     13
  95%     14
  98%     17
  99%     19
 100%    229 (longest request)

nginx确实支持HTTP Keep-alive功能,这使得浏览器可以利用同一个连接发送多个请求并接收响应,提高了网络通信效率,减少了建立TCP连接的成本。 ### HTTP Keep-alive详解 HTTP Keep-alive,也称为持久连接,允许客户端(通常是Web浏览器)在完成了一个HTTP请求之后,继续保持与服务器的连接状态一段时间。这样做的目的是为了减少建立新连接所需的时间,同时也可以让服务器在同一连接上处理后续的请求,从而提高性能和减少资源消耗。 ### Nginx如何实现Keep-aliveNginx配置文件中,默认情况下已经启用了Keep-alive功能。你可以通过`proxy_http_version 1.1;`和`http { keepalive_timeout 50s;}`这样的指令来调整HTTP版本以及Keep-alive超时时间等设置。 - `proxy_http_version 1.1;`用于启用HTTP/1.1协议,这是启用Keep-alive的关键之一。 - `keepalive_timeout 50s;`表示当无数据传输超过50秒后关闭连接。 ### 开启和调整Keep-alive 如果你需要调整默认设置,例如增加Keep-alive的超时时间或者优化资源管理,可以在`server`块或者全局`http`块中添加上述指令: ```nginx http { # 其他配置... proxy_http_version 1.1; keepalive_timeout 60s; # 设置为60秒作为示例 } server { # 其他服务器特定配置... } ``` ### 相关问题: 1. **HTTP Keep-alive对服务器资源的影响是什么?** - 使用HTTP Keep-alive可以帮助服务器更高效地管理连接,减少连接建立和断开带来的系统开销,尤其是在高并发访问的情况下更为明显。 2. **如何在Nginx配置文件中禁用HTTP Keep-alive?** - 可以将`keepalive_timeout`值设为0秒,并关闭`proxy_http_version`为1.1的功能。但这可能会导致浏览器尝试使用HTTP/1.0连接,这通常不会保持连接,除非有额外的脚本或配置来实现这一目标。 3. **为何在某些场景下需要禁用HTTP Keep-alive?** - 禁用HTTP Keep-alive可能出于安全、性能或其他特定需求考虑,比如: - 需要防止长时间占用连接影响其他流量,特别是在有流量控制限制的网络环境。 - 某些服务可能需要明确地清理每个会话的资源,禁止共享连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值