nginx之keepalive

一:设置

keepalive_timeout  0;

发curl:

[xxx ~]$ curl -H "Keep-Alive: 60" -H "Connection: keep-alive" '10.195.100.22:015/a.php'

tcpdump:

[xxx~]# tcpdump -i eth1 -n host 10.195.100.22
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size bytes
::54.008723 IP 10.46.43.13. > 10.195.100.22.: Flags [S], seq , win , options [mss ,sackOK,TS val ecr ,nop,wscale ], length
::54.040827 IP 10.195.100.22. > 10.46.43.13.: Flags [S.], seq , ack , win , options [mss ,sackOK,TS val ecr ,nop,wscale ], length
::54.040846 IP 10.46.43.13. > 10.195.100.22.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::54.040909 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::54.073615 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::54.075765 IP 10.195.100.22. > 10.46.43.13.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::54.075775 IP 10.46.43.13. > 10.195.100.22.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::54.075812 IP 10.195.100.22. > 10.46.43.13.: Flags [F.], seq , ack , win , options [nop,nop,TS val ecr ], length
::54.075818 IP 10.46.43.13. > 10.195.100.22.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::54.075871 IP 10.46.43.13. > 10.195.100.22.: Flags [F.], seq , ack , win , options [nop,nop,TS val ecr ], length
::54.108547 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length

发现服务端主动断开了连接

使用nc和telnet

[xxx ~]$ nc 10.195.100.22
GET /a.php HTTP/1.1
host:client.map.baidu.com
Connection: keep-alive
Keep-Alive: HTTP/1.1 OK
Date: Sun, Jul :: GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Set-Cookie: BAIDUID=1556C9779F393DA51F0AF097670ADB40:FG=; expires=Mon, -Jul- :: GMT; max-age=; path=/; domain=.baidu.com; version=
P3P: CP=" OTI DSP COR IVA OUR IND COM "
tracecode:
Server: Apache a [xxx ~]$ telnet 10.195.100.22
Trying 10.195.100.22...
Connected to xxx (10.195.100.22).
Escape character is '^]'.
GET /a.php HTTP/1.1
host:client.map.baidu.com
Connection: keep-alive
Keep-Alive: HTTP/1.1 OK
Date: Sun, Jul :: GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Set-Cookie: BAIDUID=51ACA99FB5239F18891DAD65A018F0BF:FG=; expires=Mon, -Jul- :: GMT; max-age=; path=/; domain=.baidu.com; version=
P3P: CP=" OTI DSP COR IVA OUR IND COM "
tracecode:
Server: Apache a Connection closed by foreign host.

tcpdump:

[xxx ~]# tcpdump -i eth1 -n host 10.195.100.22
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size bytes
::50.516727 IP 10.46.43.13. > 10.195.100.22.: Flags [S], seq , win , options [mss ,sackOK,TS val ecr ,nop,wscale ], length
::50.552093 IP 10.195.100.22. > 10.46.43.13.: Flags [S.], seq , ack , win , options [mss ,sackOK,TS val ecr ,nop,wscale ], length
::50.552107 IP 10.46.43.13. > 10.195.100.22.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::57.006536 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::57.042492 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::57.042504 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::57.080212 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::59.032504 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::59.067393 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::59.459524 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::59.493148 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::59.494975 IP 10.195.100.22. > 10.46.43.13.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::59.494982 IP 10.46.43.13. > 10.195.100.22.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::59.494985 IP 10.195.100.22. > 10.46.43.13.: Flags [F.], seq , ack , win , options [nop,nop,TS val ecr ], length
::59.494990 IP 10.46.43.13. > 10.195.100.22.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::59.495012 IP 10.46.43.13. > 10.195.100.22.: Flags [F.], seq , ack , win , options [nop,nop,TS val ecr ], length
::59.528472 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length

还是服务端主动断开了连接

二:设置

keepalive_timeout  60;

重启nginx(reload无效)

发curl

tcpdump:

[xxx ~]# tcpdump -i eth1 -n host 10.195.100.22
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size bytes
::02.019610 IP 10.46.43.13. > 10.195.100.22.: Flags [S], seq , win , options [mss ,sackOK,TS val ecr ,nop,wscale ], length
::02.054175 IP 10.195.100.22. > 10.46.43.13.: Flags [S.], seq , ack , win , options [mss ,sackOK,TS val ecr ,nop,wscale ], length
::02.054197 IP 10.46.43.13. > 10.195.100.22.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::02.054249 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::02.088571 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::02.091411 IP 10.195.100.22. > 10.46.43.13.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::02.091421 IP 10.46.43.13. > 10.195.100.22.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::02.091544 IP 10.46.43.13. > 10.195.100.22.: Flags [F.], seq , ack , win , options [nop,nop,TS val ecr ], length
::02.126687 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::02.126703 IP 10.195.100.22. > 10.46.43.13.: Flags [F.], seq , ack , win , options [nop,nop,TS val ecr ], length
::02.126711 IP 10.46.43.13. > 10.195.100.22.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length

此时客户端断开了连接

nc或者telnet

[xxxx ~]$ telnet 10.195.100.22                                             Trying 10.195.100.22...
Connected to xxx (10.195.100.22).
Escape character is '^]'.
GET /a.php HTTP/1.1
host:client.map.baidu.com
Connection: keep-alive
Keep-Alive: HTTP/1.1 OK
Date: Sun, Jul :: GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: BAIDUID=C59340AAA043E8A90B1C04AE3695B17F:FG=; expires=Mon, -Jul- :: GMT; max-age=; path=/; domain=.baidu.com; version=
P3P: CP=" OTI DSP COR IVA OUR IND COM "
tracecode:
Server: Apache a GET /a.php HTTP/1.1
host:client.map.baidu.com
Connection: keep-alive
Keep-Alive: HTTP/1.1 OK
Date: Sun, Jul :: GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: BAIDUID=6CA0EEB34CC4FFEDDA20CA3893CF9E46:FG=; expires=Mon, -Jul- :: GMT; max-age=; path=/; domain=.baidu.com; version=
P3P: CP=" OTI DSP COR IVA OUR IND COM "
tracecode:
Server: Apache a Connection closed by foreign host.

tcpdump

[xxxx ~]# tcpdump -i eth1 -n host 10.195.100.22
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size bytes
::04.742501 IP 10.46.43.13. > 10.195.100.22.: Flags [S], seq , win , options [mss ,sackOK,TS val ecr ,nop,wscale ], length
::04.777034 IP 10.195.100.22. > 10.46.43.13.: Flags [S.], seq , ack , win , options [mss ,sackOK,TS val ecr ,nop,wscale ], length
::04.777048 IP 10.46.43.13. > 10.195.100.22.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::10.873033 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::10.907724 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::10.907735 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::10.942783 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::11.681531 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::11.717736 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::12.435530 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::12.471209 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::12.472896 IP 10.195.100.22. > 10.46.43.13.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::12.472903 IP 10.46.43.13. > 10.195.100.22.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::18.466520 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::18.501110 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::18.501127 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::18.535716 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::19.046512 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::19.081658 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::19.530550 IP 10.46.43.13. > 10.195.100.22.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::19.565884 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::19.568107 IP 10.195.100.22. > 10.46.43.13.: Flags [P.], seq :, ack , win , options [nop,nop,TS val ecr ], length
::19.568114 IP 10.46.43.13. > 10.195.100.22.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::29.582270 IP 10.195.100.22. > 10.46.43.13.: Flags [F.], seq , ack , win , options [nop,nop,TS val ecr ], length
::29.582284 IP 10.46.43.13. > 10.195.100.22.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length
::29.582325 IP 10.46.43.13. > 10.195.100.22.: Flags [F.], seq , ack , win , options [nop,nop,TS val ecr ], length
::29.617689 IP 10.195.100.22. > 10.46.43.13.: Flags [.], ack , win , options [nop,nop,TS val ecr ], length

此时:在发送完第一个http请求后,10内服务端并没有断开连接,客户端可以再次发生http包,并且可以复用上次的tcp连接,act和seq号都是接着之前的连接来的。10s如无请求才会断开。

另外:建立tcp连接后,如果客户端在60s内没有发送一个完整的htttp请求包,服务端也会断开此次tcp连接

上一篇:Apache+Tomcat部署负载均衡(或集群)


下一篇:Python学习日志9月13日