{"id":115576,"date":"2026-10-12T17:17:12","date_gmt":"2026-10-12T09:17:12","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/115576.html"},"modified":"2026-10-12T17:17:12","modified_gmt":"2026-10-12T09:17:12","slug":"linux%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e5%ae%9e%e6%88%98%ef%bd%9cnginxhaproxy%e5%9b%9b%e5%b1%82%e4%b8%83%e5%b1%82%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e5%ae%8c%e6%95%b4%e5%ae%9e%e6%93%8d","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/115576.html","title":{"rendered":"Linux\u8d1f\u8f7d\u5747\u8861\u5b9e\u6218\uff5cNginx+HAProxy\u56db\u5c42\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u6574\u5b9e\u64cd"},"content":{"rendered":"<h2>Linux\u8d1f\u8f7d\u5747\u8861\u5b9e\u6218&#xff5c;Nginx&#043;HAProxy\u56db\u5c42\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u6574\u5b9e\u64cd<\/h2>\n<h3>\u5f15\u8a00<\/h3>\n<p>\u672c\u7bc7\u9002\u5408CentOS7\u8fd0\u7ef4\u5165\u95e8\u540c\u5b66&#xff0c;\u8bfb\u5b8c\u672c\u6587\u4f60\u53ef\u4ee5\u5b66\u4f1a&#xff1a;<\/p>\n<li>\u7406\u89e3\u8d1f\u8f7d\u5747\u8861\u6838\u5fc3\u601d\u60f3&#xff0c;\u5206\u6e05\u53cd\u5411\u4ee3\u7406\u4e0e\u8d1f\u8f7d\u5747\u8861\u7684\u5173\u7cfb&#xff0c;\u8ba4\u8bc6Nginx\u591a\u4e2a\u4e3b\u6d41\u7248\u672c\u5dee\u5f02&#xff1b;<\/li>\n<li>Nginx\u7684upstream\u6a21\u5757\u5b9e\u64cd&#xff0c;\u638c\u63e1\u8f6e\u8be2\u3001\u6743\u91cd\u3001ip_hash\u3001least_conn\u7b49\u8c03\u5ea6\u7b97\u6cd5&#xff0c;\u540e\u7aef\u8282\u70b9backup\u3001down\u3001\u5065\u5eb7\u68c0\u67e5\u53c2\u6570\u7528\u6cd5&#xff1b;<\/li>\n<li>HAProxy\u57fa\u7840\u539f\u7406&#xff0c;\u5206\u6e05\u56db\u5c42TCP\u4ee3\u7406\u4e0e\u4e03\u5c42HTTP\u4ee3\u7406\u7684\u533a\u522b&#xff0c;\u7406\u89e3X\u2011Forwarded\u2011For\u83b7\u53d6\u771f\u5b9e\u5ba2\u6237\u7aefIP&#xff1b;<\/li>\n<li>HAProxy\u914d\u7f6efrontend\/backend\/listen\u4e09\u79cd\u914d\u7f6e\u5199\u6cd5&#xff0c;ACL\u8bbf\u95ee\u89c4\u5219\u3001Web\u72b6\u6001\u76d1\u63a7\u9762\u677f&#xff1b;<\/li>\n<li>\u5b8c\u6574\u591a\u673a\u5b9e\u9a8c\u590d\u73b0&#xff0c;\u4ece\u540e\u7aefWeb\u670d\u52a1\u90e8\u7f72\u3001\u8d1f\u8f7d\u5747\u8861\u914d\u7f6e\u3001\u5ba2\u6237\u7aef\u9a8c\u8bc1\u6574\u5957\u6d41\u7a0b\u3002<\/li>\n<p>\u5168\u90e8\u6765\u81ea\u8bfe\u5802\u591a\u673a\u73af\u5883\u5b9e\u64cd&#xff0c;\u9700\u8981\u591a\u53f0CentOS\u865a\u62df\u673a\u642d\u5efa\u96c6\u7fa4\u73af\u5883&#xff0c;\u9002\u5408\u5b66\u4e60Web\u96c6\u7fa4\u5165\u95e8\u3002<\/p>\n<h3>\u524d\u7f6e\u8bf4\u660e<\/h3>\n<p>\u2705 \u73af\u5883&#xff1a;\u591a\u53f0CentOS7\u865a\u62df\u673a&#xff1b;\u5efa\u8bae\u51c6\u5907&#xff1a;1\u53f0\u8d1f\u8f7d\u5747\u8861\u5668&#xff0c;3\u53f0\u540e\u7aefWeb\u8282\u70b9&#xff0c;1\u53f0\u5ba2\u6237\u7aef&#xff0c;\u53ef\u9009\u8def\u7531\u5668\u4e3b\u673a&#xff1b;\u5173\u95edSELinux\u3001\u9632\u706b\u5899&#xff1b; \u2705 \u524d\u7f6e\u77e5\u8bc6&#xff1a;\u719f\u6089Nginx\u865a\u62df\u4e3b\u673a\u3001\u7f51\u7edcIP\/\u7f51\u5173\u57fa\u7840&#xff0c;\u4e86\u89e3HTTP\u57fa\u7840&#xff1b; \u2705 \u65b0\u624b\u53cb\u597d&#xff1a;\u6982\u5ff5\u642d\u914d\u901a\u4fd7\u6bd4\u55bb&#xff0c;\u914d\u7f6e\u5e26\u6ce8\u91ca&#xff0c;\u8ddf\u7740\u6b65\u9aa4\u53ef\u4ee5\u590d\u73b0\u5b9e\u9a8c\u3002<\/p>\n<p>&#x1f4a1;\u4ee3\u7801\u5757#\u540e\u9762\u4e3a\u6ce8\u91ca&#xff0c;\u4e0d\u8981\u590d\u5236\u5230\u7ec8\u7aef\u6267\u884c\u3002 \u26a0\ufe0f\u5b9e\u9a8c\u73af\u5883\u5173\u95ed\u9632\u706b\u5899\u4e0eSELinux&#xff1b;\u751f\u4ea7\u73af\u5883\u9700\u8981\u5408\u7406\u914d\u7f6e\u9632\u706b\u5899\u653e\u884c\u7aef\u53e3\u3002<\/p>\n<hr \/>\n<h3>\u4e00\u3001\u8d1f\u8f7d\u5747\u8861\u57fa\u7840\u6982\u5ff5<\/h3>\n<p>\u6bd4\u55bb&#xff1a;\u8d1f\u8f7d\u5747\u8861\u5c31\u50cf\u516c\u53f8\u524d\u53f0\u63a5\u5f85&#xff0c;\u5927\u91cf\u7528\u6237\u8bf7\u6c42\u8fc7\u6765&#xff0c;\u524d\u53f0\u628a\u8bf7\u6c42\u5206\u53d1\u5230\u540e\u9762\u591a\u4e2a\u4e1a\u52a1\u5458\u5de5\u5904\u7406&#xff0c;\u907f\u514d\u5355\u4e2a\u4eba\u5e72\u6d3b\u7d2f\u6b7b&#xff0c;\u63d0\u5347\u6574\u4f53\u5904\u7406\u80fd\u529b&#xff0c;\u540c\u65f6\u4e00\u53f0\u5458\u5de5\u6302\u6389&#xff0c;\u5176\u4ed6\u5458\u5de5\u53ef\u4ee5\u7ee7\u7eed\u5e72\u6d3b&#xff0c;\u5b9e\u73b0\u9ad8\u53ef\u7528\u3002<\/p>\n<p>\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u5904\u5728\u771f\u5b9eWeb\u670d\u52a1\u5668\u7684\u524d\u9762&#xff0c;\u7528\u6237\u53ea\u8bbf\u95ee\u4ee3\u7406&#xff0c;\u4ee3\u7406\u628a\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u540e\u7aef\u591a\u53f0Web\u673a\u5668&#xff0c;\u8fd9\u5c31\u662f\u8d1f\u8f7d\u5747\u8861\u3002<\/p>\n<ul>\n<li>\u7528\u6237\u770b\u4e0d\u5230\u540e\u7aef\u771f\u5b9e\u670d\u52a1\u5668IP&#xff1b;<\/li>\n<li>\u4ee3\u7406\u63a5\u6536\u7528\u6237\u8bf7\u6c42&#xff0c;\u8f6c\u53d1\u540e\u7aef&#xff0c;\u62ff\u5230\u7ed3\u679c\u518d\u8fd4\u56de\u7ed9\u7528\u6237&#xff1b;<\/li>\n<li>\u540e\u7aefWeb\u670d\u52a1\u5668\u4e0d\u9700\u8981\u516c\u7f51IP&#xff0c;\u5168\u90e8\u6d41\u91cf\u7ecf\u8fc7\u8d1f\u8f7d\u5747\u8861\u4ee3\u7406\u3002<\/li>\n<\/ul>\n<h4>Nginx\u5e38\u89c1\u7248\u672c\u5bf9\u6bd4<\/h4>\n<table>\n<tr>\u7248\u672c\u7279\u70b9\u9002\u7528\u573a\u666f<\/tr>\n<tbody>\n<tr>\n<td>\u5b98\u65b9Nginx<\/td>\n<td>\u5f00\u6e90\u514d\u8d39&#xff0c;\u57fa\u7840\u8d1f\u8f7d\u5747\u8861\u3001\u53cd\u5411\u4ee3\u7406<\/td>\n<td>\u7edd\u5927\u591a\u6570\u4e2d\u5c0f\u4f01\u4e1a&#xff0c;\u5b66\u4e60\u5b9e\u9a8c\u9996\u9009<\/td>\n<\/tr>\n<tr>\n<td>Nginx Plus<\/td>\n<td>\u5546\u4e1a\u4ed8\u8d39&#xff0c;\u9ad8\u7ea7\u5065\u5eb7\u68c0\u67e5\u3001\u4f1a\u8bdd\u7c98\u6027\u3001\u52a8\u6001\u914d\u7f6e<\/td>\n<td>\u4f01\u4e1a\u5546\u4e1a\u751f\u4ea7\u73af\u5883<\/td>\n<\/tr>\n<tr>\n<td>OpenResty<\/td>\n<td>Nginx &#043; LuaJIT&#xff0c;\u652f\u6301Lua\u4e8c\u6b21\u5f00\u53d1<\/td>\n<td>\u9700\u8981\u9ad8\u5ea6\u81ea\u5b9a\u4e49\u4e1a\u52a1\u903b\u8f91<\/td>\n<\/tr>\n<tr>\n<td>Tengine<\/td>\n<td>\u963f\u91cc\u7ef4\u62a4\u7684Nginx\u5206\u652f&#xff0c;\u6027\u80fd\u4f18\u5316\u3001\u52a8\u6001\u6a21\u5757<\/td>\n<td>\u56fd\u5185\u5927\u578b\u4e92\u8054\u7f51\u4e1a\u52a1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4e8c\u3001Nginx\u8d1f\u8f7d\u5747\u8861\u5b9e\u64cd&#xff08;upstream\u6a21\u5757&#xff09;<\/h3>\n<h4>\u5b9e\u9a8c\u62d3\u6251<\/h4>\n<table>\n<tr>\u4e3b\u673a\u540dIP\u89d2\u8272<\/tr>\n<tbody>\n<tr>\n<td>nginx.user.cloud<\/td>\n<td>10.1.8.10<\/td>\n<td>Nginx\u8d1f\u8f7d\u5747\u8861\u5668<\/td>\n<\/tr>\n<tr>\n<td>web1.user.cloud<\/td>\n<td>10.1.8.11<\/td>\n<td>\u540e\u7aefWeb\u8282\u70b91<\/td>\n<\/tr>\n<tr>\n<td>web2.user.cloud<\/td>\n<td>10.1.8.12<\/td>\n<td>\u540e\u7aefWeb\u8282\u70b92<\/td>\n<\/tr>\n<tr>\n<td>web3.user.cloud<\/td>\n<td>10.1.8.13<\/td>\n<td>\u540e\u7aefWeb\u8282\u70b93<\/td>\n<\/tr>\n<tr>\n<td>client1.user.cloud<\/td>\n<td>10.1.8.21<\/td>\n<td>\u6d4b\u8bd5\u5ba2\u6237\u7aef<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b9e\u9a8c\u524d\u63d0&#xff1a;\u5168\u90e8\u673a\u5668\u5173\u95edselinux\u3001\u5173\u95edfirewalld&#xff1b;\u7f51\u7edc\u4e92\u901a\u3002<\/p>\n<h4>2.1 \u90e8\u7f72\u540e\u7aefWeb\u8282\u70b9&#xff08;web1\u2011web3\u4e09\u53f0\u673a\u5668&#xff09;<\/h4>\n<p>\u4e09\u53f0web\u670d\u52a1\u5668\u6267\u884c\u76f8\u540c\u64cd\u4f5c&#xff0c;\u9875\u9762\u8f93\u51fa\u81ea\u5df1\u4e3b\u673a\u540d&#xff0c;\u65b9\u4fbf\u89c2\u5bdf\u8d1f\u8f7d\u5206\u53d1\u6548\u679c\u3002<\/p>\n<p>yum <span class=\"token function\">install<\/span> <span class=\"token parameter variable\">-y<\/span> nginx<br \/>\n<span class=\"token comment\"># \u9875\u9762\u6807\u8bb0\u672c\u673a\u4e3b\u673a\u540d&#xff0c;\u7528\u6765\u89c2\u5bdf\u8bf7\u6c42\u843d\u5230\u54ea\u53f0\u673a\u5668<\/span><br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;Welcome to <span class=\"token variable\"><span class=\"token variable\">$(<\/span><span class=\"token function\">hostname<\/span><span class=\"token variable\">)<\/span><\/span>&#034;<\/span> <span class=\"token operator\">&gt;<\/span> \/usr\/share\/nginx\/html\/index.html<br \/>\nsystemctl <span class=\"token builtin class-name\">enable<\/span> nginx <span class=\"token parameter variable\">&#8211;now<\/span><\/p>\n<p>\u5ba2\u6237\u7aef\u6d4b\u8bd5\u8bbf\u95ee\u540e\u7aef\u5355\u673a\u662f\u5426\u6b63\u5e38&#xff1a;<\/p>\n<p><span class=\"token function\">curl<\/span> <span class=\"token number\">10.1<\/span>.8.11<br \/>\n<span class=\"token function\">curl<\/span> <span class=\"token number\">10.1<\/span>.8.12<br \/>\n<span class=\"token function\">curl<\/span> <span class=\"token number\">10.1<\/span>.8.13<\/p>\n<h4>2.2 Nginx\u8d1f\u8f7d\u5747\u8861\u5668\u914d\u7f6e<\/h4>\n<p>\u5b89\u88c5nginx&#xff0c;\u65b0\u5efa\u4ee3\u7406\u914d\u7f6e&#xff1a;<\/p>\n<p>yum <span class=\"token function\">install<\/span> <span class=\"token parameter variable\">-y<\/span> nginx<br \/>\n<span class=\"token comment\"># \u521b\u5efa\u8d1f\u8f7d\u5747\u8861\u914d\u7f6e\u6587\u4ef6<\/span><br \/>\n<span class=\"token function\">vim<\/span> \/etc\/nginx\/conf.d\/proxy.conf<\/p>\n<p>\u5199\u5165\u914d\u7f6e&#xff1a;<\/p>\n<p><span class=\"token comment\"># upstream&#xff1a;\u5b9a\u4e49\u540e\u7aef\u670d\u52a1\u5668\u7ec4&#xff0c;\u81ea\u5b9a\u4e49\u7ec4\u540d\u53ebweb<\/span><br \/>\nupstream web <span class=\"token punctuation\">{<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.11:80<span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.12:80<span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.13:80<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>server <span class=\"token punctuation\">{<\/span><br \/>\n    listen <span class=\"token number\">80<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    server_name www.user.cloud<span class=\"token punctuation\">;<\/span><br \/>\n    location \/ <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\"># \u628a\u8bf7\u6c42\u8f6c\u53d1\u5230\u4e0a\u9762\u5b9a\u4e49\u7684web\u540e\u7aef\u7ec4<\/span><br \/>\n        proxy_pass http:\/\/web<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u6821\u9a8c\u914d\u7f6e\u3001\u542f\u52a8nginx<\/p>\n<p>nginx <span class=\"token parameter variable\">-t<\/span><br \/>\nsystemctl <span class=\"token builtin class-name\">enable<\/span> nginx <span class=\"token parameter variable\">&#8211;now<\/span><\/p>\n<p>\u5ba2\u6237\u7aef\u914d\u7f6ehosts\u89e3\u6790\u57df\u540d\u505a\u6d4b\u8bd5<\/p>\n<p><span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;10.1.8.10 www.user.cloud&#034;<\/span> <span class=\"token operator\">&gt;&gt;<\/span> \/etc\/hosts<br \/>\n<span class=\"token comment\"># \u5faa\u73af90\u6b21\u8bf7\u6c42&#xff0c;\u7edf\u8ba1\u6bcf\u53f0\u540e\u7aef\u88ab\u8bbf\u95ee\u6b21\u6570<\/span><br \/>\n<span class=\"token keyword\">for<\/span> <span class=\"token for-or-select variable\">i<\/span> <span class=\"token keyword\">in<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">..<\/span><span class=\"token number\">90<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><span class=\"token keyword\">do<\/span> <span class=\"token function\">curl<\/span> <span class=\"token parameter variable\">-s<\/span> www.user.cloud<span class=\"token punctuation\">;<\/span><span class=\"token keyword\">done<\/span><span class=\"token operator\">|<\/span><span class=\"token function\">sort<\/span><span class=\"token operator\">|<\/span><span class=\"token function\">uniq<\/span> <span class=\"token parameter variable\">-c<\/span><\/p>\n<p>\u9ed8\u8ba4\u8f6e\u8be2\u7b97\u6cd5&#xff0c;\u4e09\u53f0\u673a\u5668\u5404\u5206\u914d30\u6b21\u8bf7\u6c42\u3002<\/p>\n<h4>2.3 upstream\u5e38\u7528\u53c2\u6570\u8be6\u89e3<\/h4>\n<p>upstream web <span class=\"token punctuation\">{<\/span><br \/>\n    keepalive <span class=\"token number\">32<\/span><span class=\"token punctuation\">;<\/span>                      <span class=\"token comment\"># worker\u8fdb\u7a0b\u4fdd\u6301\u540e\u7aef\u8fde\u63a5\u7f13\u5b58\u6570\u91cf<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.11:80 <span class=\"token assign-left variable\">weight<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">2<\/span> <span class=\"token assign-left variable\">max_fails<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">3<\/span> <span class=\"token assign-left variable\">fail_timeout<\/span><span class=\"token operator\">&#061;<\/span>30s<span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.12:80 backup<span class=\"token punctuation\">;<\/span>        <span class=\"token comment\"># backup&#xff1a;\u5907\u7528\u673a\u5668&#xff0c;\u5168\u90e8\u4e3b\u670d\u52a1\u5668\u6545\u969c\u624d\u542f\u7528<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.13:80 down<span class=\"token punctuation\">;<\/span>          <span class=\"token comment\"># down&#xff1a;\u624b\u52a8\u4e0b\u7ebf&#xff0c;\u4e0d\u63a5\u6536\u4e1a\u52a1\u8bf7\u6c42<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<ul>\n<li>weight&#xff1a;\u6743\u91cd&#xff0c;\u6570\u503c\u8d8a\u5927\u5206\u914d\u8bf7\u6c42\u8d8a\u591a&#xff1b;<\/li>\n<li>max_fails&#xff1a;\u6700\u5927\u5931\u8d25\u5c1d\u8bd5\u6b21\u6570&#xff1b;<\/li>\n<li>fail_timeout&#xff1a;\u5931\u8d25\u8d85\u65f6\u65f6\u95f4&#xff0c;\u8fbe\u5230\u5931\u8d25\u6b21\u6570\u4e34\u65f6\u9694\u79bb\u6545\u969c\u8282\u70b9&#xff1b;<\/li>\n<li>backup&#xff1a;\u5907\u7528\u8282\u70b9&#xff0c;\u4e3b\u8282\u70b9\u5168\u90e8\u6302\u6389\u624d\u63a5\u7ba1\u6d41\u91cf&#xff1b;<\/li>\n<li>down&#xff1a;\u624b\u52a8\u6807\u8bb0\u4e0b\u7ebf\u7ef4\u62a4\u3002<\/li>\n<\/ul>\n<h4>2.4 Nginx\u4e3b\u6d41\u8c03\u5ea6\u7b97\u6cd5<\/h4>\n<h5>1&#xff09;\u8f6e\u8be2 round\u2011robin&#xff08;Nginx\u9ed8\u8ba4&#xff09;<\/h5>\n<p>\u8bf7\u6c42\u6309\u987a\u5e8f\u8f6e\u6d41\u5206\u7ed9\u6bcf\u4e00\u53f0\u540e\u7aef\u670d\u52a1\u5668&#xff0c;\u8282\u70b9\u6545\u969c\u81ea\u52a8\u5254\u9664\u3002<\/p>\n<p>upstream web <span class=\"token punctuation\">{<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.11:80<span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.12:80<span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.13:80<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u52a0\u6743\u8f6e\u8be2&#xff0c;\u786c\u4ef6\u6027\u80fd\u4e0d\u4e00\u6837\u7684\u65f6\u5019\u7528&#xff0c;\u6743\u91cd\u8d8a\u9ad8\u5206\u5230\u8bf7\u6c42\u8d8a\u591a<\/p>\n<p>upstream web <span class=\"token punctuation\">{<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.11:80 <span class=\"token assign-left variable\">weight<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.12:80 <span class=\"token assign-left variable\">weight<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">20<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.13:80 <span class=\"token assign-left variable\">weight<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token number\">30<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h5>2&#xff09;ip_hash \u4f1a\u8bdd\u4fdd\u6301<\/h5>\n<p>\u6839\u636e\u5ba2\u6237\u7aef\u6e90IP\u505ahash&#xff0c;\u540c\u4e00\u4e2a\u5ba2\u6237\u7aefIP\u6c38\u8fdc\u843d\u5230\u540c\u4e00\u53f0\u540e\u7aef\u670d\u52a1\u5668&#xff0c;\u89e3\u51b3session\u4f1a\u8bdd\u4e22\u5931\u95ee\u9898\u3002<\/p>\n<p>\u7f3a\u70b9&#xff1a;\u5185\u7f51NAT\u73af\u5883\u5927\u91cf\u7528\u6237\u5171\u7528\u540c\u4e00\u4e2a\u516c\u7f51IP&#xff0c;\u4f1a\u9020\u6210\u6d41\u91cf\u96c6\u4e2d\u5230\u4e00\u53f0\u673a\u5668&#xff0c;\u8d1f\u8f7d\u4e0d\u5747\u8861\u3002<\/p>\n<p>upstream web <span class=\"token punctuation\">{<\/span><br \/>\n    ip_hash<span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.11:80<span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.12:80<span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.13:80<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h5>3&#xff09;least_conn \u6700\u5c11\u8fde\u63a5<\/h5>\n<p>\u8bf7\u6c42\u5206\u914d\u7ed9\u5f53\u524d\u6d3b\u8dc3\u8fde\u63a5\u6570\u6700\u5c11\u7684\u540e\u7aef\u8282\u70b9&#xff0c;\u9002\u5408\u8bf7\u6c42\u5904\u7406\u65f6\u95f4\u5dee\u5f02\u5927\u7684\u4e1a\u52a1\u3002<\/p>\n<p>upstream web <span class=\"token punctuation\">{<\/span><br \/>\n    least_conn<span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.11:80<span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.12:80<span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.13:80<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h5>4&#xff09;hash \u901a\u7528\u54c8\u5e0c&#xff08;url_hash&#xff09;<\/h5>\n<p>\u6839\u636eURL\u3001\u81ea\u5b9a\u4e49\u53d8\u91cf\u54c8\u5e0c\u5206\u53d1&#xff0c;\u9002\u5408\u7f13\u5b58\u670d\u52a1\u5668\u573a\u666f&#xff1b;\u5b98\u65b9nginx\u9700\u8981\u989d\u5916\u6a21\u5757\u652f\u6301\u3002<\/p>\n<p>upstream web <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token builtin class-name\">hash<\/span> <span class=\"token variable\">$request_uri<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.11:80<span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.12:80<span class=\"token punctuation\">;<\/span><br \/>\n    server <span class=\"token number\">10.1<\/span>.8.13:80<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>least_time\u6700\u5c11\u54cd\u5e94\u65f6\u95f4\u7b97\u6cd5&#xff0c;\u53ea\u6709Nginx Plus\u5546\u4e1a\u7248\u63d0\u4f9b&#xff0c;\u5f00\u6e90\u7248\u672c\u4e0d\u652f\u6301\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001HAProxy\u8d1f\u8f7d\u5747\u8861<\/h3>\n<p>HAProxy\u662f\u4e00\u6b3e\u9ad8\u6027\u80fd\u4ee3\u7406\u8f6f\u4ef6&#xff0c;\u540c\u65f6\u652f\u6301\u56db\u5c42TCP\u8d1f\u8f7d\u5747\u8861\u548c\u4e03\u5c42HTTP\u8d1f\u8f7d\u5747\u8861&#xff0c;\u5f00\u6e90\u3001\u9ad8\u5e76\u53d1&#xff0c;\u5927\u91cf\u4e92\u8054\u7f51\u9879\u76ee\u4f7f\u7528\u3002<\/p>\n<h4>3.1 \u56db\u5c42\u4ee3\u7406 vs \u4e03\u5c42\u4ee3\u7406<\/h4>\n<ul>\n<li>\u4e03\u5c42&#xff08;HTTP&#xff09;&#xff1a;\u89e3\u6790HTTP\u534f\u8bae&#xff0c;\u53ef\u4ee5\u8bfb\u53d6header\u3001cookie\u3001url&#xff1b;\u53ef\u4ee5\u6309\u57df\u540d\u3001URL\u8def\u5f84\u505a\u590d\u6742\u5206\u53d1&#xff1b;\u540e\u7aef\u770b\u5230\u7684\u6e90IP\u662fHAProxy&#xff0c;\u9700\u8981X\u2011Forwarded\u2011For\u62ff\u5230\u771f\u5b9e\u5ba2\u6237\u7aefIP\u3002<\/li>\n<li>\u56db\u5c42&#xff08;TCP&#xff09;&#xff1a;\u4e0d\u89e3\u6790\u5e94\u7528\u5c42\u5185\u5bb9&#xff0c;\u53ea\u8f6c\u53d1TCP\u6570\u636e\u5305&#xff0c;\u53ea\u8bc6\u522bIP\u548c\u7aef\u53e3&#xff1b;\u8f6c\u53d1\u6548\u7387\u6781\u9ad8&#xff0c;\u652f\u6301MySQL\u3001SSH\u7b49\u975eHTTP\u4e1a\u52a1\u3002<\/li>\n<\/ul>\n<p>\u4e24\u6761TCP\u94fe\u8def&#xff1a;\u5ba2\u6237\u7aef \u2194 HAProxy&#xff1b;HAProxy \u2194 \u540e\u7aef\u670d\u52a1\u5668&#xff0c;\u4e24\u6761\u8fde\u63a5\u4e92\u76f8\u72ec\u7acb\u3002\u540e\u7aef\u670d\u52a1\u5668\u4e0d\u80fd\u88ab\u7528\u6237\u76f4\u63a5\u8bbf\u95ee&#xff0c;\u5168\u90e8\u6d41\u91cf\u7ecf\u8fc7\u4ee3\u7406\u3002<\/p>\n<h4>3.2 HAProxy\u5e38\u7528\u8c03\u5ea6\u7b97\u6cd5<\/h4>\n<table>\n<tr>\u7b97\u6cd5\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>roundrobin<\/td>\n<td>\u52a0\u6743\u8f6e\u8be2&#xff0c;\u52a8\u6001&#xff0c;\u652f\u6301\u6743\u91cd\u8c03\u6574<\/td>\n<\/tr>\n<tr>\n<td>static\u2011rr<\/td>\n<td>\u9759\u6001\u8f6e\u8be2&#xff0c;\u6743\u91cd\u8fd0\u884c\u4e2d\u4e0d\u80fd\u52a8\u6001\u4fee\u6539<\/td>\n<\/tr>\n<tr>\n<td>leastconn<\/td>\n<td>\u6700\u5c11\u8fde\u63a5\u4f18\u5148&#xff0c;\u957f\u8fde\u63a5\u4e1a\u52a1\u9996\u9009<\/td>\n<\/tr>\n<tr>\n<td>source<\/td>\n<td>\u6e90IP\u54c8\u5e0c&#xff0c;\u4f1a\u8bdd\u4fdd\u6301&#xff0c;\u7c7b\u4f3cnginx ip_hash<\/td>\n<\/tr>\n<tr>\n<td>uri<\/td>\n<td>\u6839\u636e\u8bf7\u6c42URI\u54c8\u5e0c&#xff0c;\u7f13\u5b58\u573a\u666f<\/td>\n<\/tr>\n<tr>\n<td>hdr(name)<\/td>\n<td>\u6839\u636eHTTP\u8bf7\u6c42\u5934\u54c8\u5e0c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>3.3 HAProxy\u4e03\u5c42HTTP\u4ee3\u7406\u5b9e\u64cd<\/h4>\n<p>\u5b9e\u9a8c\u73af\u5883\u6cbf\u7528\u4e0a\u9762\u4e09\u53f0web\u540e\u7aef&#xff1a;10.1.8.11\u201113\u3002 haproxy\u673a\u5668\u5b89\u88c5\u8f6f\u4ef6&#xff1a;<\/p>\n<p>yum <span class=\"token function\">install<\/span> <span class=\"token parameter variable\">-y<\/span> haproxy<br \/>\n<span class=\"token function\">cp<\/span> \/etc\/haproxy\/haproxy.cfg<span class=\"token punctuation\">{<\/span>,.ori<span class=\"token punctuation\">}<\/span> <span class=\"token comment\">#\u5907\u4efd\u539f\u59cb\u914d\u7f6e<\/span><br \/>\n<span class=\"token function\">vim<\/span> \/etc\/haproxy\/haproxy.cfg<\/p>\n<p>\u5728\u914d\u7f6e\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u4e03\u5c42HTTP\u914d\u7f6e&#xff0c;\u4f7f\u7528frontend\u2011backend\u5199\u6cd5&#xff0c;\u642d\u914dACL\u89c4\u5219\u5b9e\u73b0\u6309\u540e\u7f00\u5206\u6d41&#xff1a;<\/p>\n<p><span class=\"token comment\"># frontend&#xff1a;\u63a5\u6536\u7528\u6237\u8bf7\u6c42\u524d\u7aef\u5165\u53e3<\/span><br \/>\nfrontend front_web<br \/>\n    <span class=\"token builtin class-name\">bind<\/span> *:80<br \/>\n    default_backend back_web<br \/>\n    <span class=\"token comment\"># acl \u81ea\u5b9a\u4e49\u5339\u914d\u89c4\u5219&#xff0c;\u5339\u914d\u8bbf\u95ee\u8def\u5f84\u4ee5.txt\u7ed3\u5c3e<\/span><br \/>\n    acl is_txt url_reg <span class=\"token parameter variable\">-i<\/span> <span class=\"token punctuation\">\\\\<\/span>.txt$<br \/>\n    <span class=\"token comment\"># \u5982\u679c\u5339\u914dis_txt\u89c4\u5219&#xff0c;\u5c31\u4ea4\u7ed9back_test\u540e\u7aef\u7ec4<\/span><br \/>\n    use_backend back_test <span class=\"token keyword\">if<\/span> is_txt<\/p>\n<p><span class=\"token comment\"># \u9ed8\u8ba4web\u540e\u7aef\u7ec4<\/span><br \/>\nbackend back_web<br \/>\n    balance roundrobin<br \/>\n    server web1 <span class=\"token number\">10.1<\/span>.8.11:80 check<br \/>\n    server web2 <span class=\"token number\">10.1<\/span>.8.12:80 check<br \/>\n    server web3 <span class=\"token number\">10.1<\/span>.8.13:80 check<\/p>\n<p><span class=\"token comment\"># txt\u8bf7\u6c42\u4e13\u7528\u540e\u7aef&#xff0c;\u540e\u7aefweb\u5f00\u542f81\u7aef\u53e3<\/span><br \/>\nbackend back_test<br \/>\n    balance roundrobin<br \/>\n    server test1 <span class=\"token number\">10.1<\/span>.8.11:81 check<br \/>\n    server test2 <span class=\"token number\">10.1<\/span>.8.12:81 check<br \/>\n    server test3 <span class=\"token number\">10.1<\/span>.8.13:81 check<\/p>\n<p>\u53c2\u6570\u8bf4\u660e&#xff1a;check\u5f00\u542f\u540e\u7aef\u5065\u5eb7\u68c0\u67e5&#xff0c;\u8282\u70b9\u6545\u969c\u81ea\u52a8\u5254\u9664\u3002<\/p>\n<p>\u540e\u7aefweb\u673a\u5668\u989d\u5916\u5f00\u542f81\u7aef\u53e3\u670d\u52a1&#xff1a;<\/p>\n<p><span class=\"token comment\"># web1\u2011web3\u5168\u90e8\u6267\u884c<\/span><br \/>\n<span class=\"token function\">mkdir<\/span> \/test<br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;hello html from <span class=\"token variable\"><span class=\"token variable\">$(<\/span><span class=\"token function\">hostname<\/span><span class=\"token variable\">)<\/span><\/span>&#034;<\/span> <span class=\"token operator\">&gt;<\/span> \/test\/index.html<br \/>\n<span class=\"token builtin class-name\">echo<\/span> <span class=\"token string\">&#034;hello txt from <span class=\"token variable\"><span class=\"token variable\">$(<\/span><span class=\"token function\">hostname<\/span><span class=\"token variable\">)<\/span><\/span>&#034;<\/span> <span class=\"token operator\">&gt;<\/span> \/test\/index.txt<br \/>\n<span class=\"token function\">cat<\/span> <span class=\"token operator\">&gt;<\/span> \/etc\/nginx\/conf.d\/vhost-test.conf <span class=\"token operator\">&lt;&lt;<\/span><span class=\"token string\">&#039;EOF&#039;<br \/>\nserver {<br \/>\n    listen 81;<br \/>\n    root \/test;<br \/>\n}<br \/>\nEOF<\/span><br \/>\nsystemctl restart nginx<\/p>\n<p>\u542f\u52a8haproxy\u670d\u52a1<\/p>\n<p>systemctl <span class=\"token builtin class-name\">enable<\/span> haproxy <span class=\"token parameter variable\">&#8211;now<\/span><\/p>\n<p>\u5ba2\u6237\u7aef\u6d4b\u8bd5\u9a8c\u8bc1&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u666e\u901a80\u8bf7\u6c42&#xff0c;\u843d\u5230back_web\u7ec4<\/span><br \/>\n<span class=\"token keyword\">for<\/span> <span class=\"token for-or-select variable\">n<\/span> <span class=\"token keyword\">in<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">..<\/span><span class=\"token number\">90<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">do<\/span> <span class=\"token function\">curl<\/span> <span class=\"token punctuation\">[<\/span>http:\/\/10.1.8.10<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">(<\/span>http:\/\/10.1.8.10<span class=\"token punctuation\">)<\/span> -s<span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">done<\/span> <span class=\"token operator\">|<\/span><span class=\"token function\">sort<\/span><span class=\"token operator\">|<\/span><span class=\"token function\">uniq<\/span> <span class=\"token parameter variable\">-c<\/span><br \/>\n<span class=\"token comment\"># \u8bbf\u95eetxt\u540e\u7f00&#xff0c;\u89e6\u53d1acl\u89c4\u5219&#xff0c;\u843d\u5230back_test 81\u7aef\u53e3\u7ec4<\/span><br \/>\n<span class=\"token function\">curl<\/span> <span class=\"token punctuation\">[<\/span>http:\/\/10.1.8.10\/index.txt<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">(<\/span>http:\/\/10.1.8.10\/index.txt<span class=\"token punctuation\">)<\/span><\/p>\n<h4>3.4 HAProxy\u56db\u5c42TCP\u4ee3\u7406\u793a\u4f8b&#xff08;\u4ee3\u7406SSH&#xff09;<\/h4>\n<p>\u4f7f\u7528listen\u4e00\u4f53\u5316\u914d\u7f6e&#xff0c;\u76f4\u63a5\u8f6c\u53d1TCP\u6d41\u91cf&#xff0c;\u4ee3\u7406SSH 22\u7aef\u53e3&#xff0c;\u5bf9\u5916\u76d1\u542c1022\u7aef\u53e3&#xff1a;<\/p>\n<p>listen ssh_proxy<br \/>\n    mode tcp          <span class=\"token comment\"># mode tcp \u5f00\u542f\u56db\u5c42\u6a21\u5f0f<\/span><br \/>\n    <span class=\"token builtin class-name\">bind<\/span> *:1022<br \/>\n    balance roundrobin<br \/>\n    server web1 <span class=\"token number\">10.1<\/span>.8.11:22 check<br \/>\n    server web2 <span class=\"token number\">10.1<\/span>.8.12:22 check<br \/>\n    server web3 <span class=\"token number\">10.1<\/span>.8.13:22 check<\/p>\n<p>\u91cd\u542fhaproxy&#xff0c;\u5ba2\u6237\u7aef\u6d4b\u8bd5&#xff1a;<\/p>\n<p><span class=\"token comment\"># \u901a\u8fc71022\u7aef\u53e3\u4ee3\u7406ssh\u767b\u5f55\u540e\u7aef\u673a\u5668<\/span><br \/>\n<span class=\"token keyword\">for<\/span> <span class=\"token for-or-select variable\">n<\/span> <span class=\"token keyword\">in<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">..<\/span><span class=\"token number\">90<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">do<\/span> <span class=\"token function\">ssh<\/span> root&#064;10.1.8.10 <span class=\"token parameter variable\">-p<\/span> <span class=\"token number\">1022<\/span> <span class=\"token function\">hostname<\/span> <span class=\"token operator\"><span class=\"token file-descriptor important\">2<\/span>&gt;<\/span>\/dev\/null<span class=\"token punctuation\">;<\/span><span class=\"token keyword\">done<\/span><span class=\"token operator\">|<\/span><span class=\"token function\">sort<\/span><span class=\"token operator\">|<\/span><span class=\"token function\">uniq<\/span> <span class=\"token parameter variable\">-c<\/span><\/p>\n<h4>3.5 HAProxy\u76d1\u63a7\u7edf\u8ba1\u9762\u677f<\/h4>\n<p>\u589e\u52a0\u76d1\u63a7\u9875\u9762\u914d\u7f6e&#xff0c;\u53ef\u4ee5\u6d4f\u89c8\u5668\u67e5\u770b\u540e\u7aef\u8282\u70b9\u72b6\u6001\u3001\u8fde\u63a5\u6570\u3001\u8bf7\u6c42\u7edf\u8ba1&#xff1a;<\/p>\n<p>listen haproxy_stats<br \/>\n    <span class=\"token builtin class-name\">bind<\/span> *:8404<br \/>\n    stats <span class=\"token builtin class-name\">enable<\/span><br \/>\n    stats uri \/stats<br \/>\n    stats auth admin:123456<\/p>\n<p>\u91cd\u542fhaproxy&#xff0c;\u6d4f\u89c8\u5668\u8bbf\u95ee http:\/\/IP:8404\/stats&#xff0c;\u8f93\u5165\u8d26\u53f7\u5bc6\u7801\u67e5\u770b\u96c6\u7fa4\u72b6\u6001\u3002<\/p>\n<hr \/>\n<h3>&#x1f6a8;\u65b0\u624b\u5e38\u89c1\u5751\u5408\u96c6<\/h3>\n<li>\n<p>\u274cNginx upstream\u914d\u7f6e\u5b8c\u8bbf\u95ee\u62a5\u9519502 Bad Gateway<\/p>\n<p>\u2705\u6392\u67e5&#xff1a;\u540e\u7aefweb\u670d\u52a1\u662f\u5426\u6b63\u5e38&#xff1b;\u7f51\u7edc\u662f\u5426\u901a&#xff1b;\u9632\u706b\u5899\/selinux&#xff1b;proxy_pass\u540e\u9762\u7684\u540d\u5b57\u4e0eupstream\u7ec4\u540d\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<\/li>\n<li>\n<p>\u274cip_hash\u6a21\u5f0f&#xff0c;\u5927\u91cf\u5185\u7f51\u7528\u6237\u8bf7\u6c42\u5168\u90e8\u6253\u5230\u540c\u4e00\u53f0\u540e\u7aef<\/p>\n<p>\u2705NAT\u5185\u7f51\u73af\u5883&#xff0c;\u591a\u7528\u6237\u5171\u4eab\u540c\u4e00\u4e2a\u516c\u7f51\u51fa\u53e3IP&#xff0c;ip_hash\u4e0d\u9002\u5408\u8be5\u573a\u666f&#xff1b;\u6539\u7528\u52a0\u6743\u8f6e\u8be2\/least_conn\u3002<\/p>\n<\/li>\n<li>\n<p>\u274cNginx\u914d\u7f6ebackup\u5907\u7528\u8282\u70b9&#xff0c;\u4e00\u76f4\u4e0d\u63a5\u6536\u6d41\u91cf<\/p>\n<p>\u2705backup\u8282\u70b9\u53ea\u6709\u5168\u90e8\u4e3bserver\u6545\u969c\u4e0b\u7ebf\u4e4b\u540e&#xff0c;\u624d\u4f1a\u63a5\u7ba1\u4e1a\u52a1\u6d41\u91cf&#xff0c;\u6b63\u5e38\u60c5\u51b5\u4e0d\u4f1a\u5206\u914d\u8bf7\u6c42\u3002<\/p>\n<\/li>\n<li>\n<p>\u274cHAProxy\u4e03\u5c42\u4ee3\u7406&#xff0c;\u540e\u7aef\u62ff\u4e0d\u5230\u7528\u6237\u771f\u5b9e\u8bbf\u95eeIP<\/p>\n<p>\u2705HAProxy\u9ed8\u8ba4\u6e90IP\u662f\u4ee3\u7406\u670d\u52a1\u5668\u81ea\u8eab&#xff1b;\u4e03\u5c42\u6a21\u5f0f\u914d\u7f6eX\u2011Forwarded\u2011For\u8bf7\u6c42\u5934&#xff0c;\u540e\u7aefweb\u8bfb\u53d6\u8be5http\u5934\u62ff\u5230\u771f\u5b9e\u5ba2\u6237\u7aefIP\u3002<\/p>\n<\/li>\n<li>\n<p>\u274cHAProxy\u56db\u5c42\u4ee3\u7406\u914d\u7f6e\u5b8cssh\u8fde\u63a5\u5931\u8d25<\/p>\n<p>\u2705\u786e\u8ba4mode tcp&#xff1b;\u540e\u7aefsshd\u670d\u52a1\u6b63\u5e38&#xff1b;check\u5065\u5eb7\u68c0\u67e5\u662f\u5426\u5931\u8d25&#xff0c;\u770bhaproxy\u65e5\u5fd7\u6392\u67e5\u3002<\/p>\n<\/li>\n<li>\n<p>\u274cacl\u89c4\u5219\u4e0d\u751f\u6548&#xff0c;txt\u540e\u7f00\u6ca1\u6709\u8f6c\u53d1\u5230\u6307\u5b9a\u540e\u7aef<\/p>\n<p>\u2705acl\u8bed\u6cd5&#xff1b;url_reg\u6b63\u5219\u8868\u8fbe\u5f0f\u5199\u6cd5&#xff1b;\u914d\u7f6e\u4fee\u6539\u540e\u5fc5\u987b\u91cd\u8f7dhaproxy\u3002<\/p>\n<\/li>\n<li>\n<p>\u274cNginx\u7684least_time\u7b97\u6cd5\u914d\u7f6e\u62a5\u9519<\/p>\n<p>\u2705\u5f00\u6e90\u514d\u8d39Nginx\u4e0d\u652f\u6301least_time&#xff0c;\u8be5\u7279\u6027\u4ec5Nginx Plus\u5546\u4e1a\u7248\u53ef\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>\u274chaproxy\u76d1\u63a7\u9875\u9762\u6253\u4e0d\u5f00<\/p>\n<p>\u2705\u786e\u8ba4\u76d1\u542c\u7aef\u53e3\u3001\u9632\u706b\u5899\u653e\u884c&#xff1b;\u8d26\u53f7\u5bc6\u7801\u914d\u7f6e\u6b63\u786e&#xff1b;\u670d\u52a1\u5df2\u91cd\u8f7d\u3002<\/p>\n<\/li>\n<h3>\u603b\u7ed3<\/h3>\n<li>\u8d1f\u8f7d\u5747\u8861\u539f\u7406&#xff1a;\u53cd\u5411\u4ee3\u7406\u4f5c\u4e3a\u6d41\u91cf\u5165\u53e3&#xff0c;\u628a\u8bf7\u6c42\u5206\u53d1\u591a\u53f0\u540e\u7aef\u670d\u52a1\u5668&#xff0c;\u5b9e\u73b0\u6269\u5bb9\u4e0e\u6545\u969c\u9694\u79bb&#xff1b;\u5206\u6e05Nginx\u5404\u4e2a\u7248\u672c\u7684\u533a\u522b\u3002<\/li>\n<li>Nginx upstream\u6a21\u5757&#xff1a;\u652f\u6301\u8f6e\u8be2\u3001\u52a0\u6743\u8f6e\u8be2\u3001ip_hash\u4f1a\u8bdd\u4fdd\u6301\u3001least_conn\u6700\u5c11\u8fde\u63a5&#xff1b;\u638c\u63e1weight\u3001backup\u3001down\u3001max_fails\u53c2\u6570&#xff1b;\u5f00\u6e90\u7248\u65e0least_time\u3002<\/li>\n<li>HAProxy&#xff1a;\u540c\u65f6\u652f\u6301\u56db\u5c42TCP\u4ee3\u7406\u3001\u4e03\u5c42HTTP\u4ee3\u7406&#xff1b;frontend\/backend\u5206\u79bb\u914d\u7f6e&#xff0c;\u4e5f\u53ef\u4ee5\u7528listen\u4e00\u4f53\u5316&#xff1b;acl\u5b9e\u73b0\u6309URL\u540e\u7f00\u7b49\u6761\u4ef6\u8def\u7531&#xff1b;\u5185\u7f6eweb\u76d1\u63a7\u9762\u677f&#xff1b;\u652f\u6301roundrobin\u3001leastconn\u3001source\u7b49\u591a\u79cd\u8c03\u5ea6\u7b97\u6cd5\u3002<\/li>\n<li>\u56db\u5c42\u53ea\u8f6c\u53d1TCP\u62a5\u6587&#xff0c;\u9002\u5408SSH\u3001MySQL\u7b49TCP\u4e1a\u52a1&#xff1b;\u4e03\u5c42\u89e3\u6790HTTP&#xff0c;\u53ef\u4ee5\u505aURL\u3001header\u590d\u6742\u903b\u8f91\u5206\u53d1&#xff1b;\u56db\u5c42\u4e03\u5c42\u540e\u7aef\u9ed8\u8ba4\u770b\u5230\u7684\u662f\u4ee3\u7406IP&#xff0c;\u4e03\u5c42\u9700\u8981X\u2011Forwarded\u2011For\u4f20\u9012\u771f\u5b9e\u5ba2\u6237\u7aefIP\u3002<\/li>\n<h3>\u601d\u8003\u9898&amp;\u4e92\u52a8<\/h3>\n<li>Nginx\u4e2dip_hash\u4f5c\u7528\u662f\u4ec0\u4e48&#xff1f;\u4ec0\u4e48\u573a\u666f\u4e0d\u9002\u5408\u4f7f\u7528ip_hash&#xff1f;<\/li>\n<li>HAProxy\u56db\u5c42\u4ee3\u7406mode tcp\u4e0e\u4e03\u5c42http\u6a21\u5f0f&#xff0c;\u6838\u5fc3\u533a\u522b\u662f\u4ec0\u4e48&#xff0c;\u5206\u522b\u9002\u5408\u4ec0\u4e48\u4e1a\u52a1&#xff1f;<\/li>\n<li>upstream\u91cc\u9762backup\u53c2\u6570\u7684\u4f5c\u7528\u662f\u4ec0\u4e48&#xff1f;<\/li>\n<li>HAProxy\u7684acl\u89c4\u5219\u662f\u5728\u54ea\u4e00\u5c42\u751f\u6548&#xff0c;\u56db\u5c42TCP\u6a21\u5f0f\u80fd\u4e0d\u80fd\u4f7f\u7528url_reg\u5339\u914dURL&#xff1f;<\/li>\n<p>\u6b22\u8fce\u8bc4\u8bba\u533a\u5206\u4eab\u8e29\u5751&#xff0c;\u591a\u673a\u73af\u5883\u5b8c\u6574\u590d\u73b0\u5b9e\u9a8c&#xff0c;\u7406\u89e3\u4f1a\u66f4\u52a0\u6df1\u523b&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u8d1f\u8f7d\u5747\u8861\u5b9e\u6218&#xff5c;NginxHAProxy\u56db\u5c42\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u6574\u5b9e\u64cd<br \/>\n\u5f15\u8a00<br \/>\n\u672c\u7bc7\u9002\u5408CentOS7\u8fd0\u7ef4\u5165\u95e8\u540c\u5b66&#xff0c;\u8bfb\u5b8c\u672c\u6587\u4f60\u53ef\u4ee5\u5b66\u4f1a&#xff1a;<br \/>\n\u7406\u89e3\u8d1f\u8f7d\u5747\u8861\u6838\u5fc3\u601d\u60f3&#xff0c;\u5206\u6e05\u53cd\u5411\u4ee3\u7406\u4e0e\u8d1f\u8f7d\u5747\u8861\u7684\u5173\u7cfb&#xff0c;\u8ba4\u8bc6Nginx\u591a\u4e2a\u4e3b\u6d41\u7248\u672c\u5dee\u5f02&#xff1b;Nginx\u7684upstream\u6a21\u5757\u5b9e\u64cd&#xff0c;\u638c\u63e1\u8f6e\u8be2\u3001\u6743\u91cd\u3001ip_hash\u3001least_conn\u7b49\u8c03\u5ea6\u7b97\u6cd5&#xff0c;\u540e\u7aef\u8282\u70b9backup\u3001down\u3001\u5065\u5eb7\u68c0\u67e5\u53c2\u6570<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,117,43,258,44],"topic":[],"class_list":["post-115576","post","type-post","status-publish","format-standard","hentry","category-server","tag-linux","tag-nginx","tag-43","tag-258","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux\u8d1f\u8f7d\u5747\u8861\u5b9e\u6218\uff5cNginx+HAProxy\u56db\u5c42\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u6574\u5b9e\u64cd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/115576.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux\u8d1f\u8f7d\u5747\u8861\u5b9e\u6218\uff5cNginx+HAProxy\u56db\u5c42\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u6574\u5b9e\u64cd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Linux\u8d1f\u8f7d\u5747\u8861\u5b9e\u6218&#xff5c;NginxHAProxy\u56db\u5c42\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u6574\u5b9e\u64cd \u5f15\u8a00 \u672c\u7bc7\u9002\u5408CentOS7\u8fd0\u7ef4\u5165\u95e8\u540c\u5b66&#xff0c;\u8bfb\u5b8c\u672c\u6587\u4f60\u53ef\u4ee5\u5b66\u4f1a&#xff1a; \u7406\u89e3\u8d1f\u8f7d\u5747\u8861\u6838\u5fc3\u601d\u60f3&#xff0c;\u5206\u6e05\u53cd\u5411\u4ee3\u7406\u4e0e\u8d1f\u8f7d\u5747\u8861\u7684\u5173\u7cfb&#xff0c;\u8ba4\u8bc6Nginx\u591a\u4e2a\u4e3b\u6d41\u7248\u672c\u5dee\u5f02&#xff1b;Nginx\u7684upstream\u6a21\u5757\u5b9e\u64cd&#xff0c;\u638c\u63e1\u8f6e\u8be2\u3001\u6743\u91cd\u3001ip_hash\u3001least_conn\u7b49\u8c03\u5ea6\u7b97\u6cd5&#xff0c;\u540e\u7aef\u8282\u70b9backup\u3001down\u3001\u5065\u5eb7\u68c0\u67e5\u53c2\u6570\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/115576.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-12T09:17:12+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/115576.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/115576.html\",\"name\":\"Linux\u8d1f\u8f7d\u5747\u8861\u5b9e\u6218\uff5cNginx+HAProxy\u56db\u5c42\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u6574\u5b9e\u64cd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-12T09:17:12+00:00\",\"dateModified\":\"2026-10-12T09:17:12+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/115576.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/115576.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/115576.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\u8d1f\u8f7d\u5747\u8861\u5b9e\u6218\uff5cNginx+HAProxy\u56db\u5c42\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u6574\u5b9e\u64cd\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linux\u8d1f\u8f7d\u5747\u8861\u5b9e\u6218\uff5cNginx+HAProxy\u56db\u5c42\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u6574\u5b9e\u64cd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/115576.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux\u8d1f\u8f7d\u5747\u8861\u5b9e\u6218\uff5cNginx+HAProxy\u56db\u5c42\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u6574\u5b9e\u64cd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Linux\u8d1f\u8f7d\u5747\u8861\u5b9e\u6218&#xff5c;NginxHAProxy\u56db\u5c42\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u6574\u5b9e\u64cd \u5f15\u8a00 \u672c\u7bc7\u9002\u5408CentOS7\u8fd0\u7ef4\u5165\u95e8\u540c\u5b66&#xff0c;\u8bfb\u5b8c\u672c\u6587\u4f60\u53ef\u4ee5\u5b66\u4f1a&#xff1a; \u7406\u89e3\u8d1f\u8f7d\u5747\u8861\u6838\u5fc3\u601d\u60f3&#xff0c;\u5206\u6e05\u53cd\u5411\u4ee3\u7406\u4e0e\u8d1f\u8f7d\u5747\u8861\u7684\u5173\u7cfb&#xff0c;\u8ba4\u8bc6Nginx\u591a\u4e2a\u4e3b\u6d41\u7248\u672c\u5dee\u5f02&#xff1b;Nginx\u7684upstream\u6a21\u5757\u5b9e\u64cd&#xff0c;\u638c\u63e1\u8f6e\u8be2\u3001\u6743\u91cd\u3001ip_hash\u3001least_conn\u7b49\u8c03\u5ea6\u7b97\u6cd5&#xff0c;\u540e\u7aef\u8282\u70b9backup\u3001down\u3001\u5065\u5eb7\u68c0\u67e5\u53c2\u6570","og_url":"https:\/\/www.wsisp.com\/helps\/115576.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-12T09:17:12+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/115576.html","url":"https:\/\/www.wsisp.com\/helps\/115576.html","name":"Linux\u8d1f\u8f7d\u5747\u8861\u5b9e\u6218\uff5cNginx+HAProxy\u56db\u5c42\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u6574\u5b9e\u64cd - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-12T09:17:12+00:00","dateModified":"2026-10-12T09:17:12+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/115576.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/115576.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/115576.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux\u8d1f\u8f7d\u5747\u8861\u5b9e\u6218\uff5cNginx+HAProxy\u56db\u5c42\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u6574\u5b9e\u64cd"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/115576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=115576"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/115576\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=115576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=115576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=115576"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=115576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}