{"id":22279,"date":"2024-02-05T12:36:26","date_gmt":"2024-02-05T12:36:26","guid":{"rendered":"https:\/\/server.hk\/blog\/22279\/"},"modified":"2024-09-11T15:26:28","modified_gmt":"2024-09-11T15:26:28","slug":"configuring-load-balancers-on-your-usa-vps-nginx-and-haproxy-3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/22279\/","title":{"rendered":"\u5728\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-IqdboErOwu\">\u5728\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\u6216\u61c9\u7528\u7a0b\u5f0f\u7684\u95dc\u9375\u65b9\u9762\u3002\u5b83\u6709\u52a9\u65bc\u5c07\u9032\u4f86\u7684\u7db2\u7d61\u6d41\u91cf\u5206\u5e03\u5230\u591a\u500b\u670d\u52d9\u5668\uff0c\u78ba\u4fdd\u6700\u4f73\u8868\u73fe\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\u8ca0\u8f09\u5747\u8861\u5668\u3002<\/p>\n<h2 id=\"what-is-a-load-balancer-IqdboErOwu\">\u4ec0\u9ebc\u662f\u8ca0\u8f09\u5747\u8861\u5668\uff1f<\/h2>\n<p>\u8ca0\u8f09\u5747\u8861\u5668\u4f5c\u70ba\u5ba2\u6236\u7aef\u8207\u670d\u52d9\u5668\u4e4b\u9593\u7684\u4e2d\u4ecb\uff0c\u5206\u6563\u4f86\u81ea\u591a\u500b\u5f8c\u7aef\u670d\u52d9\u5668\u7684\u9032\u4f86\u8acb\u6c42\u3002\u5b83\u6709\u52a9\u65bc\u9632\u6b62\u4efb\u4f55\u55ae\u4e00\u670d\u52d9\u5668\u904e\u8f09\uff0c\u6539\u5584\u97ff\u61c9\u6642\u9593\u4e26\u78ba\u4fdd\u9ad8\u53ef\u7528\u6027\u3002<\/p>\n<p>\u8ca0\u8f09\u5747\u8861\u5668\u6709\u5169\u7a2e\u4e3b\u8981\u985e\u578b\uff1a\u57fa\u65bc\u8edf\u4ef6\u548c\u57fa\u65bc\u786c\u4ef6\u3002\u57fa\u65bc\u8edf\u4ef6\u7684\u8ca0\u8f09\u5747\u8861\u5668\uff0c\u5982Nginx\u548cHAProxy\uff0c\u66f4\u5177\u9748\u6d3b\u6027\u3001\u6210\u672c\u6548\u76ca\u66f4\u9ad8\u4e14\u66f4\u6613\u65bc\u914d\u7f6e\u3002<\/p>\n<h2 id=\"configuring-load-balancers-with-nginx-IqdboErOwu\">\u4f7f\u7528Nginx\u914d\u7f6e\u8ca0\u8f09\u5747\u8861\u5668<\/h2>\n<p>Nginx\u662f\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u7db2\u9801\u4f3a\u670d\u5668\u53ca\u53cd\u5411\u4ee3\u7406\u4f3a\u670d\u5668\uff0c\u4e5f\u53ef\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\uff1a<\/p>\n<ol>\n<li>\u5728\u60a8\u7684VPS\u4e0a\u5b89\u88ddNginx\uff0c\u57f7\u884c\u4ee5\u4e0b\u547d\u4ee4\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>\u5340\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\uff0c\u4f7f\u66f4\u6539\u751f\u6548\uff1a<\/li>\n<pre><code>sudo service nginx restart<\/code><\/pre>\n<\/ol>\n<p>\u7d93\u904e\u9019\u4e9b\u914d\u7f6e\uff0cNginx\u5c07\u6703\u5c07\u63a5\u6536\u5230\u7684\u8acb\u6c42\u5206\u914d\u5230\u5728<code>upstream<\/code>\u5340\u584a\u6307\u5b9a\u7684\u5f8c\u7aef\u670d\u52d9\u5668\u3002<\/p>\n<h2 id=\"configuring-load-balancers-with-haproxy-IqdboErOwu\">\u4f7f\u7528HAProxy\u914d\u7f6e\u8ca0\u8f09\u5747\u8861\u5668<\/h2>\n<p>HAProxy\u662f\u53e6\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90\u8edf\u4ef6\uff0c\u70baTCP\u548c\u57fa\u65bcHTTP\u7684\u61c9\u7528\u7a0b\u5f0f\u63d0\u4f9b\u9ad8\u53ef\u7528\u6027\u548c\u8ca0\u8f09\u5747\u8861\u3002\u4ee5\u4e0b\u662f\u60a8\u5982\u4f55\u5728\u9999\u6e2fVPS\u4e0a\u914d\u7f6eHAProxy\u4f5c\u70ba\u8ca0\u8f09\u5747\u8861\u5668\uff1a<\/p>\n<ol>\n<li>\u5728\u60a8\u7684VPS\u4e0a\u5b89\u88ddHAProxy\uff0c\u57f7\u884c\u4ee5\u4e0b\u547d\u4ee4\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:80 check\r\n    server backend2 backend2.example.com:80 check\r\n    server backend3 backend3.example.com:80 check<\/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\uff0c\u4f7f\u66f4\u6539\u751f\u6548\uff1a<\/li>\n<pre><code>sudo service haproxy restart<\/code><\/pre>\n<\/ol>\n<p>\u7d93\u904e\u9019\u4e9b\u914d\u7f6e\uff0cHAProxy\u5c07\u4f7f\u7528\u5faa\u74b0\u7b97\u6cd5\u5c07\u63a5\u6536\u5230\u7684\u8acb\u6c42\u5747\u8861\u5730\u5206\u914d\u5230\u5728<code>backend<\/code>\u5340\u584a\u6307\u5b9a\u7684\u5f8c\u7aef\u670d\u52d9\u5668\u3002<\/p>\n<h2 id=\"summary-IqdboErOwu\">\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\u6216\u61c9\u7528\u7a0b\u5f0f\u7684\u6700\u4f73\u8868\u73fe\u548c\u9ad8\u53ef\u7528\u6027\u81f3\u95dc\u91cd\u8981\u3002Nginx\u548cHAProxy\u662f\u5169\u7a2e\u6d41\u884c\u7684\u8ca0\u8f09\u5747\u8861\u9078\u9805\uff0c\u63d0\u4f9b\u9748\u6d3b\u6027\u3001\u53ef\u64f4\u5c55\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5728\u5c0b\u627e\u53ef\u9760\u4e14\u9ad8\u6548\u80fd\u7684\u9999\u6e2fVPS\u5bc4\u6258\uff0c\u8acb\u8003\u616e<a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u6191\u85c9\u4ed6\u5011\u7684\u9802\u5c16VPS\u89e3\u6c7a\u65b9\u6848\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u914d\u7f6e\u8ca0\u8f09\u5747\u8861\u5668\u4e26\u512a\u5316\u60a8\u7684\u7db2\u7ad9\u6216\u61c9\u7528\u7a0b\u5f0f\u7684\u8868\u73fe\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u9999\u6e2fVPS\u4e0a\u914d\u7f6e\u8ca0\u8f09\u5747\u8861\u5668\uff1aN&#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-22279","post","type-post","status-publish","format-standard","hentry","category-4927"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/22279","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=22279"}],"version-history":[{"count":2,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/22279\/revisions"}],"predecessor-version":[{"id":23065,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/22279\/revisions\/23065"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=22279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=22279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=22279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}