{"id":21,"date":"2019-01-22T09:43:34","date_gmt":"2019-01-22T01:43:34","guid":{"rendered":"http:\/\/111.230.15.63\/?p=21"},"modified":"2019-01-22T09:53:52","modified_gmt":"2019-01-22T01:53:52","slug":"nginx%e5%81%9a%e5%89%8d%e7%ab%afproxy%e6%97%b6time_wait%e8%bf%87%e5%a4%9a%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/rtbdev.com\/?p=21","title":{"rendered":"Nginx\u505a\u524d\u7aefProxy\u65f6TIME_WAIT\u8fc7\u591a\u7684\u95ee\u9898"},"content":{"rendered":"<p>\u6211\u4eec\u7684DSP\u7cfb\u7edf\u76ee\u524d\u57fa\u672c\u975e\u51cc\u6668\u65f6\u6bb5\u7684QPS\u90fd\u572810W\u4ee5\u4e0a\uff0c\u6211\u4eec\u4f7f\u7528Golang\u6765\u5904\u7406\u8fd9\u4e9bHTTP\u8bf7\u6c42\uff0cWeb\u670d\u52a1\u5668\u7684\u524d\u7aef\u7528Nginx\u6765\u505a\u8d1f\u8f7d\u5747\u8861\uff0c\u901a\u8fc7Nginx\u7684proxy_pass\u6765\u4e0eGolang\u4ea4\u4e92\u3002<\/p>\n<p>\u7531\u4e8enginx\u4ee3\u7406\u4f7f\u7528\u4e86\u77ed\u94fe\u63a5\u7684\u65b9\u5f0f\u548c\u540e\u7aef\u4ea4\u4e92\u7684\u539f\u56e0\uff0c\u4f7f\u5f97\u7cfb\u7edfTIME_WAIT\u7684tcp\u8fde\u63a5\u5f88\u591a\uff1a<\/p>\n<pre>shell&gt; netstat -n | awk '\/^tcp\/ {++state[$NF]} END {for(key in state) print key,\"\\t\",state[key]}'\nTIME_WAIT 250263\nCLOSE_WAIT 57\nFIN_WAIT2 3\nESTABLISHED 2463\nSYN_RECV 8<\/pre>\n<p>ss \u6bd4 netstat \u8981\u5feb\uff0c\u6240\u4ee5\u4e5f\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u6765\u67e5\u770b\uff1a<\/p>\n<pre>shell&gt; ss -ant | awk 'NR&gt;1 {++s[$1]} END {for(k in s) print k,s[k]}'<\/pre>\n<p>\u8fd9\u4f1a\u5360\u7528\u7cfb\u7edf\u8fc7\u591a\u7684\u7aef\u53e3\uff0c\u8fd8\u6d6a\u8d39\u4e86\u7cfb\u7edf\u8d44\u6e90\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u60f3\u529e\u6cd5\u51cf\u5c11TIME_WAIT\u3002<\/p>\n<p>TIME_WAIT\u5bfc\u81f4\u5360\u7528\u8fc7\u591a\u7aef\u53e3\u7684\u4e00\u4e2a\u5f71\u54cd\u662f\u4f1a\u5360\u7528\u6211\u4eec\u9700\u8981\u4f7f\u7528\u5230\u7684\u7aef\u53e3\uff0c\u4f8b\u5982\u6211\u4eec\u6709\u4e2a\u670d\u52a1\u76d1\u542c\u7684\u7aef\u53e3\u4e3a8012\uff0c\u91cd\u542f\u7684\u65f6\u5019\u7ecf\u5e38\u4f1a\u63d0\u793a\u7aef\u53e3\u88ab\u5360\u7528\u3002<br \/>\n\u901a\u8fc7\u67e5\u770b\/proc\/sys\/net\/ipv4\/ip_local_port_range\u53ef\u4ee5\u77e5\u9053\u8bbe\u7f6e\u7684Linux\u5185\u6838\u81ea\u52a8\u5206\u914d\u7aef\u53e3\u7684\u7aef\u53e3\u8303\u56f4\uff1a<\/p>\n<pre>shell&gt; cat \/proc\/sys\/net\/ipv4\/ip_local_port_range\n1025 65535<\/pre>\n<p>\u5bf9\u4e8e\u8fd9\u4e2a\u8bbe\u7f6e\u7cfb\u7edf\u5c31\u4f1a\u4ece1025~65535\u8fd9\u4e2a\u8303\u56f4\u5185\u968f\u673a\u5206\u914d\u7aef\u53e3\u6765\u7528\u4e8e\u8fde\u63a5\uff0c\u800c\u6211\u4eec\u670d\u52a1\u76848012\u7aef\u53e3\u521a\u597d\u5728\u8fd9\u4e2a\u8303\u56f4\u4e4b\u5185\uff0c\u6240\u4ee5\u5982\u679c8012\u521a\u597d\u88ab\u7cfb\u7edf\u5206\u914d\u51fa\u53bb\u4f7f\u7528\u4e86\uff0c\u5219\u542f\u52a8\u6211\u4eec\u670d\u52a1\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u63d0\u793a\u7aef\u53e3\u88ab\u5360\u7528\u3002<\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\/proc\/sys\/net\/ipv4\/ip_local_reserved_ports\u6765\u544a\u8bc9\u7cfb\u7edf\u7ed9\u6211\u4eec\u9884\u7559\u54ea\u4e9b\u7aef\u53e3\uff0c\u4e0d\u53ef\u4ee5\u7528\u4e8e\u81ea\u52a8\u5206\u914d\u3002<\/p>\n<pre>shell&gt; vim \/etc\/sysctl.conf\nnet.ipv4.ip_local_reserved_ports = 8012,11211-11220\n\nshell&gt; sysctl -p<\/pre>\n<p>\u5173\u4e8e\u9884\u7559\u7aef\u53e3\u7684\u66f4\u5177\u4f53\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003\uff1a<\/p>\n<p>\u7f51\u7edc\u7aef\u53e3\u9884\u7559<br \/>\n\u9884\u7559\u7aef\u53e3\u907f\u514d\u5360\u7528ip_local_reserved_ports<br \/>\n\u4e0a\u9762\u6211\u4eec\u89e3\u51b3\u4e86\u7aef\u53e3\u5360\u7528\u95ee\u9898\uff0c\u4f46\u662f\u6211\u4eec\u8fd8\u662f\u9700\u8981\u89e3\u51b3TIME_WAIT\u8fc7\u591a\u7684\u95ee\u9898\u3002<br \/>\nNginx 1.1\u4ee5\u4e0a\u7248\u672c\u7684upstream\u5df2\u7ecf\u652f\u6301keep-alive\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5f00\u542fNginx proxy\u7684keep-alive\u6765\u51cf\u5c11tcp\u8fde\u63a5\uff1a<\/p>\n<pre>\nupstream http_backend {\n server 127.0.0.1:8080;\n\nkeepalive 16;\n}\n\nserver {\n ...\n\nlocation \/http\/ {\n proxy_pass http:\/\/http_backend;\n proxy_http_version 1.1;\n proxy_set_header Connection \"\";\n ...\n }\n}\n<\/pre>\n<p>\u53ef\u4ee5\u53c2\u52a0Nginx\u7684\u5b98\u65b9\u6587\u6863\uff1a\u00a0<a href=\"http:\/\/nginx.org\/cn\/docs\/http\/ngx_http_upstream_module.html#keepalive\">http:\/\/nginx.org\/cn\/docs\/http\/ngx_http_upstream_module.html#keepalive<\/a><\/p>\n<p>\u5f00\u4e86keep-alive\u4e4b\u540e\uff0cTIME_WAIT\u660e\u663e\u51cf\u5c11\uff1a<\/p>\n<pre>\nshell&gt; netstat -n | awk '\/^tcp\/ {++state[$NF]} END {for(key in state) print key,\"\\t\",state[key]}'\nTIME_WAIT 12612\nCLOSE_WAIT 11\nFIN_WAIT1 4\nFIN_WAIT2 1\nESTABLISHED 7667\nSYN_RECV 3\n<\/pre>\n<p>\u53e6\u5916\u4e0d\u5c11\u6587\u7ae0\u63d0\u5230\u53ef\u4ee5\u4fee\u6539\u7cfb\u7edf\u7684\/etc\/sysctl.conf\u914d\u7f6e\u6765\u51cf\u5c11TIME_WAIT\u7684tcp\u8fde\u63a5\uff1a<\/p>\n<pre>net.ipv4.tcp_tw_reuse = 1\nnet.ipv4.tcp_tw_recycle = 1<\/pre>\n<p>\u53c2\u89c1\uff1a\u00a0<a href=\"http:\/\/blog.s135.com\/post\/271\/\">http:\/\/blog.s135.com\/post\/271\/<\/a><\/p>\n<p>\u4e0d\u8fc7\u5f00\u542ftcp_tw_recycle\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u4e9b\u4e0d\u7a33\u5b9a\u7684\u7f51\u7edc\u95ee\u9898\uff0c\u8bf7\u53c2\u8003\uff1a<\/p>\n<p>\u8bb0\u4e00\u6b21TIME_WAIT\u7f51\u7edc\u6545\u969c<br \/>\n\u518d\u53d9TIME_WAIT<br \/>\n\u5173\u4e8esysctl\u76f8\u5173\u914d\u7f6e\u7684\u8bf4\u660e\uff0c\u8bf7\u53c2\u8003\uff1a<br \/>\n<a href=\"https:\/\/www.kernel.org\/doc\/Documentation\/networking\/ip-sysctl.txt\">https:\/\/www.kernel.org\/doc\/Documentation\/networking\/ip-sysctl.txt<\/a><\/p>\n<p>\u53c2\u8003\u6587\u7ae0\uff1a<\/p>\n<p>\u90e8\u5206\u7f51\u7edc\u5185\u6838\u53c2\u6570\u8bf4\u660e<br \/>\n<a href=\"http:\/\/performancewiki.com\/linux-tuning.html\">http:\/\/performancewiki.com\/linux-tuning.html<\/a><br \/>\ntcp\u534f\u8baetimestamp\u5b57\u6bb5\u5bfc\u81f4\u95ee\u9898\u5206\u6790<br \/>\n<a href=\"http:\/\/www.lognormal.com\/blog\/2012\/09\/27\/linux-tcpip-tuning\/\">http:\/\/www.lognormal.com\/blog\/2012\/09\/27\/linux-tcpip-tuning\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u7684DSP\u7cfb\u7edf\u76ee\u524d\u57fa\u672c\u975e\u51cc\u6668\u65f6\u6bb5\u7684QPS\u90fd\u572810W\u4ee5\u4e0a\uff0c\u6211\u4eec\u4f7f\u7528Golang\u6765\u5904\u7406\u8fd9\u4e9bHTTP\u8bf7\u6c42\uff0cWeb\u670d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[8],"_links":{"self":[{"href":"https:\/\/rtbdev.com\/index.php?rest_route=\/wp\/v2\/posts\/21"}],"collection":[{"href":"https:\/\/rtbdev.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rtbdev.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rtbdev.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rtbdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21"}],"version-history":[{"count":2,"href":"https:\/\/rtbdev.com\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":24,"href":"https:\/\/rtbdev.com\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions\/24"}],"wp:attachment":[{"href":"https:\/\/rtbdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rtbdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rtbdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}