{"id":1773,"date":"2023-03-26T22:16:59","date_gmt":"2023-03-26T14:16:59","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1773"},"modified":"2023-04-23T21:19:10","modified_gmt":"2023-04-23T13:19:10","slug":"nginx-load-balancing-configuration","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/26\/nginx-load-balancing-configuration\/","title":{"rendered":"Nginx\u8d1f\u8f7d\u5747\u8861\u914d\u7f6e"},"content":{"rendered":"<p>Nginx\u4e0d\u4ec5\u53ef\u4ee5\u4f5c\u4e3a\u5f3a\u5927\u7684web\u670d\u52a1\u5668\uff0c\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u800c\u4e14nginx\u8fd8\u53ef\u4ee5\u6309\u7167\u8c03\u5ea6\u89c4\u5219\u5b9e\u73b0\u52a8\u9759\u5206\u79bb\uff0c\u8fd8\u53ef\u4ee5\u5bf9\u540e\u7aef\u7684\u670d\u52a1\u5668\u505a\u8d1f\u8f7d\u5747\u8861\u3002<\/p>\n<h2>Nginx\u8d1f\u8f7d\u5747\u8861\u914d\u7f6e<\/h2>\n<p>Nginx\u7684\u8d1f\u8f7d\u5747\u8861\u4e3b\u8981\u662f\u5bf9<code>proxy_pass<\/code>\u548c<code>upstream<\/code>\u7684\u914d\u7f6e\uff1a<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-json\">http {\n    # \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684server\u540d\u5b57\u5373upstream_name\u4e0d\u80fd\u5e26\u4e0b\u5212\u7ebf\n    upstream upstream_name {\n        server 192.168.0.28:8001;\n        server 192.168.0.28:8002;\n    }\n\n    server {\n        listen       8080;\n        server_name  localhost;\n\n        location \/ {\n            proxy_pass http:\/\/upstream_name;\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<code>http<\/code>\u4e0b\u6dfb\u52a0<code>upstream upstream_name {}<\/code>\u6765\u914d\u7f6e\u8981\u6620\u5c04\u7684\u670d\u52a1\u5668\uff0c\u5176\u4e2d\u7684<code>upstream_name<\/code>\u53ef\u4ee5\u6307\u5b9a\u4e3a\u670d\u52a1\u7684\u57df\u540d\u6216\u8005\u9879\u76ee\u7684\u4ee3\u53f7\u3002<\/p>\n<p><code>server<\/code>\u4e0b\u7684<code>location<\/code>\u6211\u4eec\u5c06<code>\/<\/code>\u4e0b\u7684\u5168\u90e8\u8bf7\u6c42\u8f6c\u53d1\u5230<code>http:\/\/upstream_name<\/code>\uff0c\u4e5f\u5c31\u662f\u4e0a\u9762\u914d\u7f6e\u7684\u670d\u52a1\u5668\u5217\u8868\u4e2d\u7684\u67d0\u4e00\u53f0\u670d\u52a1\u5668\u4e0a\u3002\u5177\u4f53\u662f\u54ea\u53f0\u670d\u52a1\u5668\uff0cNginx\u4f1a\u6839\u636e\u914d\u7f6e\u7684\u8c03\u5ea6\u7b97\u6cd5\u6765\u786e\u8ba4\u3002<\/p>\n<h2>Nginx\u8d1f\u8f7d\u5747\u8861\u7b56\u7565<\/h2>\n<p>Nginx\u7684\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u67094\u79cd\uff1a<\/p>\n<h3>\u8f6e\u8be2(\u9ed8\u8ba4)<\/h3>\n<p>\u6700\u57fa\u672c\u7684\u914d\u7f6e\u65b9\u6cd5\uff0c\u5b83\u662f<code>upstream<\/code>\u7684\u9ed8\u8ba4\u7b56\u7565\uff0c\u6bcf\u4e2a\u8bf7\u6c42\u4f1a\u6309\u65f6\u95f4\u987a\u5e8f\u9010\u4e00\u5206\u914d\u5230\u4e0d\u540c\u7684\u540e\u7aef\u670d\u52a1\u5668\u3002<\/p>\n<p>\u53c2\u6570\u6709\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u53c2\u6570<\/th>\n<th style=\"text-align: left;\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><code>fail_timeout<\/code><\/td>\n<td style=\"text-align: left;\">\u4e0emax_fails\u7ed3\u5408\u4f7f\u7528<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>max_fails<\/code><\/td>\n<td style=\"text-align: left;\">\u8bbe\u7f6e\u5728fail_timeout\u53c2\u6570\u8bbe\u7f6e\u7684\u65f6\u95f4\u5185\u6700\u5927\u5931\u8d25\u6b21\u6570<br \/>\u5982\u679c\u5728\u8fd9\u4e2a\u65f6\u95f4\u5185\uff0c\u6240\u6709\u9488\u5bf9\u8be5\u670d\u52a1\u5668\u7684\u8bf7\u6c42\u90fd\u5931\u8d25\u4e86\uff0c\u90a3\u4e48\u8ba4\u4e3a\u8be5\u670d\u52a1\u5668\u4f1a\u88ab\u8ba4\u4e3a\u662f\u505c\u673a\u4e86<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>fail_time<\/code><\/td>\n<td style=\"text-align: left;\">\u670d\u52a1\u5668\u4f1a\u88ab\u8ba4\u4e3a\u505c\u673a\u7684\u65f6\u95f4\u957f\u5ea6\uff0c\u9ed8\u8ba4\u4e3a10s<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>backup<\/code><\/td>\n<td style=\"text-align: left;\">\u6807\u8bb0\u8be5\u670d\u52a1\u5668\u4e3a\u5907\u7528\u670d\u52a1\u5668\u3002\u5f53\u4e3b\u670d\u52a1\u5668\u505c\u6b62\u65f6\uff0c\u8bf7\u6c42\u4f1a\u88ab\u53d1\u9001\u5230\u5b83\u8fd9\u91cc<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><code>down<\/code><\/td>\n<td style=\"text-align: left;\">\u6807\u8bb0\u670d\u52a1\u5668\u6c38\u4e45\u505c\u673a\u4e86<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u5728\u8f6e\u8be2\u4e2d\uff0c\u5982\u679c\u670d\u52a1\u5668down\u6389\u4e86\uff0c\u4f1a\u81ea\u52a8\u5254\u9664\u8be5\u670d\u52a1\u5668<\/li>\n<li>\u7f3a\u7701\u914d\u7f6e\u5c31\u662f\u8f6e\u8be2\u7b56\u7565<\/li>\n<li>\u6b64\u7b56\u7565\u9002\u5408\u670d\u52a1\u5668\u914d\u7f6e\u76f8\u5f53\uff0c\u65e0\u72b6\u6001\u4e14\u77ed\u5e73\u5feb\u7684\u670d\u52a1\u4f7f\u7528<\/li>\n<\/ul>\n<h3>\u6743\u91cd<\/h3>\n<p>\u5728\u8f6e\u8be2\u7b56\u7565\u7684\u57fa\u7840\u4e0a\u5236\u5b9a\u6ca6\u9677\u7684\u51e0\u7387\u3002\u4f8b\u5982<\/p>\n<pre><code class=\"language-json\">upstream foo {\n    server localhost:8001 weight=2;\n    server localhost:8002;\n    server localhost:8003 backup;\n    server localhost:8004 max_fails=3 fail_timeout=20s;\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u4f8b\u5b50\u4e2d\uff0cweight\u53c2\u6570\u7528\u4e8e\u5236\u5b9a\u8f6e\u8be2\u7684\u51e0\u7387\uff0cweight\u9ed8\u8ba4\u503c\u4e3a1\uff1bweight\u7684\u6570\u503c\u548c\u88ab\u8bbf\u95ee\u7684\u51e0\u7387\u6210\u6b63\u6bd4\u3002<\/p>\n<p>\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u6743\u91cd\u8d8a\u9ad8\u5206\u914d\u5230\u9700\u8981\u5904\u7406\u7684\u8bf7\u6c42\u8d8a\u591a\u3002<\/li>\n<li>\u6b64\u7b56\u7565\u53ef\u4ee5\u4e0e<code>least_conn<\/code>\u548c<code>ip_hash<\/code>\u7ed3\u5408\u4f7f\u7528\u3002<\/li>\n<li>\u6b64\u7b56\u7565\u6bd4\u8f83\u9002\u5408\u670d\u52a1\u5668\u7684\u786c\u4ef6\u914d\u7f6e\u5dee\u522b\u6bd4\u8f83\u5927\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<h3>ip_hash<\/h3>\n<p>\u8d1f\u8f7d\u5747\u8861\u5668\u6309\u7167\u5ba2\u6237\u7aefIP\u5730\u5740\u7684\u5206\u914d\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u76f8\u540c\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u4e00\u76f4\u53d1\u9001\u5230\u76f8\u540c\u7684\u670d\u52a1\u5668\u3002\u8fd9\u6837\u6bcf\u4e2a\u8bbf\u5ba2\u90fd\u56fa\u5b9a\u8bbf\u95ee\u4e00\u4e2a\u540e\u7aef\u670d\u52a1\u5668\u3002<\/p>\n<pre><code class=\"language-json\">upstream foo {\n    ip_hash;\n    server localhost:8001 weight=2;\n    server localhost:8002;\n    server localhost:8003;\n    server localhost:8004 max_fails=3 fail_timeout=20s;\n}<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u5728Nginx\u7248\u672c1.3.1\u4e4b\u524d\uff0c\u4e0d\u80fd\u5728ip_hash\u4e2d\u4f7f\u7528\u6743\u91cd\uff08weight\uff09<\/li>\n<li>ip_hash\u4e0d\u80fd\u4e0ebackup\u540c\u65f6\u4f7f\u7528<\/li>\n<li>\u6b64\u7b56\u7565\u9002\u5408\u6709\u72b6\u6001\u670d\u52a1\uff0c\u6bd4\u5982session<\/li>\n<li>\u5f53\u6709\u670d\u52a1\u5668\u9700\u8981\u5254\u9664\uff0c\u5fc5\u987b\u624b\u52a8down\u6389<\/li>\n<\/ul>\n<h3>least_conn \u6700\u5c0f\u8fde\u63a5<\/h3>\n<p>\u628a\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u8fde\u63a5\u6570\u8f83\u5c11\u7684\u540e\u7aef\u670d\u52a1\u5668\u3002\u8f6e\u8be2\u7b97\u6cd5\u662f\u628a\u8bf7\u6c42\u5e73\u5747\u7684\u8f6c\u53d1\u7ed9\u5404\u4e2a\u540e\u7aef\uff0c\u4f7f\u5b83\u4eec\u7684\u8d1f\u8f7d\u5927\u81f4\u76f8\u540c\uff1b\u4f46\u662f\uff0c\u6709\u4e9b\u8bf7\u6c42\u5360\u7528\u7684\u65f6\u95f4\u5f88\u957f\uff0c\u4f1a\u5bfc\u81f4\u5176\u6240\u5728\u7684\u540e\u7aef\u8d1f\u8f7d\u8f83\u9ad8\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cleast_conn\u8fd9\u79cd\u65b9\u5f0f\u5c31\u53ef\u4ee5\u8fbe\u5230\u66f4\u597d\u7684\u8d1f\u8f7d\u5747\u8861\u6548\u679c<\/p>\n<pre><code class=\"language-json\">upstream foo {\n    least_conn;\n    server localhost:8001 weight=2;\n    server localhost:8002;\n    server localhost:8003 backup;\n    server localhost:8004 max_fails=3 fail_timeout=20s;\n}<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u6b64\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\u9002\u5408\u8bf7\u6c42\u5904\u7406\u65f6\u95f4\u957f\u77ed\u4e0d\u4e00\u9020\u6210\u670d\u52a1\u5668\u8fc7\u8f7d\u7684\u60c5\u51b5<\/li>\n<\/ul>\n<p>\u9664\u4e86\u4e0a\u9762\u8fd9\u4e9b\u8c03\u5ea6\u7b56\u7565\u4e4b\u540e\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u7684\u8c03\u5ea6\u7b56\u7565\u53ef\u4ee5\u96c6\u6210\u5230Nginx\u4e2d\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u8fd0\u7528\u4e2d\uff0c\u9700\u8981\u6839\u636e\u4e0d\u540c\u7684\u573a\u666f\u9009\u62e9\u4e0d\u540c\u7684\u7b56\u7565\uff0c\u5927\u591a\u662f\u591a\u79cd\u7b56\u7565\u7ed3\u5408\u4f7f\u7528\u4ee5\u8fbe\u5230\u5b9e\u9645\u9700\u6c42\u7684\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nginx\u4e0d\u4ec5\u53ef\u4ee5\u4f5c\u4e3a\u5f3a\u5927\u7684web\u670d\u52a1\u5668\uff0c\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u800c\u4e14nginx\u8fd8\u53ef\u4ee5\u6309\u7167\u8c03\u5ea6\u89c4\u5219\u5b9e\u73b0\u52a8\u9759\u5206 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[332],"tags":[],"class_list":["post-1773","post","type-post","status-publish","format-standard","hentry","category-nginx"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1773"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1773\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}