{"id":22257,"date":"2024-02-05T12:14:23","date_gmt":"2024-02-05T12:14:23","guid":{"rendered":"https:\/\/server.hk\/blog\/22257\/"},"modified":"2024-09-12T06:28:41","modified_gmt":"2024-09-12T06:28:41","slug":"configuring-load-balancers-on-your-usa-vps-nginx-and-haproxy-2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/22257\/","title":{"rendered":"\u5728\u60a8\u7684\u9999\u6e2fVPS\u4e0a\u914d\u7f6e\u8ca0\u8f09\u5747\u8861\u5668\uff1aNginx\u8207HAProxy"},"content":{"rendered":"<h1 id=\"configuring-load-balancers-on-your-usa-vps-nginx-and-haproxy-cYtcnllmrN\">\u5728\u60a8\u7684\u9999\u6e2fVPS\u4e0a\u914d\u7f6e\u8ca0\u8f09\u5747\u8861\u5668\uff1aNginx\u8207HAProxy<\/h1>\n<p>\u8ca0\u8f09\u5747\u8861\u662f\u7ba1\u7406\u9ad8\u6d41\u91cf\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5f0f\u7684\u95dc\u9375\u65b9\u9762\u3002\u5b83\u6709\u52a9\u65bc\u5c07\u9032\u4f86\u7684\u7db2\u7d61\u6d41\u91cf\u5206\u4f48\u5230\u591a\u500b\u670d\u52d9\u5668\u4e0a\uff0c\u78ba\u4fdd\u6700\u4f73\u7684\u6027\u80fd\u3001\u53ef\u64f4\u5c55\u6027\u548c\u53ef\u9760\u6027\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u5982\u4f55\u5728\u60a8\u7684\u9999\u6e2fVPS\u4e0a\u914d\u7f6eNginx\u548cHAProxy\u4f5c\u70ba\u8ca0\u8f09\u5747\u8861\u5668\u3002<\/p>\n<h2 id=\"what-is-a-load-balancer-cYtcnllmrN\">\u4ec0\u9ebc\u662f\u8ca0\u8f09\u5747\u8861\u5668\uff1f<\/h2>\n<p>\u8ca0\u8f09\u5747\u8861\u5668\u4f5c\u70ba\u5ba2\u6236\u7aef\u548c\u670d\u52d9\u5668\u4e4b\u9593\u7684\u4e2d\u4ecb\uff0c\u5c07\u9032\u4f86\u7684\u8acb\u6c42\u5206\u4f48\u5230\u591a\u500b\u5f8c\u7aef\u670d\u52d9\u5668\u4e0a\u3002\u5b83\u5e6b\u52a9\u907f\u514d\u4efb\u4f55\u55ae\u4e00\u670d\u52d9\u5668\u904e\u8f09\uff0c\u6539\u5584\u97ff\u61c9\u6642\u9593\u4e26\u4fdd\u8b49\u9ad8\u53ef\u7528\u6027\u3002<\/p>\n<p>\u8ca0\u8f09\u5747\u8861\u5668\u4e3b\u8981\u6709\u5169\u7a2e\u985e\u578b\uff1a\u8edf\u4ef6\u57fa\u790e\u548c\u786c\u4ef6\u57fa\u790e\u3002\u5982Nginx\u548cHAProxy\u9019\u6a23\u7684\u8edf\u4ef6\u57fa\u790e\u8ca0\u8f09\u5747\u8861\u5668\u66f4\u5177\u9748\u6d3b\u6027\u3001\u6210\u672c\u6548\u76ca\u4e26\u4e14\u66f4\u5bb9\u6613\u914d\u7f6e\u3002<\/p>\n<h2 id=\"configuring-load-balancers-with-nginx-cYtcnllmrN\">\u4f7f\u7528Nginx\u914d\u7f6e\u8ca0\u8f09\u5747\u8861\u5668<\/h2>\n<p>Nginx\u662f\u4e00\u6b3e\u6d41\u884c\u7684\u958b\u6e90\u7db2\u7d61\u4f3a\u670d\u5668\u53ca\u53cd\u5411\u4ee3\u7406\u670d\u52d9\u5668\uff0c\u4e5f\u53ef\u4ee5\u4f5c\u70ba\u8ca0\u8f09\u5747\u8861\u5668\u3002\u4ee5\u4e0b\u662f\u60a8\u5982\u4f55\u5728\u9999\u6e2fVPS\u4e0a\u914d\u7f6eNginx\u4f5c\u70ba\u8ca0\u8f09\u5747\u8861\u5668\u7684\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u901a\u904e\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\u5728\u60a8\u7684VPS\u4e0a\u5b89\u88ddNginx\uff1a<\/li>\n<pre><code>sudo apt-get install nginx<\/code><\/pre>\n<li>\u4f7f\u7528\u6587\u672c\u7de8\u8f2f\u5668\u7de8\u8f2fNginx\u914d\u7f6e\u6587\u4ef6\uff1a<\/li>\n<pre><code>sudo nano \/etc\/nginx\/nginx.conf<\/code><\/pre>\n<li>\u5728<code>http<\/code>\u584a\u5167\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/li>\n<pre><code>http {\r\n    upstream backend {\r\n      server backend1.example.com;\r\n      server backend2.example.com;\r\n      server backend3.example.com;\r\n    }\r\n    \r\n    server {\r\n      listen 80;\r\n      \r\n      location \/ {\r\n        proxy_pass http:\/\/backend;\r\n      }\r\n    }\r\n  }<\/code><\/pre>\n<li>\u4fdd\u5b58\u914d\u7f6e\u6587\u4ef6\u4e26\u9000\u51fa\u6587\u672c\u7de8\u8f2f\u5668\u3002<\/li>\n<li>\u91cd\u65b0\u555f\u52d5Nginx\u4f7f\u66f4\u6539\u751f\u6548\uff1a<\/li>\n<pre><code>sudo service nginx restart<\/code><\/pre>\n<\/ol>\n<p>\u901a\u904e\u6b64\u914d\u7f6e\uff0cNginx\u5c07\u6703\u5c07\u9032\u4f86\u7684\u8acb\u6c42\u5206\u4f48\u5230\u5728<code>upstream<\/code>\u584a\u4e2d\u6307\u5b9a\u7684\u5f8c\u7aef\u670d\u52d9\u5668\u4e0a\u3002<\/p>\n<h2 id=\"configuring-load-balancers-with-haproxy-cYtcnllmrN\">\u4f7f\u7528HAProxy\u914d\u7f6e\u8ca0\u8f09\u5747\u8861\u5668<\/h2>\n<p>HAProxy\u662f\u53e6\u4e00\u6b3e\u6d41\u884c\u7684\u958b\u6e90\u8ca0\u8f09\u5747\u8861\u5668\u548c\u4ee3\u7406\u670d\u52d9\u5668\u3002\u4ee5\u4e0b\u662f\u60a8\u5982\u4f55\u5728\u9999\u6e2fVPS\u4e0a\u914d\u7f6eHAProxy\u7684\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u901a\u904e\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\u5728\u60a8\u7684VPS\u4e0a\u5b89\u88ddHAProxy\uff1a<\/li>\n<pre><code>sudo apt-get install haproxy<\/code><\/pre>\n<li>\u4f7f\u7528\u6587\u672c\u7de8\u8f2f\u5668\u7de8\u8f2fHAProxy\u914d\u7f6e\u6587\u4ef6\uff1a<\/li>\n<pre><code>sudo nano \/etc\/haproxy\/haproxy.cfg<\/code><\/pre>\n<li>\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/li>\n<pre><code>frontend http-in\r\n    bind *:80\r\n    default_backend servers\r\n\r\n  backend servers\r\n    balance roundrobin\r\n    server backend1 backend1.example.com\r\n    server backend2 backend2.example.com\r\n    server backend3 backend3.example.com<\/code><\/pre>\n<li>\u4fdd\u5b58\u914d\u7f6e\u6587\u4ef6\u4e26\u9000\u51fa\u6587\u672c\u7de8\u8f2f\u5668\u3002<\/li>\n<li>\u91cd\u65b0\u555f\u52d5HAProxy\u4f7f\u66f4\u6539\u751f\u6548\uff1a<\/li>\n<pre><code>sudo service haproxy restart<\/code><\/pre>\n<\/ol>\n<p>\u901a\u904e\u6b64\u914d\u7f6e\uff0cHAProxy\u5c07\u4f7f\u7528\u8f2a\u8a62\u7b97\u6cd5\u4f86\u5747\u8861\u9032\u4f86\u8acb\u6c42\uff0c\u5206\u4f48\u5230\u5728<code>backend<\/code>\u584a\u4e2d\u6307\u5b9a\u7684\u5f8c\u7aef\u670d\u52d9\u5668\u4e0a\u3002<\/p>\n<h2 id=\"summary-cYtcnllmrN\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u60a8\u7684\u9999\u6e2fVPS\u4e0a\u914d\u7f6e\u8ca0\u8f09\u5747\u8861\u5668\u5c0d\u65bc\u78ba\u4fdd\u60a8\u7684\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u7684\u6700\u4f73\u6027\u80fd\u548c\u9ad8\u53ef\u7528\u6027\u81f3\u95dc\u91cd\u8981\u3002Nginx\u548cHAProxy\u662f\u5169\u6b3e\u6d41\u884c\u7684\u958b\u6e90\u89e3\u6c7a\u65b9\u6848\uff0c\u5747\u53ef\u8f15\u9b06\u914d\u7f6e\u70ba\u8ca0\u8f09\u5747\u8861\u5668\u3002\u900f\u904e\u5206\u4f48\u9032\u4f86\u7684\u6d41\u91cf\u5230\u591a\u500b\u5f8c\u7aef\u670d\u52d9\u5668\uff0c\u8ca0\u8f09\u5747\u8861\u5668\u5e6b\u52a9\u63d0\u5347\u97ff\u61c9\u6642\u9593\u4e26\u907f\u514d\u670d\u52d9\u5668\u904e\u8f09\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u4e14\u9ad8\u6548\u7684\u9999\u6e2fVPS\u6258\u7ba1\u670d\u52d9\uff0c\u8acb\u8003\u616e<a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u6191\u85c9\u5ee3\u6cdb\u7684\u6258\u7ba1\u8a08\u5283\u548c\u51fa\u8272\u7684\u5ba2\u6236\u652f\u6301\uff0cServer.HK\u662f\u696d\u5167\u7684\u4fe1\u8cf4\u4f9b\u61c9\u5546\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u60a8\u7684\u9999\u6e2fVPS\u4e0a\u914d\u7f6e\u8ca0\u8f09\u5747\u8861\u5668&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4927],"tags":[],"class_list":["post-22257","post","type-post","status-publish","format-standard","hentry","category-4927"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/22257","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=22257"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/22257\/revisions"}],"predecessor-version":[{"id":23102,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/22257\/revisions\/23102"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=22257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=22257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=22257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}