HTTP keepalive璇﹁В
Posted 娣卞叆鍘熺悊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTTP keepalive璇﹁В相关的知识,希望对你有一定的参考价值。
鐐瑰嚮馃憜钃濊壊鈥?nbsp;娣卞叆鍘熺悊鈥濓紝鍏虫敞骞垛€?/span>璁句负鏄熸爣鈥?/span>
鎶€鏈共璐э紝绗竴鏃堕棿鎺ㄩ€?/span>
http keepalive 浠嬬粛
鍦╤ttp鏃╂湡锛屾瘡涓猦ttp璇锋眰閮借姹傛墦寮€涓€涓猼pc socket杩炴帴锛屽苟涓斾娇鐢ㄤ竴娆′箣鍚庡氨鏂紑杩欎釜tcp杩炴帴銆?/p>
浣跨敤keep-alive鍙互鏀瑰杽杩欑鐘舵€侊紝鍗冲湪涓€娆CP杩炴帴涓彲浠ユ寔缁彂閫佸浠芥暟鎹€屼笉浼氭柇寮€杩炴帴銆傞€氳繃浣跨敤keep-alive鏈哄埗锛屽彲浠ュ噺灏憈cp杩炴帴寤虹珛娆℃暟锛屼篃鎰忓懗鐫€鍙互鍑忓皯TIME_WAIT鐘舵€佽繛鎺ワ紝浠ユ鎻愰珮鎬ц兘鍜屾彁楂榟ttpd鏈嶅姟鍣ㄧ殑鍚炲悙鐜?鏇村皯鐨則cp杩炴帴鎰忓懗鐫€鏇村皯鐨勭郴缁熷唴鏍歌皟鐢?socket鐨刟ccept()鍜宑lose()璋冪敤)銆?/p>
浣嗘槸锛宬eep-alive骞朵笉鏄厤璐圭殑鍗堥,闀挎椂闂寸殑tcp杩炴帴瀹规槗瀵艰嚧绯荤粺璧勬簮鏃犳晥鍗犵敤銆傞厤缃笉褰撶殑keep-alive锛屾湁鏃舵瘮閲嶅鍒╃敤杩炴帴甯︽潵鐨勬崯澶辫繕鏇村ぇ銆傛墍浠ワ紝姝g‘鍦拌缃甼eep-alive timeout鏃堕棿闈炲父閲嶈銆?br class="mq-27">Httpd瀹堟姢杩涚▼锛屼竴鑸兘鎻愪緵浜唊eep-alive timeout鏃堕棿璁剧疆鍙傛暟銆傛瘮濡俷ginx鐨刱eepalive_timeout銆傝繖涓猭eepalive_timout鏃堕棿鍊兼剰鍛崇潃锛氫竴涓猦ttp浜х敓鐨則cp杩炴帴鍦ㄤ紶閫佸畬鏈€鍚庝竴涓搷搴斿悗锛岃繕闇€瑕乭old浣弅eepalive_timeout绉掑悗锛屾墠寮€濮嬪叧闂繖涓繛鎺ャ€?/p>
褰揾ttpd瀹堟姢杩涚▼鍙戦€佸畬涓€涓搷搴斿悗锛岀悊搴旈┈涓婁富鍔ㄥ叧闂浉搴旂殑tcp杩炴帴锛岃缃?keepalive_timeout鍚庯紝httpd瀹堟姢杩涚▼浼氭兂璇达細鈥濆啀绛夌瓑鍚э紝鐪嬬湅娴忚鍣ㄨ繕鏈夋病鏈夎姹傝繃鏉モ€濓紝杩欎竴绛夛紝渚挎槸keepalive_timeout鏃堕棿銆傚鏋滃畧鎶よ繘绋嬪湪杩欎釜绛夊緟鐨勬椂闂撮噷锛屼竴鐩存病鏈夋敹鍒版祻瑙堝彂杩囨潵http璇锋眰锛屽垯鍏抽棴杩欎釜http杩炴帴銆?/p>
http keepalive 鍒嗘瀽
鍋囪鎴戜滑鐨刪ttpd鏈嶅姟鍣ㄦ槸nginx锛?nginx鐨刱eepalive_timeout榛樿鏄?0锛屽嵆榛樿寮€鍚簡http keepalive妯″紡锛岃€屼笖http鐨勯暱杩炴帴浼氫繚鎸?0绉掞紝60绉掑唴娌℃湁鏁版嵁鏉ュ線鍗冲叧闂繛鎺ャ€?/p>
鎯呭喌涓€锛氳缃畁ginx鐨刱eepalive_timeout=0锛屽嵆涓嶅紑鍚痥eepalive妯″紡锛岃繖绉嶆儏鍐典笅锛屾瘡涓猦ttp璇锋眰杩囨潵锛宯ginx澶勭悊瀹屾瘯鍗充富鍔ㄥ彂閫丗IN淇″彿鍏抽棴杩炴帴
杩欓噷鎴戜滑璁剧疆涓氬姟sleep 60 绉?/p>
#tcpdump -n host 218.1.57.236 and port 80
20:36:50.792731 IP 218.1.57.236.43052 > 222.73.211.215.http: S 1520902589:1520902589(0) win 65535
20:36:50.792798 IP 222.73.211.215.http > 218.1.57.236.43052: S 290378256:290378256(0) ack 1520902590 win 5840
20:36:50.801629 IP 218.1.57.236.43052 > 222.73.211.215.http: . ack 1 win 32768
20:36:50.801838 IP 218.1.57.236.43052 > 222.73.211.215.http: P 1:797(796) ack 1 win 32768
20:36:50.801843 IP 222.73.211.215.http > 218.1.57.236.43052: . ack 797 win 59
20:37:50.803230 IP 222.73.211.215.http > 218.1.57.236.43052: P 1:287(286) ack 797 win 59
20:37:50.803289 IP 222.73.211.215.http > 218.1.57.236.43052: F 287:287(0) ack 797 win 59
20:37:50.893396 IP 218.1.57.236.43052 > 222.73.211.215.http: . ack 288 win 32625
20:37:50.894249 IP 218.1.57.236.43052 > 222.73.211.215.http: F 797:797(0) ack 288 win 32625
20:37:50.894252 IP 222.73.211.215.http > 218.1.57.236.43052: . ack 798 win 59
鍙互鐪嬪埌涓婇潰鏄粡鍏哥殑http杩囩▼锛岄鍏堟槸3娆℃彙鎵嬪缓绔嬭繛鎺ワ紝鐒跺悗瀹㈡埛绔彂閫佽姹傦紝鏈嶅姟鍣ㄥ鐞?0绉掑悗杩斿洖鏁版嵁锛宧ttp鍝嶅簲涓€鏃﹀彂閫佸畬姣曪紝nginx椹笂鍏抽棴杩欎釜tcp杩炴帴
鍏抽棴 keepalive 鎯呭喌涓嬶紝涓€涓猻ocket璧勬簮浠庡缓绔嬪埌鐪熸閲婃斁闇€瑕佺粡杩囩殑鏃堕棿鏄?寤虹珛tcp杩炴帴 + 浼犻€乭ttp璇锋眰 + 涓氬姟鎵ц + 浼犻€乭ttp鍝嶅簲 + 鍏抽棴tcp杩炴帴 + 2MSL
鎯呭喌浜岋細璁剧疆nginx鐨刱eepalive_timeout=300
#tcpdump -n host 218.1.57.236 and port 80
21:38:05.471129 IP 218.1.57.236.54049 > 222.73.211.215.http: S 1669618600:1669618600(0) win 65535
21:38:05.471140 IP 222.73.211.215.http > 218.1.57.236.54049: S 4166993862:4166993862(0) ack 1669618601 win 5840
21:38:05.481731 IP 218.1.57.236.54049 > 222.73.211.215.http: . ack 1 win 32768
21:38:05.481976 IP 218.1.57.236.54049 > 222.73.211.215.http: P 1:797(796) ack 1 win 32768
21:38:05.481985 IP 222.73.211.215.http > 218.1.57.236.54049: . ack 797 win 59
21:38:07.483626 IP 222.73.211.215.http > 218.1.57.236.54049: P 1:326(325) ack 797 win 59
21:38:07.747614 IP 218.1.57.236.54049 > 222.73.211.215.http: . ack 326 win 32605
21:43:07.448454 IP 222.73.211.215.http > 218.1.57.236.54049: F 326:326(0) ack 797 win 59
21:43:07.560316 IP 218.1.57.236.54049 > 222.73.211.215.http: . ack 327 win 32605
21:43:11.759102 IP 218.1.57.236.54049 > 222.73.211.215.http: F 797:797(0) ack 327 win 32605
21:43:11.759111 IP 222.73.211.215.http > 218.1.57.236.54049: . ack 798 win 59
鍙互鐪嬪埌杩欎釜http杩囩▼璺熶笂闈㈢殑keepalive=0鎯呭喌鏄瘮杈冪被浼肩殑锛屽彧涓嶈繃褰撴湇鍔″櫒鍝嶅簲瀹屾暟鎹悗锛屾病鏈夌珛鍒诲彂閫丗IN淇″彿鍏抽棴杩炴帴锛岃€屾槸浠?8鍒嗙瓑鍒?3鍒嗭紝nginx鎵嶅彂閫丗IN淇″彿鍏抽棴杩炴帴銆傝鏄庝簡nginx鐨刱eepalive=300閰嶇疆鐢熸晥浜嗐€?/p>
鎯呭喌涓夛紝璁剧疆keepalive_timeout=180锛屽苟涓斿湪鍚屼竴涓猼cp杩炴帴鍙戦€佸涓猦ttp鍝嶅簲
# tcpdump -n host 218.1.57.236 and port 80
22:43:57.102448 IP 218.1.57.236.49955 > 222.73.211.215.http: S 4009392741:4009392741(0) win 65535
22:43:57.102527 IP 222.73.211.215.http > 218.1.57.236.49955: S 4036426778:4036426778(0) ack 4009392742 win 5840
22:43:57.111337 IP 218.1.57.236.49955 > 222.73.211.215.http: . ack 1 win 32768
22:43:57.111522 IP 218.1.57.236.49955 > 222.73.211.215.http: P 1:797(796) ack 1 win 32768
22:43:57.111530 IP 222.73.211.215.http > 218.1.57.236.49955: . ack 797 win 59
22:43:59.114663 IP 222.73.211.215.http > 218.1.57.236.49955: P 1:326(325) ack 797 win 59
22:43:59.350143 IP 218.1.57.236.49955 > 222.73.211.215.http: . ack 326 win 32605
22:45:59.226102 IP 218.1.57.236.49955 > 222.73.211.215.http: P 1593:2389(796) ack 650 win 32443
22:45:59.226109 IP 222.73.211.215.http > 218.1.57.236.49955: . ack 2389 win 83
22:46:01.227187 IP 222.73.211.215.http > 218.1.57.236.49955: P 650:974(324) ack 2389 win 83
22:46:01.450364 IP 218.1.57.236.49955 > 222.73.211.215.http: . ack 974 win 32281
22:47:57.377707 IP 218.1.57.236.49955 > 222.73.211.215.http: P 3185:3981(796) ack 1298 win 32119
22:47:57.377714 IP 222.73.211.215.http > 218.1.57.236.49955: . ack 3981 win 108
22:47:59.379496 IP 222.73.211.215.http > 218.1.57.236.49955: P 1298:1622(324) ack 3981 win 108
22:47:59.628964 IP 218.1.57.236.49955 > 222.73.211.215.http: . ack 1622 win 32768
22:50:59.358537 IP 222.73.211.215.http > 218.1.57.236.49955: F 1622:1622(0) ack 3981 win 108
22:50:59.367911 IP 218.1.57.236.49955 > 222.73.211.215.http: . ack 1623 win 32768
22:50:59.686527 IP 218.1.57.236.49955 > 222.73.211.215.http: F 3981:3981(0) ack 1623 win 32768
22:50:59.686531 IP 222.73.211.215.http > 218.1.57.236.49955: . ack 3982 win 108
杩欐鎴戜滑寮€鍚簡keepalive=180锛屽苟涓旇繛缁彂閫佷簡鍑犱釜http璇锋眰锛屽彲浠ョ湅鍒帮紝閮藉鐢ㄤ簡鍚屼竴涓繛鎺ワ紙鍥犱负娌℃湁鍐嶆鐨勪笁娆℃彙鎵嬪缓绔嬭繛鎺ワ級锛岃€宯ginx鍐嶆渶鍚庝竴娆℃暟鎹彂閫佸悗锛屽湪杩囦簡180绉掑悗锛屽彂閫丗IN淇″彿鍏抽棴浜嗚繛鎺ャ€?span class="mq-283">杩欒鏄巒ginx鐨刱eepalive鍊掕鏃舵槸浠庢渶鍚庝竴涓暟鎹寘寮€濮嬭绠楃殑锛?/span>
http keepalive 瀹炶返锛氶厤缃畁ginx 涓?upstream 闀胯繛鎺eep-alive閰嶇疆
nginx浠g悊涓庝笂娓告湇鍔″櫒 upstream 涔嬮棿鐨勮繛鎺ラ粯璁ゆ槸鍏抽棴浜嗛暱杩炴帴锛屾垜浠彲浠ラ€氳繃鎶撳寘鏉ョ湅鍒帮紝閫氳繃娴忚鍣ㄦ彁浜や笂鏉ョ殑HTTP/1.1鐨勮姹傦紝缁忚繃浠g悊鏈嶅姟鍣ㄤ互鍚庢敼涓篐TTP/1.0锛?鎵€浠ユ兂瑕佷唬鐞嗕笌鏈嶅姟鍣ㄤ箣闂翠娇鐢ㄩ暱杩炴帴锛岄渶瑕佷慨鏀筺ginx鐨勭浉鍏抽厤缃€?/p>
upstream test_keepalive{
server 127.0.0.1:5002;
keepalive 4;
}
server {
listen 80;
location / {
proxy_pass http://test_keepalive;
proxy_set_header Host $host:8000;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
upstream閲岄潰鐨勬湁鍑犱釜閰嶇疆鏄窡keepalive鐩稿叧鐨勶細
keepalive: 4锛?姣忎釜nginx worker鍙互淇濈暀鐨刬dle杩炴帴锛屼篃灏辨槸璇存渶澶氬彲淇濈暀鐨勯暱杩炴帴锛屽鏋滆秴杩囪繖涓暟閲忥紝浼氭牴鎹甃RU绠楁硶锛宭east recently used 鐢ㄧ殑鏈€灏戠殑杩炴帴浼氳close锛岄渶瑕佹敞鎰忕殑鏄紝杩欎釜鍙傛暟涓嶄細闄愬埗worker鐨勮窡upstream鐨勮繛鎺ユ暟銆?/p>
keepalive_timeout :60锛?nginx璺焨pstreamd鐨?idle 绌洪棽闀胯繛鎺ワ紝鏈€澶ц秴鏃舵椂闂达紝瓒呰繃鏃堕棿娌℃湁鏁版嵁寰€鏉ュ垯瓒呮椂鍏抽棴
keepalive_requests : 100锛岃缃瘡涓暱杩炴帴鏈€澶氳兘澶勭悊鐨勮姹傛鏁帮紝瓒呰繃浜嗕互鍚庤繛鎺ュ氨浼氳close锛屽畾鏈熷叧闂浜庢竻鐞嗘瘡涓繛鎺ョ殑鍗犵敤鐨勫唴瀛樻槸闈炲父蹇呰鐨勶紝鍚﹀垯杩炴帴鍗犵敤鐨勫唴瀛樹細瓒婃潵瓒婂ぇ锛岃繖鏄笉鎺ㄨ崘鐨勩€?/p>
瀵逛簬location閲岄潰鐨勯厤缃紝涓昏鏄慨鏀筯ttp鐗堟湰涓?.1, 骞朵笖鎶妌ginx榛樿浼犻€掔粰upsteam connection锛歝lose鐨勮涓哄幓鎺?br class="mq-329">http_version闇€瑕佹寚瀹氱敤1.1 锛屼互鍙婂師鏈夌殑Connection澶撮儴瑕佽鎿︽帀
proxy_http_version 1.1;
proxy_set_header Connection "";
杩欐牱灏卞畬鎴愪簡nginx杞彂缁檜pstream鐨勯暱杩炴帴閰嶇疆浜嗭紝浣嗘槸锛屾垜浠殑upstream涔熻閰嶇疆keepalive鎵嶈鍟婏紝鍚﹀垯瀹冩棤瑙唍ginx鍙戞潵鐨刢onnection:keepalive 澶撮儴涔熶笉琛屻€?br class="mq-334">鎴戜滑鐢ㄧ殑鏄痜lask 妗嗘灦锛屾湰鍦拌皟璇曠殑璇濊鍔犱笂涓嬮潰鐨勯厤缃紝鎵嶈兘浣跨敤涓奾ttp 1.1
flask鐨勬湰鍦?span class="mq-339">run()鍚敤keep-alive
>Before app.run(), Add one line, WSGIRequestHandler.protocol_version = "HTTP/1.1"
Don't forget from werkzeug.serving import WSGIRequestHandler.
鎬濊€冧竴涓嬶紝鐢╱wsgi鐨勮姳闇€瑕佸彟澶栭厤缃甼eepalive鍚楋紵
鍥炵瓟锛歶wsgi鏀寔keep-alive 濡傛灉nginx閫氳繃http pass 缁檜wsgi锛?--http-keepalive
uwsgi --http=:8000 --http-keepalive -w app &> /dev/null
浣嗘槸nginx 涓€鑸兘鏄€氳繃uwsgi_pass 浼犻€掕姹傜粰uwsgi锛岃€屾垜浠鐨勬槸http鍗忚鐨刱eepalive 锛屾墍浠ュ浜庡叾浠栧崗璁濡傦紝uwsgi鍗忚锛宖astcgi鍗忚閮芥槸涓嶈兘鐢熸晥鐨?br class="mq-380">鍙傝€?br class="mq-381">nginx proxy 鐨勮鍒?br class="mq-382">nginx keepalive doc
閭d箞鑷虫锛宯ginx璺熶笂娓告湇鍔″櫒鐨勯暱杩炴帴閰嶇疆灏卞畬鎴愪簡
curl 娴嬭瘯璺焠ginx keep-alive 杩炴帴鏄惁澶嶇敤
curl -v http://127.0.0.1:80/api/recommend/index -H "Host: app-store-server.webapp.163.com" http://127.0.0.1:80/api/recommend/index
curl鍚庨潰璺熼殢涓や釜杩炴帴锛岃curl鍚屾椂璇锋眰涓や釜url
杩欐椂鎶撳寘
127.0.0.1.48402 > 127.0.0.1.80: Flags [S], seq 4215851718, win 43690, options [mss 65495,sackOK,TS val 2968699288 ecr 0,nop,wscale 7], length 0
127.0.0.1.80 > 127.0.0.1.48402: Flags [S.], seq 4157782454, ack 4215851719, win 43690, options [mss 65495,sackOK,TS val 2968699288 ecr 2968699288,nop,wscale 7], length 0
127.0.0.1.48402 > 127.0.0.1.80: Flags [.], ack 1, win 342, options [nop,nop,TS val 2968699288 ecr 2968699288], length 0
127.0.0.1.48402 > 127.0.0.1.80: Flags [P.], seq 1:115, ack 1, win 342, options [nop,nop,TS val 2968699289 ecr 2968699288], length 114: HTTP: GET /api/recommend/index HTTP/1.1
127.0.0.1.80 > 127.0.0.1.48402: Flags [.], ack 115, win 342, options [nop,nop,TS val 2968699289 ecr 2968699289], length 0
127.0.0.1.80 > 127.0.0.1.48402: Flags [P.], seq 1:233, ack 115, win 342, options [nop,nop,TS val 2968699291 ecr 2968699289], length 232: HTTP: HTTP/1.1 200 OK
127.0.0.1.48402 > 127.0.0.1.80: Flags [.], ack 233, win 350, options [nop,nop,TS val 2968699291 ecr 2968699291], length 0
127.0.0.1.48402 > 127.0.0.1.80: Flags [P.], seq 115:229, ack 233, win 350, options [nop,nop,TS val 2968699292 ecr 2968699291], length 114: HTTP: GET /api/recommend/index HTTP/1.1
127.0.0.1.80 > 127.0.0.1.48402: Flags [.], ack 229, win 342, options [nop,nop,TS val 2968699334 ecr 2968699292], length 0
127.0.0.1.80 > 127.0.0.1.48402: Flags [P.], seq 233:465, ack 229, win 342, options [nop,nop,TS val 2968699335 ecr 2968699292], length 232: HTTP: HTTP/1.1 200 OK
127.0.0.1.48402 > 127.0.0.1.80: Flags [F.], seq 229, ack 465, win 359, options [nop,nop,TS val 2968699335 ecr 2968699335], length 0
127.0.0.1.80 > 127.0.0.1.48402: Flags [F.], seq 465, ack 230, win 342, options [nop,nop,TS val 2968699335 ecr 2968699335], length 0
127.0.0.1.48402 > 127.0.0.1.80: Flags [.], ack 466, win 359, options [nop,nop,TS val 2968699335 ecr 2968699335], length 0
鍙互鐪嬪埌锛屼袱娆$殑璇锋眰锛岄兘鏄娇鐢ㄥ悓涓€涓鍙o紝璇锋眰瀹屾瘯浜嗗悗锛宑url涓诲姩鍏抽棴socket锛?span class="mq-859">姣曠珶绋嬪簭涔熷叧闂簡銆?br class="mq-860">鎵€浠ュ鎴风->nginx鐨勮繛鎺ユ槸瀹炵幇浜唄ttp keep-alive
鑰屾鏃讹紝濡傛灉娉ㄩ噴鎺変簡nginx閰嶇疆锛?/p>
# proxy_http_version 1.1;
# proxy_set_header Connection "";
閭d箞姝ゆ椂鍐嶆璇锋眰锛屽鎴风->nginx杩炴帴杩樻槸鍙互keep-alive鐨勶紝浣嗘槸nginx->upstream灏变笉琛屼簡锛屽洜涓洪偅鏄拡瀵筺gxin->upstream鐨勯厤缃紝瀹㈡埛绔窡nginx鐨勯厤缃粯璁ゅ氨鏄兘keep-alive
requests娴嬭瘯nginx keepalive 杩炴帴
import time
import requests
for i in range(5):
r = requests.get('http://127.0.0.1/api/recommend/index?a', headers={"Host": "app-store-server.webapp.163.com", "Connection": "keep-alive"})
time.sleep(2)
涓轰粈涔堣繖鏍疯姹傝繃鍘籲ginx锛岃窡nginx鐨勮繛鎺ユ病鏈夊鐢紝姣忔寰幆锛屽鎴风瀹屼簡閮戒細鍙戦€丗.淇″彿锛岃€屼笉浼氬鐢ㄨ繛鎺ュ憿锛?br class="mq-906">杩欎釜闂璺熶竴寮€濮嬫垜鎯冲湪缁堢curl http://xxx 涓€鏍锋兂鐪嬪埌绗琋娆url鐨勬椂鍊欙紝閮戒細澶嶇敤鍚屼竴涓繛鎺ワ紝浣嗘槸涓婄綉鏌ヤ簡涓嬶紝浣跨敤curl -v curl榛樿灏辨槸http 1.1, connection: keep-alive鐨勶紝浣嗘槸curl杩欎釜绋嬪簭缁撴潫浠ュ悗锛屽氨浼氱洿鎺ュ叧闂璼ocket锛岃繖涔熷緢姝e父锛岀▼搴忛兘鍏抽棴浜嗭紝socket鑷劧灏变細鍏抽棴銆?/p>
浣嗘槸杩欓噷鐨勬垜鑷繁鍐欎釜绋嬪簭锛屽惊鐜?娆★紝绋嬪簭娌℃湁鍏抽棴锛屽畠涔熶細鑷姩鍏抽棴socket锛屾病鏈夊鐢ㄨ繛鎺ワ紝鐪嬫潵鏄痳equests鐨勯棶棰樸€?br class="mq-908">涓婄綉鏌ヤ簡涓嬶紝鏄痳equests鐨勯棶棰橈紝瀹冮粯璁や笉鏀寔keepalive锛岄渶瑕佷娇鐢╮equests.session 瀵硅薄鏉ヨ姹傦紝鎵嶈兘浣跨敤涓妅eepalive
娴忚鍣ㄤ笂瑙傚療涓巒ginx 鐨刱eep-alive
娴忚鍣ㄦ墦寮€ http://app-store-server.webapp.163.com:8000/api/recommend/index?a 纭疄鍙互澶嶇敤杩炴帴锛屽洜涓篽ttp1.1榛樿寮€鍚簡connection: keepalive
褰揷onnection:keep-alive鐨勫ご璺熼殢璇锋眰鍒拌揪nginx锛宯ginx灏变細keepalive锛宯ginx 鏄惁寮€鍚痥eep-alive鐨勯厤缃氨鏄痥eepalive-timout, 璁剧疆鎴?灏辨槸涓嶅紑鍚痥eepalive銆?br class="mq-913">涓轰簡鏍¢獙鏁堟灉鎴戞垜鎶妌ginx鐨刱eepalive_timeout 璁剧疆鎴愪簡10
杩炵画璇锋眰3娆★紝tcp鎶撳寘鐪嬪埌
# 绗竴娆¤姹?涓夋鎻℃墜寤虹珛杩炴帴
10.0.2.2.54568 > 10.0.2.15.80: Flags [S], seq 3866240001, win 65535, options [mss 1460], length 0
17:18:47.237953 IP 10.0.2.15.80 > 10.0.2.2.54568: Flags [S.], seq 3002799617, ack 3866240002, win 29200, options [mss 1460], length 0
17:18:47.238366 IP 10.0.2.2.54568 > 10.0.2.15.80: Flags [.], ack 1, win 65535, length 0
# 绗竴娆¤姹?/span>
17:18:47.238397 IP 10.0.2.2.54568 > 10.0.2.15.80: Flags [P.], seq 1:514, ack 1, win 65535, length 513: HTTP: GET /api/recommend/index?a HTTP/1.1
17:18:47.238402 IP 10.0.2.15.80 > 10.0.2.2.54568: Flags [.], ack 514, win 30016, length 0
17:18:47.241415 IP 10.0.2.15.80 > 10.0.2.2.54568: Flags [P.], seq 1:233, ack 514, win 30016, length 232: HTTP: HTTP/1.1 200 OK
17:18:47.241993 IP 10.0.2.2.54568 > 10.0.2.15.80: Flags [.], ack 233, win 65535, length 0
# 绗簩娆?/span>
17:18:51.767547 IP 10.0.2.2.54568 > 10.0.2.15.80: Flags [P.], seq 514:1027, ack 233, win 65535, length 513: HTTP: GET /api/recommend/index?a HTTP/1.1
17:18:51.770731 IP 10.0.2.15.80 > 10.0.2.2.54568: Flags [P.], seq 233:465, ack 1027, win 31088, length 232: HTTP: HTTP/1.1 200 OK
17:18:51.771010 IP 10.0.2.2.54568 > 10.0.2.15.80: Flags [.], ack 465, win 65535, length 0
# 绗笁娆?/span>
17:18:54.606365 IP 10.0.2.2.54568 > 10.0.2.15.80: Flags [P.], seq 1027:1540, ack 465, win 65535, length 513: HTTP: GET /api/recommend/index?a HTTP/1.1
17:18:54.609627 IP 10.0.2.15.80 > 10.0.2.2.54568: Flags [P.], seq 465:697, ack 1540, win 32160, length 232: HTTP: HTTP/1.1 200 OK
17:18:54.610215 IP 10.0.2.2.54568 > 10.0.2.15.80: Flags [.], ack 697, win 65535, length 0
# 绛夊緟10绉掑悗 nginx鍙戦€丗in淇″彿
17:19:04.618932 IP 10.0.2.15.80 > 10.0.2.2.54568: Flags [F.], seq 697, ack 1540, win 32160, length 0
17:19:04.619351 IP 10.0.2.2.54568 > 10.0.2.15.80: Flags [.], ack 698, win 65535, length 0
# 杩欓噷鏈夌偣鐤戞儜浜嗭紝 娴忚鍣ㄦ病鏈夊彂閫丗in淇″彿锛屽弽鑰屾槸R.
17:21:04.620415 IP 10.0.2.2.54568 > 10.0.2.15.80: Flags [R.], seq 1540, ack 698, win 65535, length 0
绛夊緟10绉掑悗锛宯ginx鍙戦€丗.淇″彿浜嗭紝绗﹀悎鎴戜滑棰勬湡锛岀劧鍚庢祻瑙堝櫒涔熺珛椹繑鍥瀉ck淇″彿浜嗐€?br class="mq-1440">浣嗘槸娴忚鍣ㄥ眳鐒舵病鏈夊彂閫丗.缁檔ginx锛岃繖鏃跺悗鐨?0绔鍙e浜?FIN_WAIT2鐘舵€?/p>
tcp 0 0 10.0.2.15:80 10.0.2.2:57624 FIN_WAIT2 -
涓轰粈涔堟祻瑙堝櫒娌℃湁鍙婃椂鍙戦€丗.淇″彿鍛紵鑰屾槸鍦ㄤ袱鍒嗛挓鍚庯紝娴忚鍣ㄥ彂閫丷.淇″彿缁檔ginx锛岃繖鏃剁殑杩炴帴鎵嶈瀹屽叏鍏抽棴浜嗐€?/p>
鍦╪ginx杩樺浜嶧IN_WAIT2鐨勬椂鍊欙紝涔熷氨鏄client娌℃兂杩囪鍙戦€丗in淇″彿锛屾槸鍥犱负瀹冭涓鸿嚜宸辫繕鏈夋暟鎹鍙戯紝鎵€浠ヤ笉鍏抽棴鍚楋紵鐒跺悗璁剧疆浜?鍒嗛挓瓒呮椂鏃堕棿锛屽彂閫丷淇″彿锛岄噸缃繛鎺ャ€傞偅涔堣繖鏍锋湇鍔″櫒涓嶆槸涓€鐩村緱鍗犵敤浜嗕竴涓鍙e浜嶧IN_WAIT2鐘舵€佸悧锛熻繖涓嶆槸寰堟氮璐瑰悧锛?br class="mq-1449">鍦╪ginx杩樺浜嶧IN_WAIT2鐨勬椂鍊欙紝鎴戣娴忚鍣ㄧ户缁彂閫佸悓涓€涓姹傝繃鍘籲ginx锛岀湅鐪嬩細鎬庢牱鐨勶紝鎶撳寘鐪嬪埌
# 娴忚鍣ㄧ户缁儚姝e父鎯呭喌涓嬪彂閫佽姹?/span>
10.0.2.2.58500 > 10.0.2.15.80: Flags [P.], seq 514:1027, ack 234, win 65535, length 513: HTTP: GET /api/recommend/index?a HTTP/1.1
# nginx 鐢变簬涔嬪墠宸茬粡鍙戦€丗in淇″彿锛岃鏄庡畠宸茬粡鎯冲叧闂繛鎺ヤ簡锛岃繖閲屽啀娆℃敹鍒版暟鎹悗锛屽彂閫佷竴涓噸缃爣璇?/span>
18:33:39.452057 IP 10.0.2.15.80 > 10.0.2.2.58500: Flags [R], seq 1177597640, win 0, length 0
# 杩欎釜鏃跺€欐祻瑙堝櫒鎵嶈蛋姝e父娴佺▼锛屽彂閫丗.淇″彿銆?/span>
18:33:39.452155 IP 10.0.2.2.58500 > 10.0.2.15.80: Flags [F.], seq 1027, ack 234, win 65535, length 0
# nginx 鐪嬪埌F.淇″彿锛屾病鏈夐笩瀹冿紝姣曠珶鎴戜箣鍓嶅彂閫佷簡R.淇″彿锛岀牬闀滀笉鑳介噸鍦嗭紝鍙兘缁х画鍙戦€丷淇″彿
18:33:39.452163 IP 10.0.2.15.80 > 10.0.2.2.58500: Flags [R], seq 1177597640, win 0, length 0
# 娴忚鍣ㄦ敹鍒板悗锛屽ソ鍚э紝閭e氨缁撴潫鎶婏紒
18:33:39.452304 IP 10.0.2.2.58500 > 10.0.2.15.80: Flags [R.], seq 4103854591, ack 234, win 0, length 0
18:33:39.452835 IP 10.0.2.2.58509 > 10.0.2.15.80: Flags [S], seq 193480705, win 65535, options [mss 1460], length 0
18:33:39.452857 IP 10.0.2.15.80 > 10.0.2.2.58509: Flags [S.], seq 1631094942, ack 193480706, win 29200, options [mss 1460], length 0
18:33:39.452981 IP 10.0.2.2.58509 > 10.0.2.15.80: Flags [.], ack 1, win 65535, length 0
18:33:39.453059 IP 10.0.2.2.58509 > 10.0.2.15.80: Flags [P.], seq 1:514, ack 1, win 65535, length 513: HTTP: GET /api/recommend/index?a HTTP/1.1
涓婇潰鐨勬祦绋嬶細
娴忚鍣ㄧ户缁悜姝e父鎯呭喌涓嬪彂閫佽姹?/p>
nginx 鐢变簬涔嬪墠宸茬粡鍙戦€丗in淇″彿锛岃鏄庡畠宸茬粡鎯冲叧闂繛鎺ヤ簡锛岃繖閲屽啀娆℃敹鍒版暟鎹悗锛屽彂閫佷竴涓噸缃爣璇?/p>
杩欎釜鏃跺€欐祻瑙堝櫒鎵嶈蛋姝e父娴佺▼锛屽彂閫丗.淇″彿銆?/p>
nginx 鐪嬪埌F.淇″彿锛屾病鏈夐笩瀹冿紝姣曠珶鎴戜箣鍓嶅彂閫佷簡R.淇″彿锛岀牬闀滀笉鑳介噸鍦嗭紝鍙兘缁х画鍙戦€丷淇″彿
娴忚鍣ㄦ敹鍒板悗锛屽ソ鍚э紝閭e氨缁撴潫鎶婏紒
涓嬮潰灏遍噸鏂板紑绔彛锛屽紑鍚柊鐨勪竴涓笁娆℃彙鎵嬶紝纭畾鏂拌繛鎺?/p>
娴嬭瘯杩囩▼涓繕纰板埌涓€绉嶆儏鍐垫槸锛宯ginx keepalive 瓒呮椂鍚庯紝鍙戦€丗IN缁欏鎴风锛?杩欐椂瀹㈡埛绔啀娆¤姹傦紝灏辫蛋姝e父娴佺▼浜嗭紝鍚憂ginx鍙戦€丗IN.淇″彿锛宯ginx涔熷洖搴攁ck淇″彿銆?/p>
18:27:36.692101 IP 10.0.2.15.80 > 10.0.2.2.59264: Flags [F.], seq 929, ack 2477, win 34045, length 0
18:27:36.692546 IP 10.0.2.2.59264 > 10.0.2.15.80: Flags [.], ack 930, win 65535, length 0
# nginx鍙戦€丗鍚庯紝鎴戝湪娴忚鍣ㄧ户缁彂閫佸悓涓€涓姹傦紝
18:27:40.712616 IP 10.0.2.2.59264 > 10.0.2.15.80: Flags [F.], seq 2477, ack 930, win 65535, length 0
18:27:40.712675 IP 10.0.2.15.80 > 10.0.2.2.59264: Flags [.], ack 2478, win 34045, length 0
... 杩炴帴鍏抽棴鍚庯紝涓嬩釜璇锋眰灏遍噸鏂板缓绔嬫柊鐨勮姹備簡
缁忚繃涓婇潰鐨勬祴璇曪紝鎬濊€冨嚑涓棶棰橈細
nginx 80绔彛澶勪簬FIN_WAIT2鐨勬椂鍊欙紝濡傛灉闀挎椂闂翠笉璇锋眰锛屽垯浼氬湪client鐨勮秴鏃讹紙涓婇潰2鍒嗛挓锛夊悗鍙戦€丷淇″彿鍏抽棴杩炴帴锛熻繖涓槸涓轰粈涔? client涓轰粈涔堜笉杩斿洖FIN锛?/p>
鏈夋椂鍊檆lient鏄甯稿彂閫丗in鍏抽棴杩炴帴锛屾湁鏃跺€欐槸鍙戦€丷ST淇″彿鍏抽棴杩炴帴锛?/p>
nginx 80绔彛澶勪簬FIN_WAIT2鐨勬椂鍊欙紝濡傛灉缁х画璇锋眰锛屽垯浼氶噸缃畬杩炴帴鍚庯紝閲嶆柊鍚姩鏂扮殑杩炴帴涓夋鎻℃墜锛屾甯歌姹傦紝鐒跺悗FIN_WAIT2涔熶細娑堝け
鎴戝彂鐜板悓涓€涓數鑴戯紝涓嶅悓鐢佃剳鍙戦€佽姹傦紝閮戒細鐢ㄥ悓涓€涓繛鎺ワ紝闅惧埌涓嶆槸涓€涓祻瑙堝櫒鍚勮嚜鐙珛鐨?鍚楋紵闅鹃亾鏄疦AT缃戠粶鐨勫奖鍝嶏紵杩樻槸闀胯繛鎺ユ牴鏈氨鏄叏灞€绠$悊鐨勶紵
ngxin浣跨敤鍏朵粬鍗忚锛屾瘮濡備娇鐢╱wsgi_pass 锛宖astcgi_pass杞彂璇锋眰鍒皍pstream鐨勮瘽锛屾€庝箞淇濇寔keepalive锛?/p>
ack 鏈哄埗锛屾垜鍙戠幇浜嗗彧鏈?娆℃彙鎵嬶紝鍜?娆℃彙鎵嬬殑鏃跺€欙紝ack鏄姞1鐨勶紝濡傛灉姝e父浼犺緭鏁版嵁锛宎ck灏辨槸涓婁竴涓寘鏈€鍚庣殑seq鏁板瓧锛屼笉浼氬姞1銆?/p>
绋嬪簭鍏抽棴锛屾瘮濡傚鎴风鐨刢url锛屽叧闂簡灏变竴瀹氫細鍙戦€佸叧闂繛鎺ョ殑璇锋眰鍚楋紵绔彛渚濋檮鍦ㄧ▼搴忎笂闈㈢殑鍚?/p>
鍦╪ginx keepalive 瓒呮椂鐨勬椂鍊欙紝鍙戦€丗IN缁欐祻瑙堝櫒鐨勬椂鍊欙紝娴忚鍣ㄧ珛鍒诲洖搴斾簡ack锛屼絾鏄祻瑙堝櫒鍗存病鏈夋仮澶岶IN淇″彿缁檔ginx鍛??鑰屾槸鍦ㄤ袱鍒嗛挓鍚庯紝娴忚鍣ㄥ彂閫丷.淇″彿缁檔ginx锛岃繖鏃剁殑杩炴帴鎵嶈瀹屽叏鍏抽棴浜嗐€?br class="mq-1785">鍦╪ginx杩樺浜嶧IN_WAIT2鐨勬椂鍊欙紝涔熷氨鏄client娌℃兂杩囪鍙戦€丗in淇″彿锛屾槸鍥犱负瀹冭涓鸿嚜宸辫繕鏈夋暟鎹鍙戯紝鎵€浠ヤ笉鍏抽棴鍚楋紵鐒跺悗璁剧疆浜?鍒嗛挓瓒呮椂鏃堕棿锛屽彂閫丷淇″彿锛岄噸缃繛鎺ャ€傞偅涔堣繖鏍锋湇鍔″櫒涓嶆槸涓€鐩村緱鍗犵敤浜嗕竴涓鍙e浜嶧IN_WAIT2鐘舵€佸悧锛熻繖涓嶆槸寰堟氮璐瑰悧锛?/p>
鍙傝€?/h4>
銆奣CP keepalive鐨勬帰绌?(2) : 娴忚鍣ㄧ殑Keepalive鏈哄埗銆?/p>
https://links.jianshu.com/go?to=https%3A%2F%2Fblog.chionlab.moe%2F2016%2F11%2F07%2Ftcp-keepalive-on-chrome%2F
銆奣CP淇濇椿锛圱CP keepalive锛夈€?/p>
https://links.jianshu.com/go?to=https%3A%2F%2Fblog.csdn.net%2Fchenlycly%2Farticle%2Fdetails%2F81030671
銆妕cpdump flags銆?/p>
https://links.jianshu.com/go?to=https%3A%2F%2Ffwknowledge.wordpress.com%2F2013%2F03%2F05%2Ftcpdump-flags%2F
銆奌TTP Keep-Alive鏄粈涔堬紵濡備綍宸ヤ綔锛熴€?/p>
https://links.jianshu.com/go?to=http%3A%2F%2Fwww.nowamagic.net%2Facademy%2Fdetail%2F23350305
銆婅秴鏃朵笌閲嶈瘯鏈哄埗锛堜竴锛夈€?/p>
https://links.jianshu.com/go?to=https%3A%2F%2Ftoutiao.io%2Fposts%2F38jggw%2Fpreview
-娣卞叆鍘熺悊-
鐭ュ叾鐒跺苟鐭ュ叾鎵€浠ョ劧
以上是关于HTTP keepalive璇﹁В的主要内容,如果未能解决你的问题,请参考以下文章