{"id":1090,"date":"2018-06-29T07:26:02","date_gmt":"2018-06-29T07:26:02","guid":{"rendered":"http:\/\/www.ipcpu.com\/?p=1090"},"modified":"2022-09-14T04:50:42","modified_gmt":"2022-09-14T04:50:42","slug":"nginx-high-connection","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2018\/06\/nginx-high-connection\/","title":{"rendered":"\u9ad8\u5e76\u53d1\u72b6\u6001\u4e0bnginx\u7684\u914d\u7f6e(20\u4e07\u5e76\u53d1)"},"content":{"rendered":"

\u9ad8\u5e76\u53d1\u72b6\u6001\u4e0bnginx\u7684\u914d\u7f6e(20\u4e07\u5e76\u53d1).md<\/p>\n

\u4e00\u3001\u6982\u8ff0<\/h2>\n

\u6700\u8fd1\u4e1a\u52a1\u91cf\u6bd4\u8f83\u5927\uff0cnginx\u51fa\u73b0\u4e86\u5341\u516d\u4e03\u4e07\u5c06\u8fd1\u4e8c\u5341\u4e07\u5e76\u53d1\uff08ESTABLISH\u8fde\u63a5\u6570\uff09\u7684\u60c5\u51b5\uff0c\u6240\u4ee5\u5bf920\u4e07\u4ee5\u4e0b\u5e76\u53d1\uff0cnginx\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u914d\u7f6e\u8fdb\u884c\u4e86\u6574\u7406\uff1a<\/p>\n

\u4e8c\u3001\u64cd\u4f5c\u7cfb\u7edf\u57fa\u7840\u4f18\u5316<\/h2>\n

\u7f51\u4e0a\u4e00\u5927\u5806\uff0c\u57fa\u672c\u5c31\u662f\u6539sysctl.conf\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f<\/p>\n

\n
net.ipv4.tcp_max_tw_buckets = 940000\r\nnet.ipv4.tcp_tw_recycle=0\r\nnet.ipv4.ip_local_port_range = 1024 65000\r\n#\u7aef\u53e3\u8303\u56f4\u5c3d\u91cf\u5927\u4e00\u4e9b<\/code><\/pre>\n<\/div>\n

\u4e09\u3001nf_conntrack\u7684\u914d\u7f6e<\/h2>\n

\u5982\u679c\u542f\u7528\u4e86nf_conntrack\uff0c\u5e76\u4e14\u914d\u7f6e\u7684\u6570\u503c\u8f83\u5c0f\uff0c\u90a3\u4e48\u4f1a\u9047\u5230<\/p>\n

\n
nf_conntrack: table full, dropping packet<\/code><\/pre>\n<\/div>\n

\u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u5efa\u8bae\u5173\u95ednf_conntrack\uff0c\u8bbe\u7f6eNOTRACK\u6216\u8005\u8c03\u5927\u6570\u503c\u3002
\n\u505aopenstack\u4e91\u8ba1\u7b97\u7684\u540c\u4e8b\u63a8\u8350\u914d\u7f6e<\/p>\n

\n
nf_conntrack_max = 4194304 \r\nnf_conntrack_buckets = 1048576\r\nnf_conntrack_tcp_timeout_time_wait = 60<\/code><\/pre>\n<\/div>\n

\u4e5f\u53ef\u4ee5\u53c2\u7167\u672c\u7ad9\u4ee5\u524d\u7684\u6587\u7ae0\uff1a
\nhttps:\/\/www.ipcpu.com\/2015\/06\/nf_conntrack-table-full\/<\/a><\/p>\n

\u56db\u3001\u6587\u4ef6\u6253\u5f00\u6570\u914d\u7f6e<\/h2>\n

\u5982\u679c\u4f60\u7684\u6587\u4ef6\u6253\u5f00\u6570\u662f65535\uff0c\u90a3\u4e48\u5e94\u8be5\u5148\u9047\u5230\u7684\u9519\u8bef\u662f\"Too many open files\"<\/p>\n

\n
 error: 2018\/06\/15 17:03:51 [crit] 3325#0: accept4() failed (24: Too many open files)<\/code><\/pre>\n<\/div>\n

\u9700\u8981\u8c03\u6574\u64cd\u4f5c\u7cfb\u7edf\u7684\/etc\/security\/limits.conf\u4ee5\u53ca\u9644\u5c5e\u6587\u4ef6\u3002
\n\u5728nginx\u5c42\u9762\u67092\u4e2a\u53c2\u6570\u4e5f\u9700\u8981\u8c03\u6574<\/p>\n

\n
worker_rlimit_nofile 512000;\r\n##@@worker\u8fdb\u7a0b\u7684\u6700\u5927\u6253\u5f00\u6587\u4ef6\u6570\u9650\u5236\u3002\r\nworker_connections 151200;\r\n##@@worker\u8fdb\u7a0b\u53ef\u4ee5\u4fdd\u6301\u7684\u6700\u5927\u5e76\u53d1\u8fde\u63a5\u3002\u5b98\u65b9\u6587\u6863\u660e\u786e\u6807\u51fa\uff0c\u4f5c\u4e3a\u4ee3\u7406\u4f7f\u7528\u65f6\uff0c\u8fd9\u4e2a\u503c\u5305\u542b\"nginx\u4e0e\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\"\u548c\"nginx\u4e0eupstream\u7684\u8fde\u63a5\"\u3002<\/code><\/pre>\n<\/div>\n

\u7f51\u4e0a\u6709\u4e2a\u516c\u5f0f\uff1a
\nmax_clients = worker_processes * worker_connections \/ 2;
\n(\u90e8\u5206\u6587\u6863\u662f\u9664\u4ee54\uff0c\u662f\u8003\u8651\u6d4f\u89c8\u5668\u5e76\u53d1\u6570\u4e3a2)
\n\u8fd9\u4e2a\u53c2\u6570\u89e3\u51b3\u4e86\uff0c\u57fa\u672c\u4e0a\u4e03\u516b\u4e07\u7684ESTABLISH\u662f\u6ca1\u95ee\u9898\u4e86\u3002<\/p>\n

\u4e94\u3001\u7aef\u53e3\u8017\u5c3d\u7684\u95ee\u9898<\/h2>\n

\u5982\u679c\u5e76\u53d1\u6570\u7ee7\u7eed\u5f80\u4e0a\u589e\u957f\uff0c\u53ef\u80fd\u51fa\u73b0\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n

\n
2018\/06\/16 08:37:35 [crit] 27744#0: *137027418 connect() to 10.140.2.47:443 failed (99: Cannot assign requested address) while connecting to upstream, client: 10.140.2.200, server: 0.0.0.0:9443, upstream: \"10.140.2.47:443\", bytes from\/to client:0\/0, bytes from\/to upstream:0\/0<\/code><\/pre>\n<\/div>\n

\u5f88\u660e\u663e\u7aef\u53e3\u8017\u5c3d\uff0c\u6ca1\u6709\u53ef\u4ee5\u8d44\u6e90\u4e86\u3002
\n\u8fd9\u91cc\u7684\u7aef\u53e3\u8017\u5c3d\uff0c\u5e76\u4e0d\u662f\u63071-65535\u7aef\u53e3\uff0c\u800c\u662fsocket\u4e94\u5143\u7ec4\uff0c\u5982\u4e0b<\/p>\n

<\/p>\n

\u5bf9\u4e8e\u5ba2\u6237\u7aef\u8bbf\u95eenginx\u7684\u8fde\u63a5\u6765\u8bf4\uff0c<\/p>\n

\n

\u6e90IP\u548c\u6e90\u7aef\u53e3\u90fd\u662f\u968f\u673a\u7684\uff0c\u76ee\u7684IP\uff08nginx\u672c\u673aIP\uff09\u548c\u76ee\u7684\u7aef\u53e3\uff08nginx\u672c\u673a\u670d\u52a1\u7aef\u53e3\uff09\u662f\u56fa\u5b9a\u7684\uff0c\u8fd9\u6837\u7406\u8bba\u4e0a\u6765\u8bf4\uff0c\u662f\u6ca1\u6709\u9650\u5236\u7684\u3002<\/p>\n<\/blockquote>\n

\u5bf9\u4e8enginx\u8bbf\u95eeupstream\u6765\u8bf4\uff0c<\/p>\n

\n

\u6e90IP\u56fa\u5b9a\uff08nginx\u672c\u673aIP\uff09\uff0c\u6e90\u7aef\u53e3\u968f\u673a\uff0c\u76ee\u7684IP\u548c\u76ee\u7684\u7aef\u53e3\uff08upstream\u5730\u5740\uff09\u9700\u8981\u770bupstream\u7684\u6570\u91cf\uff0c\u603b\u6570\u5c31\u662f6\u4e07\u968f\u673a\u7aef\u53e3\u4e58\u4ee5upstream\u7684\u4e2a\u6570\u3002<\/p>\n<\/blockquote>\n

\u5982\u679cupstream\u7684\u4e2a\u6570\u5f88\u5c11\uff0c\u90a3\u5c31\u4f1a\u51fa\u73b0\u7aef\u53e3\u8017\u5c3d\uff0c\u4f8b\u59825\u4e2aupstream\u5730\u5740\uff0c\u90a3\u4e48\u603b\u6570\u5c31\u662f6\u4e07*5=30\u4e07\uff08\u5b9e\u9645\u53ea\u670980-90%\uff09\u5927\u7ea625\u4e07\u3002<\/p>\n

\u6211\u4eec\u5728nginx\u670d\u52a1\u5668\u4e0a\u4f7f\u7528 \"ss -n\" \u4e5f\u53d1\u73b0\u786e\u5b9e\u662f\u6309\u7167\u4e94\u5143\u7ec4\u4f7f\u7528\u7684\uff0c\u4f8b\u5982\u4e0b\u9762\u768413958\u672c\u673a\u7aef\u53e3\u5c31\u662f\u7528\u4e86\u4e24\u6b21\uff0c\u4ea7\u751f\u4e86\u4e24\u4e2asocket\u8fde\u63a5\u3002<\/p>\n

\n
ESTAB 0 0 172.28.4.173:13958 172.28.4.161:3000               \r\nESTAB 0 0 172.28.4.173:13958 172.28.4.162:3000 <\/code><\/pre>\n<\/div>\n

\u90a3\u4e48\u5982\u4f55\u89e3\u51b3\u7aef\u53e3\u8017\u5c3d\u7684\u95ee\u9898\u5462\uff1f<\/strong>
\n\u4e00\u65b9\u9762\u53ef\u4ee5\u5728upstream\u673a\u5668\u4e0a\u589e\u52a0IP\u5730\u5740\u6216\u8005\u7aef\u53e3\uff0c
\n\u53e6\u4e00\u65b9\u9762\u4e5f\u53ef\u4ee5\u5728nginx\u4e3b\u673a\u4e0a\u589e\u52a0IP\u5730\u5740\uff0c\u7136\u540e\u4f7f\u7528nginx\u7684proxy_bind\u6307\u5b9a\u6e90\u5730\u5740\u3002<\/p>\n

\u4e94\u3001\u7cfb\u7edf\u5185\u5b58<\/h2>\n

\u5728\u64cd\u4f5c\u7cfb\u7edf\u5c42\u9762\u6bcf\u4e2aTCP\u8fde\u63a5\u4f1a\u5360\u75283k-10k\u7684\u5185\u5b58\uff0c\u4ee520\u4e07\u6765\u8ba1\u7b97\uff0c\u9700\u89812G\u5185\u5b58\u3002nginx\u7a0b\u5e8f\u672c\u8eab\u8fd8\u8981\u6d88\u8017\u5185\u5b58\uff0c\u7279\u522b\u662fnginx\u53cd\u5411\u4ee3\u7406POST\u8bf7\u6c42\u6bd4\u8f83\u591a\u7684\u60c5\u51b5\uff0c20\u4e07\u8fde\u63a5\u60c5\u51b5\u4e0b\u63a8\u835016G\u5185\u5b58\u914d\u7f6e\u3002<\/p>\n

\u516d\u3001\u6df1\u5ea6\u8ffd\u95ee<\/h2>\n

TCP\u8fde\u63a5\u4f1a\u4e0d\u4f1a\u5360\u7528\u6587\u4ef6\u63cf\u8ff0\u7b26\uff1f<\/h4>\n

\u5904\u5728ESTABLISH\u72b6\u6001\u7684TCP\u8fde\u63a5\uff0c\u662f\u4f1a\u5360\u7528\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u3002\u800c\u5904\u5728TIME_WAIT\u7b49\u975e\u8fde\u63a5\u72b6\u6001\u7684TCP\u8fde\u63a5\u4e0d\u4f1a\u5360\u7528\u3002
\n\u6ce8\u610f\uff1aCLOSE_WAIT\u72b6\u6001\u4e5f\u4f1a\u5360\u7528\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08\u600e\u4e48\u770b\uff1flsof !\uff09<\/p>\n

\u5173\u4e8eTIME_WAIT\u6570\u91cf\u8fc7\u5927\u4f1a\u6709\u4ec0\u4e48\u5f71\u54cd\uff1f<\/h4>\n

\u8fd9\u4e2a\u62c5\u5fc3\u662f\u591a\u4f59\u7684\uff0c\u6211\u76ee\u524d\u673a\u5668\u4e0aTIME_WAIT\u6570\u91cf\u572860\u4e07\u5de6\u53f3\uff0ctcp_max_tw_buckets \u8bbe\u7f6e\u7684\u662f94\u4e07\uff0c\u7531\u4e8eTIME_WAIT\u5360\u7528\u5185\u5b58\u5f88\u5c0f\uff0c\u5e76\u4e14\u4e0d\u5360\u7528\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u76ee\u524d\u6ca1\u53d1\u73b0\u6709\u4ec0\u4e48\u95ee\u9898\u3002\u552f\u4e00\u9700\u8981\u62c5\u5fc3\u7684\u5c31\u662fTIME_WAIT\u8017\u5c3d\u4e86nginx\u8bbf\u95eeupstream\u65f6\u5019\u7684\u4e94\u5143\u7ec4\uff0c\u8fd9\u4e2a\u62c5\u5fc3\u53ef\u4ee5\u901a\u8fc7\u5728nginx\u4e3b\u673a\u4e0a\u589e\u52a0IP\u5730\u5740\u89e3\u51b3\uff08\u540c\u7aef\u53e3\u8017\u5c3d\uff09\u3002
\n<\/p>\n

\u5173\u4e8eupstream\u4e2d\u7684keepalive\u9009\u9879\uff0c\u4ec0\u4e48\u610f\u601d\uff1f<\/h4>\n

\u7ebf\u4e0a\u7684nginx\u4ee3\u7406\u670d\u52a1\u5668upstream\u4e2d\u7684keepalive\u9009\u9879\u8bbe\u7f6e\u4e3a64\uff0c\u4f46\u5b9e\u9645\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u548c\u540e\u7aef\u5355\u53f0\u670d\u52a1\u5668\u4ea7\u751f\u4e86\u4e00\u4e07\u591aESTAB\u8fde\u63a5\u3002\u67e5\u770b\u4e86nginx\u5b98\u65b9\u6587\u6863\uff0c\u53d1\u73b0\u8fd9\u4e2a\u503c\u662f\u6700\u5927\u7684idle(\u7a7a\u95f2)\u6570\u636e\uff0c\u53ea\u6709\u7528\u5b8c\u7684\u8fde\u63a5\u624d\u4f1a\u56de\u6536\u3002<\/p>\n

\n
The connections parameter sets the maximum number of idle keepalive connections to upstream servers that are preserved in the cache of each worker process. When this number is exceeded, the least recently used connections are closed.<\/code><\/pre>\n<\/div>\n

\u53c2\u8003\u8d44\u6599<\/h2>\n

https:\/\/work-jlsun.github.io\/2016\/09\/01\/tunning-nginx-performance.html<\/a>
\n
http:\/\/nginx.org\/en\/docs\/http\/ngx_http_upstream_module.html<\/a>
\n
https:\/\/www.cs.utah.edu\/~mflatt\/past-courses\/cs5460\/lecture17.pdf<\/a><\/p>\n

<\/p>\n

<\/div>\n
<\/div>\n
\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n<\/div>\n
\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n<\/div>\n

<\/wiz_tmp_tag><\/p>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » \u9ad8\u5e76\u53d1\u72b6\u6001\u4e0bnginx\u7684\u914d\u7f6e(20\u4e07\u5e76\u53d1)<\/a><\/p>","protected":false},"excerpt":{"rendered":"

\u9ad8\u5e76\u53d1\u72b6\u6001\u4e0bnginx\u7684\u914d\u7f6e(20\u4e07\u5e76\u53d1).md \u4e00\u3001\u6982\u8ff0 \u6700\u8fd1\u4e1a\u52a1\u91cf\u6bd4\u8f83\u5927\uff0cnginx\u51fa\u73b0\u4e86\u5341\u516d\u4e03\u4e07\u5c06\u8fd1\u4e8c\u5341\u4e07\u5e76\u53d1\uff08ESTABLISH\u8fde\u63a5\u6570\uff09\u7684\u60c5\u51b5\uff0c\u6240\u4ee5\u5bf920\u4e07\u4ee5\u4e0b\u5e76\u53d1\uff0cnginx\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u914d\u7f6e\u8fdb\u884c\u4e86\u6574\u7406\uff1a \u4e8c\u3001\u64cd\u4f5c\u7cfb\u7edf\u57fa\u7840\u4f18\u5316 \u7f51\u4e0a\u4e00\u5927\u5806\uff0c\u57fa\u672c\u5c31\u662f\u6539sysctl.conf\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f net.ipv4.tcp_max_tw_buckets = 940000 net.ipv4.tcp_tw_recycle=0 net.ipv4.ip_local_port_range = 1024 65000 #\u7aef\u53e3\u8303\u56f4\u5c3d\u91cf\u5927\u4e00\u4e9b \u4e09\u3001nf_conntrack\u7684\u914d\u7f6e \u5982\u679c\u542f\u7528\u4e86nf_conntrack\uff0c\u5e76\u4e14\u914d\u7f6e\u7684\u6570\u503c\u8f83\u5c0f\uff0c\u90a3\u4e48\u4f1a\u9047\u5230 nf_conntrack: table full, dropping packet \u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u5efa\u8bae\u5173\u95ednf_conntrack\uff0c\u8bbe\u7f6eNOTRACK\u6216\u8005\u8c03\u5927\u6570\u503c\u3002 \u505aopenstack\u4e91\u8ba1\u7b97\u7684\u540c\u4e8b\u63a8\u8350\u914d\u7f6e nf_conntrack_max = 4194304 nf_conntrack_buckets = 1048576 nf_conntrack_tcp_timeout_time_wait = 60 \u4e5f\u53ef\u4ee5\u53c2\u7167\u672c\u7ad9\u4ee5\u524d\u7684\u6587\u7ae0\uff1a https:\/\/www.ipcpu.com\/2015\/06\/nf_conntrack-table-full\/ \u56db\u3001\u6587\u4ef6\u6253\u5f00\u6570\u914d\u7f6e \u5982\u679c\u4f60\u7684\u6587\u4ef6\u6253\u5f00\u6570\u662f65535\uff0c\u90a3\u4e48\u5e94\u8be5\u5148\u9047\u5230\u7684\u9519\u8bef\u662f”Too many open files” error: 2018\/06\/15 17:03:51 [crit] 3325#0: accept4() failed (24: Too many open files) \u9700\u8981\u8c03\u6574\u64cd\u4f5c\u7cfb\u7edf\u7684\/etc\/security\/limits.conf\u4ee5\u53ca\u9644\u5c5e\u6587\u4ef6\u3002 \u5728nginx\u5c42\u9762\u67092\u4e2a\u53c2\u6570\u4e5f\u9700\u8981\u8c03\u6574 worker_rlimit_nofile 512000; […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,13],"tags":[17,181,14,44,18,127],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1090"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=1090"}],"version-history":[{"count":4,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1090\/revisions"}],"predecessor-version":[{"id":1136,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1090\/revisions\/1136"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}