{"id":253218,"date":"2022-09-28T09:43:34","date_gmt":"2022-09-28T01:43:34","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=253218"},"modified":"2022-09-28T11:01:21","modified_gmt":"2022-09-28T03:01:21","slug":"nginx-shyo-conf","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/nginx-shyo-conf.html","title":{"rendered":"Nginx\u4e0a\u6e38\u670d\u52a1\u5668\u957f\u8fde\u63a5\u914d\u7f6e"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nNginx Upstream\u8fde\u63a5\u540e\u7aef\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a\u77ed\u8fde\u63a5\u548c\u957f\u8fde\u63a5\u3002Keep-Alive\u5373\u4fd7\u79f0\u7684\u957f\u8fde\u63a5\uff0c\u4f7f\u5ba2\u6237\u7aef\u5230\u670d\u52a1\u7aef\u5efa\u7acb\u7684\u8fde\u63a5\u6301\u7eed\u6709\u6548\uff0c\u5f53\u5bf9\u670d\u52a1\u5668\u53d1\u8d77\u540e\u7eed\u8bf7\u6c42\u65f6\uff0cKeep-Alive\u529f\u80fd\u53ef\u4ee5\u590d\u7528\u5148\u524d\u4f7f\u7528\u8fc7\u7684\u8fde\u63a5\uff0c\u51cf\u5c11\u4e86\u91cd\u65b0\u5efa\u7acb\u8fde\u63a5\u6240\u9700\u7684\u5f00\u9500\u3002\u901a\u5e38\u4f7f\u7528HTTP\u534f\u8bae\u91c7\u7528\u7684\u662f\u8bf7\u6c42\u5e94\u7b54\u6a21\u5f0f\uff0c\u5ba2\u6237\u7aef\u5230\u670d\u52a1\u7aef\u7684\u4e00\u6b21\u8bf7\u6c42\u5b8c\u6210\u4e86\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u8fde\u63a5\u4e5f\u968f\u4e4b\u5173\u95ed\u3002\u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e0b\uff0c\u6bd4\u5982\u540e\u7aefRESTful\u670d\u52a1\uff0c\u5c31\u9700\u8981\u4fdd\u6301\u4f4f\u8fde\u63a5\uff0c\u8fd9\u65f6\u5c31\u53ef\u4ee5\u542f\u7528HTTP Keep-Alive\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\u3001\u6982\u8ff0<\/strong><\/div>\n

Nginx Upstream\u8fde\u63a5\u540e\u7aef\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a\u77ed\u8fde\u63a5\u548c\u957f\u8fde\u63a5\u3002Keep-Alive\u5373\u4fd7\u79f0\u7684\u957f\u8fde\u63a5\uff0c\u4f7f\u5ba2\u6237\u7aef\u5230\u670d\u52a1\u7aef\u5efa\u7acb\u7684\u8fde\u63a5\u6301\u7eed\u6709\u6548\uff0c\u5f53\u5bf9\u670d\u52a1\u5668\u53d1\u8d77\u540e\u7eed\u8bf7\u6c42\u65f6\uff0cKeep-Alive\u529f\u80fd\u53ef\u4ee5\u590d\u7528\u5148\u524d\u4f7f\u7528\u8fc7\u7684\u8fde\u63a5\uff0c\u51cf\u5c11\u4e86\u91cd\u65b0\u5efa\u7acb\u8fde\u63a5\u6240\u9700\u7684\u5f00\u9500\u3002\u901a\u5e38\u4f7f\u7528HTTP\u534f\u8bae\u91c7\u7528\u7684\u662f\u8bf7\u6c42\u5e94\u7b54\u6a21\u5f0f\uff0c\u5ba2\u6237\u7aef\u5230\u670d\u52a1\u7aef\u7684\u4e00\u6b21\u8bf7\u6c42\u5b8c\u6210\u4e86\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u8fde\u63a5\u4e5f\u968f\u4e4b\u5173\u95ed\u3002\u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e0b\uff0c\u6bd4\u5982\u540e\u7aefRESTful\u670d\u52a1\uff0c\u5c31\u9700\u8981\u4fdd\u6301\u4f4f\u8fde\u63a5\uff0c\u8fd9\u65f6\u5c31\u53ef\u4ee5\u542f\u7528HTTP Keep-Alive\u3002
\nnginx \u901a\u5e38\u60c5\u51b5\u4e0b\u90fd\u662f\u7528\u6765\u5f53\u4f5c\u4e00\u4e2a\u53cd\u5411\u4ee3\u7406\uff0c\u901a\u5e38\u4e00\u4e2a\u8bf7\u6c42\u90fd\u9700\u8981\u7ecf\u8fc7 client -> nginx -> backend_server \u8fd9\u4e48\u51e0\u6210\u5173\u7cfb\u3002\u901a\u5e38\u60c5\u51b5\u4e0b client -> nginx \u4f7f\u7528\u7684 HTTP 1.1 \u6216\u8005 2.0 \u7684\u534f\u8bae\uff0ckeep-alive \u590d\u7528\u4e86 TCP \u7684\u8fde\u63a5\uff0c\u51cf\u5c11\u4e86 TCP \u9891\u53d1\u521b\u5efa\u548c\u9500\u6bc1\u5e26\u6765\u7684\u6027\u80fd\u635f\u5931\u3002\u4f46\u662f\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cnginx -> backend_server \u662f HTTP 1.0 \u7684\u534f\u8bae\uff0c\u5e76\u6ca1\u6709\u590d\u7528 TCP \u7684\u8fde\u63a5\u3002
\n\"\"<\/a><\/p>\n

\u5bf9\u53d1\u5f80\u4e0a\u6e38\u8bf7\u6c42\u5f53\u4e2d\u8981\u52a0\u4e0ahttp\u5934\u90e8\u56fe\u7247\u91cc\u9762\u7684\u4e24\u6761\u6307\u4ee4proxy_http_version\u548cproxy_set_header\uff0c\u56e0\u4e3ahttp 1.0\u534f\u8bae\u662f\u4e0d\u652f\u6301keepalive\u957f\u8fde\u63a5\u7684\uff0c\u4e3a\u4e86\u9632\u6b62\u7528\u6237\u53d1\u6765\u7684\u662fhttp 1.0\u534f\u8bae\u6211\u4eec\u9700\u8981\u91cd\u7f6ehttp_version\u4e3a1.1\uff0c\u8fd9\u6837\u4e00\u76f4\u53ef\u4ee5\u4f7f\u7528keepalive\u957f\u8fde\u63a5\u3002\u5176\u6b21\u4e3a\u4e86\u9632\u6b62\u7528\u6237connection\u5934\u90e8\u7ed9\u6211\u4eec\u4f20\u5165\u7684\u662fclosed\u800c\u4e0d\u662fkeepalived\uff0c\u6211\u4eec\u9700\u8981\u4e3b\u52a8\u8bbe\u7f6e\u5411\u4e0a\u6e38\u53d1\u7684Connection\u503c\u4e3aConnection \"\"\u3002<\/p>\n

\u4e8c\u3001\u914d\u7f6e<\/strong><\/div>\n
1\u3001\u8bed\u6cd5<\/strong><\/span><\/div>\n
\u529f\u80fd:\u901a\u8fc7\u590d\u7528\u8fde\u63a5,\u964d\u4f4eNginx\u4e0e\u4e0a\u6e38\u670d\u52a1\u5668\u5efa\u7acb,\u5173\u95ed\u8fde\u63a5\u7684\u6d88\u8017\uff0c\u63d0\u5347\u541e\u5410\u91cf\u7684\u540c\u65f6\u964d\u4f4e\u65f6\u95f4\u5ef6\u8fdf.\r\n\u6a21\u5757\uff1angx_http_upstream_keepalive_module\u662f\u9ed8\u8ba4\u7f16\u5165\u5230Nginx\u4e2d\u7684,\u5982\u679c\u4e0d\u60f3\u7f16\u8fdb\u53bb\u53ef\u4ee5\u7528\r\n      --without-http_upstream_keepalive_module\u5bf9\u8fd9\u4e2a\u6a21\u5757\u8fdb\u884c\u79fb\u9664.\r\n(\/home\/muten\/module\/nginx-1.13.7\u4e2d\u6267\u884c.\/configure --help |more \u641c\u7d22http_upstream_keepalive_module\u53ef\u9a8c\u8bc1)\r\n\r\n\u5173\u4e8eupstream_keepalive\u7684\u6307\u4ee4\uff1a\r\n(1)keepalive\r\nActivates the cache for connections to upstream servers.\r\nThe connections parameter sets the maximum number of idle keepalive connections to \r\nupstream servers that are preserved in the cache of each worker process. When this \r\nnumber is exceeded, the least recently used connections are closed.\r\n\u8868\u793a\u4e0a\u6e38\u670d\u52a1\u5668\u4e2d\u6700\u591a\u4fdd\u6301\u591a\u5c11\u4e2a\u7528\u4e8ekeepalive\u8bf7\u6c42\u7684\u7a7a\u95f2\u7684\u8fde\u63a5.\r\n\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u4fc3\u4f7f\u4e0a\u6e38\u670d\u52a1\u5668\u4e2d\u6bcf\u4e2aworker\u8fdb\u7a0b\u4e2d\u5f00\u8f9f\u51fa\u6765\u4e00\u5757\u7f13\u5b58\u7528\u4e8ekeepalive\u8fde\u63a5\uff0c\r\n\u5f53\u8d85\u8fc7\u8fd9\u4e2a\u6570\u5b57\u7684\u65f6\u5019,\u5c06\u4f1a\u5229\u7528LRU\u7b97\u6cd5\u5c06\u4e00\u4e9b\u8fde\u63a5\u5173\u95ed.\r\n\r\n\u56e0\u4e3ahttp1.0\u534f\u8bae\u662f\u4e0d\u652f\u6301\u957f\u8fde\u63a5\u7684,\u4e3a\u4e86\u9632\u6b62\u7528\u6237\u53d1\u6765\u7684\u662fhttp1.0,\u6211\u4eec\u9700\u8981\u91cd\u7f6ehttp\u7248\u672c,\u5c06\u5176\u7f6e\u62101.1,\u8fd9\u6837\u5c31\u53ef\u4ee5\u4e00\u76f4\u4f7f\u7528keep_alive\u957f\u8fde\u63a5;\r\n\u4e3a\u4e86\u9632\u6b62\u7528\u6237\u7684Connection\u5934\u90e8\u7ed9\u6211\u4eec\u4f20\u9012\u7684\u662fClose\u800c\u4e0d\u662fKeepalive,\u6211\u4eec\u4e3b\u52a8\u8bbe\u7f6e\u5411\u4e0a\u6e38\u53d1\u9001\u7684connection.\r\n\r\nWhen using load balancing methods other than the default round-robin method, it is necessary to activate them \r\nbefore the keepalive directive.\r\n\u5f53\u4f7f\u7528\u7684\u8d1f\u8f7d\u5747\u8861\u65b9\u6cd5\u4e0d\u662f\u9ed8\u8ba4\u7684\u8f6e\u8be2\u7b97\u6cd5\u65f6\uff0c\u9700\u8981\u5728keepalive\u6307\u4ee4\u4e4b\u524d\u6fc0\u6d3b\u4ed6\u4eec\r\n\r\n\u8bed\u6cd5\uff1a\r\nSyntax: keepalive connections;\r\nDefault:    \u2014\r\nContext:    upstream\r\nThis directive appeared in version 1.1.4.\r\n\r\nSyntax: proxy_http_version 1.0 | 1.1;\r\nDefault:    \r\nproxy_http_version 1.0;\r\nContext:    http, server, location\r\nThis directive appeared in version 1.1.4.\r\n\r\nSyntax: proxy_set_header field value;\r\nDefault:    \r\nproxy_set_header Host $proxy_host;\r\nproxy_set_header Connection close;\r\nContext:    http, server, location\r\n\r\n\u5bf9\u4e0a\u6e38\u8fde\u63a5\u7684http\u5934\u90e8\u8bbe\u5b9a\uff1a\r\nproxy_http_version 1.1;\r\nproxy_set_header Connection \"\";\r\n\r\n\u914d\u7f6e\r\nupstream http_backend {\r\n    server 127.0.0.1:8080;\r\n    keepalive 16;\r\n    keepalive_timeout 60s;\r\n}\r\n\r\nserver {\r\n    ...\r\n    location \/http\/ {\r\n        proxy_pass http:\/\/http_backend;\r\n        proxy_http_version 1.1;\r\n        proxy_set_header Connection \"\";\r\n        ...\r\n    }\r\n}\r\n<\/pre>\n
2\u3001\u5b9e\u73b0\u539f\u7406<\/strong><\/span><\/div>\n

nginx upstream keepalive\u957f\u8fde\u63a5\u7684\u5b9e\u73b0\u539f\u7406\u3002\u9996\u5148\u6bcf\u4e2a\u8fdb\u7a0b\u9700\u8981\u4e00\u4e2aconnection pool\uff0c\u91cc\u9762\u90fd\u662f\u957f\u8fde\u63a5\uff0c\u591a\u8fdb\u7a0b\u4e4b\u95f4\u662f\u4e0d\u9700\u8981\u5171\u4eab\u8fd9\u4e2a\u8fde\u63a5\u6c60\u7684\u3002 \u4e00\u65e6\u4e0e\u540e\u7aef\u670d\u52a1\u5668\u5efa\u7acb\u8fde\u63a5\uff0c\u5219\u5728\u5f53\u524d\u8bf7\u6c42\u8fde\u63a5\u7ed3\u675f\u4e4b\u540e\u4e0d\u4f1a\u7acb\u5373\u5173\u95ed\u8fde\u63a5\uff0c\u800c\u662f\u628a\u7528\u5b8c\u7684\u8fde\u63a5\u4fdd\u5b58\u5728\u4e00\u4e2akeepalive connection pool\u91cc\u9762\uff0c\u4ee5\u540e\u6bcf\u6b21\u9700\u8981\u5efa\u7acb\u5411\u540e\u8fde\u63a5\u7684\u65f6\u5019\uff0c\u53ea\u9700\u8981\u4ece\u8fd9\u4e2a\u8fde\u63a5\u6c60\u91cc\u9762\u627e\uff0c\u5982\u679c\u627e\u5230\u5408\u9002\u7684\u8fde\u63a5\u7684\u8bdd\uff0c\u5c31\u53ef\u4ee5\u76f4\u63a5\u6765\u7528\u8fd9\u4e2a\u8fde\u63a5\uff0c\u4e0d\u9700\u8981\u91cd\u65b0\u521b\u5efasocket\u6216\u8005\u53d1\u8d77connect()\u3002\u8fd9\u6837\u65e2\u7701\u4e0b\u5efa\u7acb\u8fde\u63a5\u65f6\u5728\u63e1\u624b\u7684\u65f6\u95f4\u6d88\u8017\uff0c\u53c8\u53ef\u4ee5\u907f\u514dTCP\u8fde\u63a5\u7684slowstart\u3002\u5982\u679c\u5728keepalive\u8fde\u63a5\u6c60\u627e\u4e0d\u5230\u5408\u9002\u7684\u8fde\u63a5\uff0c\u90a3\u5c31\u6309\u7167\u539f\u6765\u7684\u6b65\u9aa4\u91cd\u65b0\u5efa\u7acb\u8fde\u63a5\u3002
\n\u5982\u679c\u4f60\u7684\u8fde\u63a5\u6c60\u7684\u6570\u63a7\u5236\u5728128\uff0c\u603b\u5171\u7ebf\u7a0b\u6c60\u5185\u7684\u7ebf\u7a0b\u6570\u662f128 * nginx worker \uff0c\u4f46\u56e0\u4e3a\u4f60\u8981\u5e94\u5bf9\u66f4\u591a\u7684\u5e76\u53d1\u8bf7\u6c42\uff0c\u6240\u4ee5\u4e34\u65f6\u53c8\u52a0\u4e86\u5f88\u591a\u7684\u8fde\u63a5\uff0c\u4f46\u8fd9\u4e34\u65f6\u7684\u8fde\u63a5\u662f\u77ed\u8fde\u63a5\u548c\u957f\u8fde\u63a5\u8981\u770b\u4f60\u7684nginx\u7248\u672c\uff0c1.8\u662f\u957f\u8fde\u63a5\uff0c\u90a3\u4ed6\u5982\u4f55\u88ab\u6536\u56de\uff0c\u4e24\u70b9\u4fdd\u8bc1\uff0c\u4e00\u70b9\u662f\u4ed6\u4f1a\u4e3b\u52a8\u53bb\u91ca\u653e\uff0c\u53e6\u4e00\u70b9\u662fkeepalive timeout\u7684\u65f6\u95f4<\/p>\n","protected":false},"excerpt":{"rendered":"

Nginx Upstream\u8fde\u63a5\u540e\u7aef\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a\u77ed\u8fde\u63a5\u548c\u957f\u8fde\u63a5\u3002Keep-Alive\u5373\u4fd7\u79f0\u7684\u957f\u8fde\u63a5\uff0c\u4f7f\u5ba2\u6237\u7aef\u5230 […]<\/p>\n","protected":false},"author":643,"featured_media":253224,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-253218","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=253218"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253218\/revisions"}],"predecessor-version":[{"id":253223,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253218\/revisions\/253223"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/253224"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=253218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=253218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=253218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}