{"id":49852,"date":"2024-10-08T08:02:16","date_gmt":"2024-10-08T00:02:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/49852\/"},"modified":"2024-10-08T08:02:16","modified_gmt":"2024-10-08T00:02:16","slug":"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e9%85%8d%e7%bd%ae%e6%8c%81%e4%b9%85%e9%80%a3%e6%8e%a5%e6%b1%a0%e8%88%87-upstream-keepalive","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/49852\/","title":{"rendered":"Nginx \u6280\u5de7\uff1a\u914d\u7f6e\u6301\u4e45\u9023\u63a5\u6c60\u8207 upstream keepalive"},"content":{"rendered":"<h1 id=\"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e9%85%8d%e7%bd%ae%e6%8c%81%e4%b9%85%e9%80%a3%e6%8e%a5%e6%b1%a0%e8%88%87-upstream-keepalive-zPqtNVOJeM\">Nginx \u6280\u5de7\uff1a\u914d\u7f6e\u6301\u4e45\u9023\u63a5\u6c60\u8207 upstream keepalive<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u8ca0\u8f09\u5e73\u8861\u548c\u53cd\u5411\u4ee3\u7406\u7b49\u5834\u666f\u3002\u96a8\u8457\u7db2\u7ad9\u6d41\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u7ba1\u7406\u4f3a\u670d\u5668\u8cc7\u6e90\u6210\u70ba\u4e86\u6bcf\u500b\u7cfb\u7d71\u7ba1\u7406\u54e1\u9700\u8981\u9762\u5c0d\u7684\u6311\u6230\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u914d\u7f6e Nginx \u7684\u6301\u4e45\u9023\u63a5\u6c60\u53ca upstream keepalive\uff0c\u4ee5\u63d0\u5347\u4f3a\u670d\u5668\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%8c%81%e4%b9%85%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-zPqtNVOJeM\">\u4ec0\u9ebc\u662f\u6301\u4e45\u9023\u63a5\u6c60\uff1f<\/h2>\n<p>\u6301\u4e45\u9023\u63a5\u6c60\uff08Persistent Connection Pool\uff09\u662f\u6307\u5728\u5ba2\u6236\u7aef\u8207\u4f3a\u670d\u5668\u4e4b\u9593\u4fdd\u6301\u6301\u4e45\u7684 TCP \u9023\u63a5\uff0c\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u6bcf\u6b21\u8acb\u6c42\u6642\u5efa\u7acb\u548c\u95dc\u9589\u9023\u63a5\u7684\u958b\u92b7\u3002\u9019\u5c0d\u65bc\u9ad8\u983b\u7387\u7684\u8acb\u6c42\u7279\u5225\u91cd\u8981\uff0c\u56e0\u70ba\u5b83\u80fd\u986f\u8457\u63d0\u9ad8\u97ff\u61c9\u901f\u5ea6\u548c\u6e1b\u5c11\u5ef6\u9072\u3002<\/p>\n<h2 id=\"upstream-keepalive-%e7%9a%84%e6%a6%82%e5%bf%b5-zPqtNVOJeM\">Upstream Keepalive \u7684\u6982\u5ff5<\/h2>\n<p>Upstream keepalive \u662f Nginx \u4e2d\u7684\u4e00\u500b\u529f\u80fd\uff0c\u5141\u8a31 Nginx \u5728\u8207\u4e0a\u6e38\u4f3a\u670d\u5668\uff08\u5982\u61c9\u7528\u4f3a\u670d\u5668\u6216\u8cc7\u6599\u5eab\u4f3a\u670d\u5668\uff09\u4e4b\u9593\u4fdd\u6301\u6301\u4e45\u9023\u63a5\u3002\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u6bcf\u6b21\u8acb\u6c42\u6642\u7684\u9023\u63a5\u5efa\u7acb\u6642\u9593\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e9%85%8d%e7%bd%ae-nginx-%e7%9a%84%e6%8c%81%e4%b9%85%e9%80%a3%e6%8e%a5%e6%b1%a0-zPqtNVOJeM\">\u914d\u7f6e Nginx \u7684\u6301\u4e45\u9023\u63a5\u6c60<\/h2>\n<p>\u8981\u914d\u7f6e Nginx \u7684\u6301\u4e45\u9023\u63a5\u6c60\uff0c\u9996\u5148\u9700\u8981\u5728 Nginx \u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u8a2d\u7f6e upstream \u6a21\u584a\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u57fa\u672c\u7684\u914d\u7f6e\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>http {\n    upstream backend {\n        server backend1.example.com;\n        server backend2.example.com;\n        keepalive 32;  # \u8a2d\u7f6e\u6301\u4e45\u9023\u63a5\u7684\u6578\u91cf\n    }\n\n    server {\n        location \/ {\n            proxy_pass http:\/\/backend;\n            proxy_http_version 1.1;\n            proxy_set_header Connection \"\";\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba `backend` \u7684 upstream\uff0c\u4e26\u8a2d\u7f6e\u4e86\u6700\u591a 32 \u500b\u6301\u4e45\u9023\u63a5\u3002\u9019\u610f\u5473\u8457 Nginx \u53ef\u4ee5\u5728\u9019\u4e9b\u9023\u63a5\u4e2d\u91cd\u8907\u4f7f\u7528\uff0c\u800c\u4e0d\u5fc5\u6bcf\u6b21\u90fd\u91cd\u65b0\u5efa\u7acb\u9023\u63a5\u3002<\/p>\n<h2 id=\"%e9%85%8d%e7%bd%ae-upstream-keepalive-zPqtNVOJeM\">\u914d\u7f6e Upstream Keepalive<\/h2>\n<p>\u8981\u555f\u7528 upstream keepalive\uff0c\u60a8\u9700\u8981\u5728 Nginx \u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0 `keepalive` \u6307\u4ee4\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff1a<\/p>\n<pre><code>http {\n    upstream backend {\n        server backend1.example.com;\n        server backend2.example.com;\n        keepalive 16;  # \u8a2d\u7f6e\u6301\u4e45\u9023\u63a5\u7684\u6578\u91cf\n    }\n\n    server {\n        location \/ {\n            proxy_pass http:\/\/backend;\n            proxy_http_version 1.1;\n            proxy_set_header Connection \"\";\n            proxy_set_header Host $host;\n            proxy_set_header X-Real-IP $remote_addr;\n            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u914d\u7f6e\u4e2d\uff0c\u6211\u5011\u540c\u6a23\u8a2d\u7f6e\u4e86\u6700\u591a 16 \u500b\u6301\u4e45\u9023\u63a5\u3002\u9019\u6a23\uff0c\u7576 Nginx \u5411\u4e0a\u6e38\u4f3a\u670d\u5668\u767c\u9001\u8acb\u6c42\u6642\uff0c\u53ef\u4ee5\u91cd\u8907\u4f7f\u7528\u9019\u4e9b\u9023\u63a5\uff0c\u5f9e\u800c\u6e1b\u5c11\u5ef6\u9072\u548c\u8cc7\u6e90\u6d88\u8017\u3002<\/p>\n<h2 id=\"%e6%80%a7%e8%83%bd%e6%b8%ac%e8%a9%a6%e8%88%87%e5%84%aa%e5%8c%96-zPqtNVOJeM\">\u6027\u80fd\u6e2c\u8a66\u8207\u512a\u5316<\/h2>\n<p>\u5728\u914d\u7f6e\u5b8c\u6210\u5f8c\uff0c\u5efa\u8b70\u9032\u884c\u6027\u80fd\u6e2c\u8a66\uff0c\u4ee5\u78ba\u4fdd\u6301\u4e45\u9023\u63a5\u6c60\u548c upstream keepalive \u7684\u914d\u7f6e\u80fd\u5920\u9054\u5230\u9810\u671f\u7684\u6548\u679c\u3002\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982 Apache Benchmark (ab) \u6216 JMeter \u9032\u884c\u58d3\u529b\u6e2c\u8a66\uff0c\u89c0\u5bdf\u97ff\u61c9\u6642\u9593\u548c\u4f3a\u670d\u5668\u8ca0\u8f09\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u6839\u64da\u5be6\u969b\u7684\u6d41\u91cf\u60c5\u6cc1\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8abf\u6574 `keepalive` \u7684\u6578\u91cf\uff0c\u4ee5\u9054\u5230\u6700\u4f73\u6027\u80fd\u3002\u904e\u591a\u7684\u6301\u4e45\u9023\u63a5\u53ef\u80fd\u6703\u5c0e\u81f4\u8cc7\u6e90\u6d6a\u8cbb\uff0c\u800c\u904e\u5c11\u5247\u53ef\u80fd\u7121\u6cd5\u5145\u5206\u5229\u7528\u4f3a\u670d\u5668\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-zPqtNVOJeM\">\u7e3d\u7d50<\/h2>\n<p>\u914d\u7f6e Nginx \u7684\u6301\u4e45\u9023\u63a5\u6c60\u8207 upstream keepalive \u662f\u63d0\u5347\u4f3a\u670d\u5668\u6027\u80fd\u7684\u91cd\u8981\u624b\u6bb5\u3002\u900f\u904e\u5408\u7406\u7684\u914d\u7f6e\uff0c\u60a8\u53ef\u4ee5\u6e1b\u5c11\u9023\u63a5\u5efa\u7acb\u7684\u958b\u92b7\uff0c\u63d0\u9ad8\u97ff\u61c9\u901f\u5ea6\uff0c\u4e26\u6709\u6548\u7ba1\u7406\u4f3a\u670d\u5668\u8cc7\u6e90\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u80fd\u7684\u7db2\u7ad9\u6216\u61c9\u7528\uff0c\u9019\u4e9b\u6280\u5de7\u7121\u7591\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u80fd\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f\u7db2\u7ad9\u6258\u7ba1\u9084\u662f\u61c9\u7528\u90e8\u7f72\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Nginx \u6280\u5de7\uff0c\u5b78\u7fd2\u5982\u4f55\u914d\u7f6e\u6301\u4e45\u9023\u63a5\u6c60\u8207 upstream keepalive\uff0c\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u8207\u97ff\u61c9\u901f\u5ea6\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-49852","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=49852"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49852\/revisions"}],"predecessor-version":[{"id":49853,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49852\/revisions\/49853"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=49852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=49852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=49852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}