{"id":78665,"date":"2026-02-27T22:32:50","date_gmt":"2026-02-27T14:32:50","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/78665.html"},"modified":"2026-02-27T22:32:50","modified_gmt":"2026-02-27T14:32:50","slug":"%e4%bb%8e%e5%85%a5%e9%97%a8%e5%88%b0%e5%ae%9e%e6%88%98%ef%bc%9ahaproxy-%e8%b4%9f%e8%bd%bd%e5%9d%87%e8%a1%a1%e4%b8%8e%e4%bb%a3%e7%90%86%e5%85%a8%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/78665.html","title":{"rendered":"\u4ece\u5165\u95e8\u5230\u5b9e\u6218\uff1aHAProxy \u8d1f\u8f7d\u5747\u8861\u4e0e\u4ee3\u7406\u5168\u89e3\u6790"},"content":{"rendered":"<h2 id=\"%E4%B8%80%E3%80%81HAProxy%20%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E5%AD%A6%E5%AE%83%EF%BC%9F\">\u4e00\u3001HAProxy \u662f\u4ec0\u4e48&#xff1f;\u4e3a\u4ec0\u4e48\u8981\u5b66\u5b83&#xff1f;<\/h2>\n<p><span style=\"color:#fe2c24\">HAProxy &#061; \u9ad8\u6027\u80fd\u3001\u5f00\u6e90\u7684\u8d1f\u8f7d\u5747\u8861 &#043; \u53cd\u5411\u4ee3\u7406\u8f6f\u4ef6<\/span><\/p>\n<h3 id=\"1.%20HAProxy%20%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F\">1. HAProxy \u662f\u4ec0\u4e48&#xff1f;<\/h3>\n<p>\u4e3b\u8981\u7528\u6765&#xff1a;<\/p>\n<ul>\n<li>\u628a\u7528\u6237\u8bf7\u6c42<span style=\"color:#9c8ec1\">\u5747\u5300\u5206\u7ed9\u591a\u53f0\u670d\u52a1\u5668<\/span><\/li>\n<li>\u505a<span style=\"color:#9c8ec1\">\u6d41\u91cf\u5165\u53e3\u3001\u7f51\u5173\u3001\u9ad8\u53ef\u7528<\/span><\/li>\n<li>\u652f\u6301 HTTP\/HTTPS\/TCP \u7b49&#xff0c;<span style=\"color:#9c8ec1\">\u6027\u80fd\u6781\u5f3a\u3001\u5f88\u7a33\u5b9a<\/span><\/li>\n<\/ul>\n<p>\u7b80\u5355\u7406\u89e3&#xff1a;\u4f60\u6709 10 \u53f0 Web \u670d\u52a1\u5668&#xff0c;\u7528\u6237\u4e0d\u77e5\u9053\u4e5f\u4e0d\u7528\u7ba1\u8fde\u54ea\u53f0&#xff0c;\u5168\u90fd\u5148\u8fde HAProxy&#xff0c;\u5b83\u5e2e\u4f60\u81ea\u52a8\u5206\u53d1\u3002<\/p>\n<h3 id=\"2.%C2%A0%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E5%AD%A6%E5%AE%83%EF%BC%9F\">2.\u00a0\u4e3a\u4ec0\u4e48\u8981\u5b66\u5b83&#xff1f;<\/h3>\n<p>1. \u51e0\u4e4e\u6240\u6709\u516c\u53f8\u90fd\u5728\u7528<\/p>\n<p>Nginx\u3001HAProxy \u662f\u540e\u7aef \/ \u8fd0\u7ef4 \/\u67b6\u6784\u6807\u914d&#xff0c;\u9762\u8bd5\u5fc5\u95ee\u3002<\/p>\n<p>2. \u4e13\u95e8\u625b\u9ad8\u5e76\u53d1\u3001\u625b\u6d41\u91cf<\/p>\n<p>\u6bd4 Nginx \u66f4\u64c5\u957f\u957f\u8fde\u63a5\u3001TCP \u4ee3\u7406\u3001\u56db\u5c42 \/ \u4e03\u5c42\u8d1f\u8f7d\u5747\u8861&#xff0c;\u5f88\u591a\u7f51\u5173\u3001API \u5165\u53e3\u3001\u5fae\u670d\u52a1\u7f51\u5173\u90fd\u7528\u5b83\u3002<\/p>\n<p>3. \u5b66\u5b8c\u76f4\u63a5\u63d0\u5347\u5c97\u4f4d\u7ade\u4e89\u529b<\/p>\n<p>\u4f1aHAProxy &#061; \u4f60\u80fd\u641e\u5b9a&#xff1a;<\/p>\n<ul>\n<li><span style=\"color:#494949\">\u670d\u52a1\u9ad8\u53ef\u7528<\/span><\/li>\n<li><span style=\"color:#494949\">\u6d41\u91cf\u5206\u53d1<\/span><\/li>\n<li><span style=\"color:#494949\">\u6545\u969c\u81ea\u52a8\u5254\u9664<\/span><\/li>\n<li><span style=\"color:#494949\">\u7f51\u5173\u5c42\u67b6\u6784<\/span><\/li>\n<\/ul>\n<p>4.\u00a0\u4e91\u539f\u751f \/ \u5bb9\u5668 \/ K8s \u5fc5\u5907\u57fa\u7840<\/p>\n<p>Kubernetes \u91cc\u7684 Service\u3001Ingress \u5e95\u5c42\u601d\u60f3&#xff0c;\u8ddf HAProxy \u8d1f\u8f7d\u5747\u8861\u5b8c\u5168\u540c\u6e90&#xff0c;\u5b66\u4f1a HAProxy \u5f88\u5bb9\u6613\u770b\u61c2 K8s \u7f51\u7edc\u3002<\/p>\n<hr \/>\n<h2 id=\"%E4%BA%8C%E3%80%81HAProxy%20%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5\">\u4e8c\u3001HAProxy \u6838\u5fc3\u6982\u5ff5<\/h2>\n<p>1. \u56db\u5c42\u8d1f\u8f7d\u5747\u8861&#xff08;TCP&#xff09;<\/p>\n<p>\u53ea\u770b IP &#043; \u7aef\u53e3&#xff0c;\u8f6c\u53d1\u5feb&#xff0c;\u9002\u5408 MySQL\u3001Redis\u3001RPC \u7b49\u3002<\/p>\n<p>2. \u4e03\u5c42\u8d1f\u8f7d\u5747\u8861&#xff08;HTTP\/HTTPS&#xff09;<\/p>\n<p>\u80fd\u770b URL\u3001\u57df\u540d\u3001Cookie&#xff0c;\u505a\u8def\u7531\u3001\u4f1a\u8bdd\u4fdd\u6301\u3001\u5065\u5eb7\u68c0\u67e5\u3002<\/p>\n<p>3. \u524d\u7aef frontend<\/p>\n<p>\u63a5\u6536\u7528\u6237\u8bf7\u6c42\u7684\u5165\u53e3&#xff08;\u76d1\u542c\u7aef\u53e3&#xff09;\u3002<\/p>\n<p>4. \u540e\u7aef backend<\/p>\n<p>\u771f\u6b63\u63d0\u4f9b\u670d\u52a1\u7684\u670d\u52a1\u5668\u96c6\u7fa4\u3002<\/p>\n<p>5. \u5065\u5eb7\u68c0\u67e5<\/p>\n<p>\u81ea\u52a8\u628a\u6302\u6389\u7684\u670d\u52a1\u5668\u8e22\u51fa\u53bb&#xff0c;\u6062\u590d\u540e\u518d\u52a0\u56de\u6765\u3002<\/p>\n<p>6.\u00a0\u8d1f\u8f7d\u5747\u8861\u7b97\u6cd5<\/p>\n<ul>\n<li>\n<p>roundrobin&#xff1a;\u8f6e\u8be2&#xff08;\u9ed8\u8ba4&#xff09;<\/p>\n<\/li>\n<li>\n<p>leastconn&#xff1a;\u6700\u5c11\u8fde\u63a5&#xff08;\u9002\u5408\u957f\u8fde\u63a5&#xff09;<\/p>\n<\/li>\n<li>\n<p>source&#xff1a;\u6309\u5ba2\u6237\u7aef IP \u54c8\u5e0c&#xff08;\u4f1a\u8bdd\u4fdd\u6301&#xff09;<\/p>\n<\/li>\n<\/ul>\n<p>7.\u00a0HAProxy \u548c Nginx \u533a\u522b&#xff1f;<\/p>\n<ul>\n<li>\n<p>Nginx&#xff1a;\u504f Web \u670d\u52a1\u5668\u3001\u9759\u6001\u8d44\u6e90\u3001\u7f13\u5b58\u3002<\/p>\n<\/li>\n<li>\n<p>HAProxy&#xff1a;<span style=\"color:#fe2c24\">\u4e13\u4e1a\u8d1f\u8f7d\u5747\u8861<\/span>&#xff0c;\u56db\u5c42 \/ TCP \u66f4\u5f3a&#xff0c;\u957f\u8fde\u63a5\u3001\u9ad8\u5e76\u53d1\u66f4\u7a33\u3002<\/p>\n<\/li>\n<\/ul>\n<p>8.\u00a0\u56db\u5c42\u548c\u4e03\u5c42\u533a\u522b&#xff1f;<\/p>\n<ul>\n<li>\n<p>\u56db\u5c42&#xff1a;\u5feb\u3001\u901a\u7528&#xff08;TCP&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e03\u5c42&#xff1a;\u667a\u80fd\u3001\u80fd\u8bc6\u522b HTTP&#xff0c;\u4f46\u7a0d\u8017\u6027\u80fd\u3002<\/p>\n<\/li>\n<\/ul>\n<p>9.\u00a0\u600e\u4e48\u4fdd\u8bc1\u9ad8\u53ef\u7528&#xff1f;<\/p>\n<ul>\n<li>\n<p>\u4e24\u53f0 HAProxy &#043; Keepalived \u505a\u865a\u62df IP&#xff08;VIP&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h2 id=\"%E4%B8%89%E3%80%81%E7%8E%AF%E5%A2%83%E4%B8%8E%E6%A0%B8%E5%BF%83%E9%85%8D%E7%BD%AE\" style=\"background-color:transparent\">\u4e09\u3001\u73af\u5883\u4e0e\u6838\u5fc3\u914d\u7f6e<\/h2>\n<h3 id=\"1.%20HAProxy%20%E7%9A%84%E5%AE%89%E8%A3%85%E5%8F%8A%E9%85%8D%E7%BD%AE%E5%8F%82%E6%95%B0%E8%AF%A6%E8%A7%A3\">1. HAProxy \u7684\u5b89\u88c5\u53ca\u914d\u7f6e\u53c2\u6570\u8be6\u89e3<\/h3>\n<h4 id=\"1.1%20%E5%9F%BA%E7%A1%80%E5%AE%89%E8%A3%85\">1.1 \u57fa\u7840\u5b89\u88c5<\/h4>\n<p>#\u5728\u8c03\u5ea6\u5668&#xff08;\u53cc\u7f51\u5361\u4e3b\u673a\u4e2d&#xff09;<br \/>\n[root&#064;haproxy ~]# dnf install haproxy.x86_64 -y     # \u5b89\u88c5Haproxy<br \/>\n[root&#064;haproxy ~]# systemctl enable &#8211;now haproxy    # \u5f00\u673a\u81ea\u542f\u5e76\u7acb\u5373\u542f\u52a8\u670d\u52a1<br \/>\nCreated symlink \/etc\/systemd\/system\/multi-user.target.wants\/haproxy.service \u2192 \/usr\/lib\/systemd\/system\/haproxy.service. <\/p>\n<h4 id=\"1.2%C2%A0%E5%9F%BA%E7%A1%80%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E5%AE%9E%E7%8E%B0\">1.2\u00a0\u57fa\u7840\u8d1f\u8f7d\u5747\u8861\u5b9e\u73b0<\/h4>\n<h5 id=\"%E6%96%B9%E5%BC%8F%201%EF%BC%9A%E5%89%8D%E5%90%8E%E7%AB%AF%E5%88%86%E7%A6%BB%EF%BC%88frontend%20%2B%20backend%EF%BC%89\">\u65b9\u5f0f 1&#xff1a;\u524d\u540e\u7aef\u5206\u79bb&#xff08;frontend &#043; backend&#xff09;<\/h5>\n<p>\u914d\u7f6e\u903b\u8f91&#xff1a;<\/p>\n<ul>\n<li>\n<p>frontend webcluster&#xff1a;\u5b9a\u4e49\u524d\u7aef\u76d1\u542c\u89c4\u5219&#xff0c;\u7ed1\u5b9a 80 \u7aef\u53e3\u3001\u6307\u5b9a HTTP \u6a21\u5f0f&#xff0c;\u5339\u914d\u8bf7\u6c42\u540e\u8f6c\u53d1\u5230webserver-80\u540e\u7aef\u96c6\u7fa4\u3002<\/p>\n<\/li>\n<li>\n<p>backend webserver-80&#xff1a;\u5b9a\u4e49\u540e\u7aef\u670d\u52a1\u5668\u6c60&#xff0c;\u5305\u542b\u4e24\u53f0 Web \u8282\u70b9&#xff08;192.168.0.10\/20:80&#xff09;&#xff0c;\u5e76\u914d\u7f6e\u5065\u5eb7\u68c0\u67e5\u53c2\u6570&#xff1a;<\/p>\n<ul>\n<li>\n<p>check&#xff1a;\u542f\u7528\u5065\u5eb7\u68c0\u67e5&#xff1b;<\/p>\n<\/li>\n<li>\n<p>inter 3s&#xff1a;\u6bcf 3 \u79d2\u68c0\u67e5\u4e00\u6b21&#xff1b;<\/p>\n<\/li>\n<li>\n<p>fall 3&#xff1a;\u8fde\u7eed 3 \u6b21\u68c0\u67e5\u5931\u8d25\u5219\u6807\u8bb0\u4e3a\u4e0d\u53ef\u7528&#xff1b;<\/p>\n<\/li>\n<li>\n<p>rise 5&#xff1a;\u8fde\u7eed 5 \u6b21\u68c0\u67e5\u6210\u529f\u5219\u6062\u590d\u53ef\u7528\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>#\u524d\u540e\u7aef\u5206\u5f00\u8bbe\u5b9a<br \/>\n[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<\/p>\n<p>frontend webcluster<br \/>\n    bind            *:80<br \/>\n    mode            http<br \/>\n    use_backend     webserver-80<\/p>\n<p>backend webserver-80<br \/>\n    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5<br \/>\n    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service    #\u91cd\u542fhaproxy\u670d\u52a1 <\/p>\n<p>\u9a8c\u8bc1\u6548\u679c&#xff1a;\u901a\u8fc7curl 172.25.254.100\u6d4b\u8bd5&#xff0c;\u8bf7\u6c42\u4f1a\u8f6e\u8be2\u5206\u53d1\u5230 web1 \u548c web2&#xff0c;\u8fd4\u56de\u4e0d\u540c\u8282\u70b9\u7684\u6807\u8bc6\u4fe1\u606f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"113\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143246-69a1ab0eecc0e.png\" width=\"519\" \/><\/p>\n<h5 id=\"%E6%96%B9%E5%BC%8F%202%EF%BC%9Alisten%20%E6%95%B4%E5%90%88%E5%BC%8F%E9%85%8D%E7%BD%AE\">\u65b9\u5f0f 2&#xff1a;listen \u6574\u5408\u5f0f\u914d\u7f6e<\/h5>\n<p>\u914d\u7f6e\u903b\u8f91&#xff1a;\u5c06\u524d\u7aef\u76d1\u542c\u548c\u540e\u7aef\u670d\u52a1\u5668\u6c60\u5408\u5e76\u5230listen\u5757\u4e2d&#xff0c;\u7b80\u5316\u914d\u7f6e&#xff08;\u9002\u5408\u5c0f\u578b\u96c6\u7fa4&#xff09;\u3002<\/p>\n<p>#\u7528listen\u65b9\u5f0f\u4e66\u5199\u8d1f\u8f7d\u5747\u8861<br \/>\n[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten webcluster<br \/>\n    bind        *:80<br \/>\n    mode        http<br \/>\n    server haha 192.168.0.10:80 check inter 3s fall 3 rise 5<br \/>\n    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<p>\u9a8c\u8bc1\u6548\u679c&#xff1a;\u4e0e\u524d\u540e\u7aef\u5206\u79bb\u65b9\u5f0f\u4e00\u81f4&#xff0c;\u8f6e\u8be2\u5206\u53d1\u8bf7\u6c42\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"85\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143247-69a1ab0f0ef4d.png\" width=\"500\" \/><\/p>\n<h4 id=\"1.3%C2%A0%E6%97%A5%E5%BF%97%E9%85%8D%E7%BD%AE\">1.3\u00a0\u65e5\u5fd7\u914d\u7f6e<\/h4>\n<p>\u5b9e\u73b0 Haproxy \u65e5\u5fd7\u7684\u8fdc\u7a0b\u8f6c\u53d1&#xff0c;\u4fbf\u4e8e\u96c6\u4e2d\u76d1\u63a7\u548c\u5ba1\u8ba1&#xff1a;<\/p>\n<h5 id=\"%E6%AD%A5%E9%AA%A4%201%EF%BC%9A%E6%97%A5%E5%BF%97%E6%8E%A5%E6%94%B6%E7%AB%AF%E9%85%8D%E7%BD%AE%EF%BC%88192.168.0.10%EF%BC%89\">\u6b65\u9aa4 1&#xff1a;\u65e5\u5fd7\u63a5\u6536\u7aef\u914d\u7f6e&#xff08;192.168.0.10&#xff09;<\/h5>\n<p>\u4fee\u6539\/etc\/rsyslog.conf&#xff0c;\u542f\u7528 UDP 514 \u7aef\u53e3\u63a5\u6536\u65e5\u5fd7&#xff1a;<\/p>\n<p>#\u5728192.168.0.10 \u5f00\u542f\u63a5\u53d7\u65e5\u5fd7\u7684\u7aef\u53e3<br \/>\n[root&#064;webserver1 ~]# vim \/etc\/rsyslog.conf<br \/>\n 32 module(load&#061;&#034;imudp&#034;) # needs to be done just once<br \/>\n 33 input(type&#061;&#034;imudp&#034; port&#061;&#034;514&#034;) <\/p>\n<p>\u91cd\u542f rsyslog \u670d\u52a1&#xff0c;\u9a8c\u8bc1\u7aef\u53e3\u76d1\u542c&#xff08;netstat -antlupe | grep rsyslog&#xff09;\u3002<\/p>\n<p>[root&#064;webserver1 ~]# systemctl restart rsyslog.service<\/p>\n<p>#\u6d4b\u8bd5\u63a5\u53d7\u65e5\u5fd7\u7aef\u53e3\u662f\u5426\u5f00\u542f<br \/>\n[root&#064;webserver1 ~]# netstat -antlupe | grep rsyslog<br \/>\nudp        0      0 0.0.0.0:514             0.0.0.0:*                           0          74140      30965\/rsyslogd<br \/>\nudp6       0      0 :::514                  :::*                                0          74141      30965\/rsyslogd <\/p>\n<h5 id=\"%E6%AD%A5%E9%AA%A4%202%EF%BC%9AHaproxy%20%E7%AB%AF%E9%85%8D%E7%BD%AE\">\u6b65\u9aa4 2&#xff1a;Haproxy \u7aef\u914d\u7f6e<\/h5>\n<p>\u5728haproxy.cfg\u4e2d\u6dfb\u52a0\u65e5\u5fd7\u8f6c\u53d1\u89c4\u5219&#xff1a;log 192.168.0.10 local2&#xff08;local2\u4e3a\u65e5\u5fd7\u8bbe\u65bd\u7ea7\u522b&#xff09;\u3002<\/p>\n<p>[root&#064;haproxy haproxy]# vim haproxy.cfg<\/p>\n<p>log         192.168.0.10 local2<\/p>\n<p>[root&#064;haproxy haproxy]# systemctl restart haproxy.service    #\u91cd\u542f Haproxy <\/p>\n<p>\u91cd\u542f Haproxy \u540e&#xff0c;\u901a\u8fc7curl\u89e6\u53d1\u8bf7\u6c42&#xff0c;\u5728\u65e5\u5fd7\u63a5\u6536\u7aef\u67e5\u770b\/var\/log\/messages&#xff0c;\u53ef\u770b\u5230 Haproxy \u7684\u8bbf\u95ee\u65e5\u5fd7&#xff08;\u5305\u542b\u5ba2\u6237\u7aef IP\u3001\u8bf7\u6c42\u65f6\u95f4\u3001\u540e\u7aef\u8282\u70b9\u3001\u54cd\u5e94\u72b6\u6001\u7b49&#xff09;\u3002<\/p>\n<p>#\u9a8c\u8bc1<br \/>\n\u2500<br \/>\n[2026-01-23 15:13.54]  ~<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 curl 172.25.254.100<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\n                                                                                                    \u2714<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n[2026-01-23 15:19.05]  ~<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 curl 172.25.254.100<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\n[root&#064;webserver1 ~]# cat \/var\/log\/messages<\/p>\n<p>Jan 23 15:19:06 192.168.0.100 haproxy[31310]: 172.25.254.1:9514 [23\/Jan\/2026:15:19:06.320] webcluster webcluster\/haha 0\/0\/0\/1\/1 200 273 &#8211; &#8211; &#8212;- 1\/1\/0\/0\/0 0\/0 &#034;GET \/ HTTP\/1.1&#034;<br \/>\nJan 23 15:19:10 192.168.0.100 haproxy[31310]: 172.25.254.1:9519 [23\/Jan\/2026:15:19:10.095] webcluster webcluster\/hehe 0\/0\/0\/0\/0 200 273 &#8211; &#8211; &#8212;- 1\/1\/0\/0\/0 0\/0 &#034;GET \/ HTTP\/1.1&#034; <\/p>\n<h4 id=\"1.4%C2%A0%E5%A4%9A%E8%BF%9B%E7%A8%8B%E9%85%8D%E7%BD%AE\">1.4\u00a0\u591a\u8fdb\u7a0b\u914d\u7f6e<\/h4>\n<p>\u4f18\u5316 Haproxy \u7684 CPU \u5229\u7528\u7387&#xff0c;\u901a\u8fc7\u591a\u8fdb\u7a0b\u63d0\u5347\u5e76\u53d1\u5904\u7406\u80fd\u529b&#xff1a;<\/p>\n<h5 id=\"1.%20%E5%9F%BA%E7%A1%80%E5%A4%9A%E8%BF%9B%E7%A8%8B\">1. \u57fa\u7840\u591a\u8fdb\u7a0b<\/h5>\n<p>\u914d\u7f6enbproc 2&#xff1a;\u542f\u52a8 2 \u4e2a Haproxy \u8fdb\u7a0b&#xff1b;<\/p>\n<p>#\u9ed8\u8ba4haproxy\u662f\u5355\u8fdb\u7a0b<br \/>\n[root&#064;haproxy ~]# pstree -p | grep haproxy<br \/>\n           |-haproxy(31439)&#8212;haproxy(31441)-&#043;-{haproxy}(31442)<br \/>\n[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nnbproc      2 <\/p>\n<p>\u9a8c\u8bc1&#xff1a;pstree -p | grep haproxy\u53ef\u770b\u5230\u591a\u8fdb\u7a0b\u7ed3\u6784\u3002<\/p>\n<p>#\u9a8c\u8bc1<br \/>\n[root&#064;haproxy ~]# pstree -p | grep haproxy<br \/>\n           |-haproxy(31549)-&#043;-haproxy(31551)<br \/>\n           |                &#096;-haproxy(31552) <\/p>\n<h5 id=\"2.%C2%A0%E8%BF%9B%E7%A8%8B%20CPU%20%E7%BB%91%E5%AE%9A\">2.\u00a0\u8fdb\u7a0b CPU \u7ed1\u5b9a<\/h5>\n<p>\u914d\u7f6ecpu-map 1 0\/cpu-map 2 1&#xff1a;\u5c06\u8fdb\u7a0b 1 \u7ed1\u5b9a\u5230 CPU0&#xff0c;\u8fdb\u7a0b 2 \u7ed1\u5b9a\u5230 CPU1&#xff0c;\u907f\u514d\u8fdb\u7a0b\u62a2\u5360 CPU \u8d44\u6e90\u3002<\/p>\n<p>#\u591a\u8fdb\u7a0bcpu\u7ed1\u5b9a<br \/>\n[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\n    nbproc      2<br \/>\n    cpu-map     1 0<br \/>\n    cpu-map     2 1<br \/>\n[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<h5 id=\"3.%C2%A0%E8%BF%9B%E7%A8%8B%E7%8B%AC%E7%AB%8B%E5%A5%97%E6%8E%A5%E5%AD%97\">3.\u00a0\u8fdb\u7a0b\u72ec\u7acb\u5957\u63a5\u5b57<\/h5>\n<p>\u4e3a\u4e0d\u540c\u8fdb\u7a0b\u914d\u7f6e\u72ec\u7acb\u7684 stats \u5957\u63a5\u5b57&#xff08;\u7528\u4e8e\u8fdb\u7a0b\u7ba1\u7406&#xff09;<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\n    #stats socket \/var\/lib\/haproxy\/stats<br \/>\n    stats socket \/var\/lib\/haproxy\/haproxy1  mode 600 level admin process 1<br \/>\n    stats socket \/var\/lib\/haproxy\/haporxy2  mode 660 level admin process 1<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service<\/p>\n<p>\u9a8c\u8bc1&#xff1a;ll \/var\/lib\/haproxy\/\u53ef\u770b\u5230\u521b\u5efa\u7684\u5957\u63a5\u5b57\u6587\u4ef6\u3002<\/p>\n<p>#\u6548\u679c<br \/>\n[root&#064;haproxy ~]# ll \/var\/lib\/haproxy\/<br \/>\n\u603b\u7528\u91cf 0<br \/>\nsrw-rw&#8212;- 1 root root 0  1\u6708 23 15:41 haporxy2<br \/>\nsrw&#8212;&#8212;- 1 root root 0  1\u6708 23 15:41 haproxy1 <\/p>\n<h4 id=\"1.5%C2%A0%E5%A4%9A%E7%BA%BF%E7%A8%8B%E9%85%8D%E7%BD%AE\" style=\"background-color:transparent\">1.5\u00a0\u591a\u7ebf\u7a0b\u914d\u7f6e<\/h4>\n<p>\u6ce8\u610f&#xff1a;\u591a\u7ebf\u7a0b\u4e0e\u591a\u8fdb\u7a0b\u4e0d\u80fd\u540c\u65f6\u542f\u7528&#xff0c;\u9002\u7528\u4e8e\u5355\u8fdb\u7a0b\u4e0b\u63d0\u5347\u5e76\u53d1&#xff1a;<\/p>\n<h5 id=\"1.%C2%A0%E5%89%8D%E7%BD%AE%E6%A3%80%E6%9F%A5\">1.\u00a0\u524d\u7f6e\u68c0\u67e5<\/h5>\n<p>\u67e5\u770b\u9ed8\u8ba4\u7ebf\u7a0b\u6570&#xff1a;cat \/proc\/[\u8fdb\u7a0bID]\/status | grep Threads&#xff0c;\u9ed8\u8ba4\u5355\u7ebf\u7a0b\u3002<\/p>\n<p>#\u67e5\u770b\u5f53\u524dhaproxy\u7684\u8fdb\u7a0b\u4fe1\u606f<br \/>\n[root&#064;haproxy ~]# pstree -p | grep haproxy<br \/>\n           |-haproxy(31742)-&#043;-haproxy(31744)<br \/>\n           |                &#096;-haproxy(31745)<\/p>\n<p> #\u67e5\u770bhaproxy\u5b50\u8fdb\u7a0b\u7684\u7ebf\u7a0b\u4fe1\u606f<br \/>\n[root&#064;haproxy ~]# cat \/proc\/31744\/status  | grep Threads<br \/>\nThreads:        1 <\/p>\n<h5 id=\"2.%C2%A0%E5%90%AF%E7%94%A8%E5%A4%9A%E7%BA%BF%E7%A8%8B\">2.\u00a0\u542f\u7528\u591a\u7ebf\u7a0b<\/h5>\n<ul>\n<li>\n<p>\u6ce8\u91ca\u591a\u8fdb\u7a0b\u76f8\u5173\u914d\u7f6e&#xff0c;\u6dfb\u52a0nbthread 2&#xff1a;\u5355\u8fdb\u7a0b\u4e0b\u542f\u52a8 2 \u4e2a\u7ebf\u7a0b\u3002<\/p>\n<\/li>\n<li>\n<p>\u6062\u590d\u9ed8\u8ba4 stats \u5957\u63a5\u5b57&#xff1a;stats socket \/var\/lib\/haproxy\/stats\u3002<\/p>\n<\/li>\n<\/ul>\n<p>#\u542f\u7528\u591a\u7ebf\u7a0b<br \/>\n[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<\/p>\n<p>#nbproc     2<br \/>\n    #cpu-map    1 0<br \/>\n    #cpu-map    2 1<br \/>\n    nbthread    2<br \/>\n    # turn on stats unix socket<br \/>\n    stats socket \/var\/lib\/haproxy\/stats<br \/>\n    #stats socket \/var\/lib\/haproxy\/haproxy1  mode 600 level admin process 1<br \/>\n    #stats socket \/var\/lib\/haproxy\/haporxy2  mode 660 level admin process 1<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<h5 id=\"3.%C2%A0%E9%AA%8C%E8%AF%81%E6%95%88%E6%9E%9C\">3.\u00a0\u9a8c\u8bc1\u6548\u679c<\/h5>\n<ul>\n<li>\n<p>pstree -p | grep haproxy\u53ef\u770b\u5230\u8fdb\u7a0b\u4e0b\u7684\u7ebf\u7a0b\u5b50\u8fdb\u7a0b&#xff1b;<\/p>\n<\/li>\n<li>\n<p>cat \/proc\/[\u8fdb\u7a0bID]\/status | grep Threads\u663e\u793a\u7ebf\u7a0b\u6570\u4e3a 2\u3002<\/p>\n<\/li>\n<\/ul>\n<p>#\u6548\u679c<br \/>\n[root&#064;haproxy ~]# pstree -p | grep haproxy<br \/>\n           |-haproxy(31858)&#8212;haproxy(31860)&#8212;{haproxy}(31861)<\/p>\n<p>[root&#064;haproxy ~]# cat \/proc\/31860\/status  | grep Threads<br \/>\nThreads:        2 <\/p>\n<ul>\n<li>\n<p>nbproc&#xff1a;\u8fdb\u7a0b\u6570&#xff0c;\u5229\u7528\u591a CPU \u6838\u5fc3&#xff1b;<\/p>\n<\/li>\n<li>\n<p>nbthread&#xff1a;\u7ebf\u7a0b\u6570&#xff0c;\u5355\u8fdb\u7a0b\u5185\u63d0\u5347\u5e76\u53d1&#xff1b;<\/p>\n<\/li>\n<li>\n<p>cpu-map&#xff1a;\u8fdb\u7a0b CPU \u7ed1\u5b9a&#xff0c;\u4f18\u5316\u8d44\u6e90\u8c03\u5ea6&#xff1b;<\/p>\n<\/li>\n<li>\n<p>stats socket&#xff1a;\u8fdb\u7a0b\u7ba1\u7406\u5957\u63a5\u5b57&#xff0c;\u7528\u4e8e\u8c03\u8bd5 \/ \u76d1\u63a7&#xff1b;<\/p>\n<\/li>\n<li>\n<p>check\/inter\/fall\/rise&#xff1a;\u5065\u5eb7\u68c0\u67e5\u6838\u5fc3\u53c2\u6570&#xff0c;\u4fdd\u969c\u96c6\u7fa4\u53ef\u7528\u6027\u3002<\/p>\n<\/li>\n<\/ul>\n<h3 id=\"2.%C2%A0HAPORXY%20%E5%AE%9E%E9%AA%8C%E7%8E%AF%E5%A2%83%E8%AE%BE%E5%AE%9A\" style=\"background-color:transparent\">2.\u00a0HAPORXY \u5b9e\u9a8c\u73af\u5883\u8bbe\u5b9a<\/h3>\n<p>\u8fd9\u4e00\u90e8\u5206\u662f\u5173\u4e8eHAProxy \u5b9e\u9a8c\u73af\u5883\u642d\u5efa\u7684\u64cd\u4f5c\u6307\u5f15&#xff0c;\u8be6\u7ec6\u8bb0\u5f55\u4e86\u5b9e\u9a8c\u4e2d\u5404\u8282\u70b9\u7684\u7f51\u7edc\u914d\u7f6e\u3001\u670d\u52a1\u90e8\u7f72\u53ca\u57fa\u7840\u8fde\u901a\u6027\u9a8c\u8bc1\u6b65\u9aa4&#xff0c;\u6574\u4f53\u56f4\u7ed5\u6784\u5efa\u4e00\u4e2a\u5305\u542b HAProxy \u4ee3\u7406\u8282\u70b9\u548c\u4e24\u53f0 Web \u670d\u52a1\u5668\u8282\u70b9\u7684\u5b9e\u9a8c\u73af\u5883\u5c55\u5f00<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"491\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143247-69a1ab0f2555c.png\" width=\"1226\" \/><\/p>\n<ul>\n<li>\n<p>HAProxy \u4e3b\u673a&#xff08;\u4ee3\u7406\u8282\u70b9&#xff09;<\/p>\n<\/li>\n<li>\n<p>webserver1&#xff08;\u540e\u7aef Web \u670d\u52a1\u8282\u70b9 1&#xff09;<\/p>\n<\/li>\n<li>\n<p>webserver2&#xff08;\u540e\u7aef Web \u670d\u52a1\u8282\u70b9 2&#xff09;<\/p>\n<\/li>\n<\/ul>\n<h4 id=\"2.1%C2%A0HAProxy%20%E4%B8%BB%E6%9C%BA%E9%85%8D%E7%BD%AE\">2.1\u00a0HAProxy \u4e3b\u673a\u914d\u7f6e<\/h4>\n<p>\u8be5\u8282\u70b9\u662f\u5b9e\u9a8c\u7684\u6838\u5fc3\u4ee3\u7406\u8282\u70b9&#xff0c;\u91cd\u70b9\u5b8c\u6210\u53cc\u7f51\u5361\u7f51\u7edc\u914d\u7f6e\u548c\u5185\u6838\u8def\u7531\u8f6c\u53d1\u5f00\u542f&#xff08;\u4e3a\u540e\u7eed\u4ee3\u7406 \/ \u8f6c\u53d1\u505a\u57fa\u7840&#xff09;\u3002<\/p>\n<p>1.\u00a0\u7f51\u5361 eth0 \/\u00a0eth1 \u914d\u7f6e<\/p>\n<p>[root&#064;haproxy ~]# vmset.sh eth0 172.25.254.100 haproxy<br \/>\n[root&#064;haproxy ~]# vmset.sh eth1 192.168.0.100 haproxy norouter <\/p>\n<p>2.\u00a0\u5185\u6838\u8def\u7531\u8f6c\u53d1<\/p>\n<p>&#8211; \u5f00\u542f Linux \u5185\u6838\u7684 IPv4 \u8f6c\u53d1\u529f\u80fd&#xff08;\u6838\u5fc3\u914d\u7f6e&#xff09;&#xff1b;<\/p>\n<p>&#8211; \u4f5c\u7528&#xff1a;\u8ba9 HAProxy \u8282\u70b9\u5177\u5907\u8def\u7531 \/ \u8f6c\u53d1\u80fd\u529b&#xff0c;\u80fd\u5c06\u5916\u90e8\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u540e\u7aef Web \u670d\u52a1\u5668&#xff0c;\u540c\u65f6\u5c06\u54cd\u5e94\u56de\u4f20\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>#\u914d\u7f6e\u5185\u6838\u8def\u7531\u529f\u80fd<br \/>\n[root&#064;haproxy ~]# echo net.ipv4.ip_forward&#061;1 &gt; \/etc\/sysctl.conf<br \/>\n[root&#064;haproxy ~]# sysctl -p<br \/>\nnet.ipv4.ip_forward &#061; 1 <\/p>\n<h4 id=\"2.2%C2%A0webserver1%20%E8%8A%82%E7%82%B9%E9%85%8D%E7%BD%AE\">2.2\u00a0webserver1 \u8282\u70b9\u914d\u7f6e<\/h4>\n<p>\u8be5\u8282\u70b9\u662f\u540e\u7aef Web \u670d\u52a1\u8282\u70b9&#xff0c;\u6838\u5fc3\u662f\u914d\u7f6e\u7f51\u7edc &#043; \u90e8\u7f72 HTTP \u670d\u52a1\u3002<\/p>\n<p>1.\u00a0\u7f51\u7edc\u914d\u7f6e<\/p>\n<p>[root&#064;webserver1 ~]# vmset.sh eth0 192.168.0.10 webserver1 noroute <\/p>\n<p>2. HTTP \u670d\u52a1\u90e8\u7f72<\/p>\n<p># \u901a\u8fc7 DNF \u5305\u7ba1\u7406\u5668\u5b89\u88c5 Apache HTTP \u670d\u52a1&#xff08;httpd&#xff09;&#xff1b;<br \/>\n[root&#064;webserver1 ~]# dnf install httpd -y<br \/>\n# \u81ea\u5b9a\u4e49 Web \u9875\u9762\u5185\u5bb9&#xff08;\u7528\u4e8e\u533a\u5206\u4e0d\u540c Web \u8282\u70b9&#xff09;&#xff1b;<br \/>\nroot&#064;webserver1 ~]# echo webserver1 &#8211; 192.168.0.10 &gt; \/var\/www\/html\/index.html<br \/>\n# \u8bbe\u7f6e httpd \u5f00\u673a\u81ea\u542f&#xff0c;\u5e76\u7acb\u5373\u542f\u52a8\u670d\u52a1<br \/>\n[root&#064;webserver1 ~]# systemctl enable &#8211;now httpd<br \/>\nCreated symlink \/etc\/systemd\/system\/multi-user.target.wants\/httpd.service \u2192 \/usr\/lib\/systemd\/system\/httpd.service. <\/p>\n<h4 id=\"2.3%C2%A0%C2%A0webserver2%20%E8%8A%82%E7%82%B9%E9%85%8D%E7%BD%AE\">2.3\u00a0\u00a0webserver2 \u8282\u70b9\u914d\u7f6e<\/h4>\n<p>\u548c webserver1 \u914d\u7f6e\u903b\u8f91\u5b8c\u5168\u4e00\u81f4&#xff0c;\u4ec5 IP \u548c\u6807\u8bc6\u4fe1\u606f\u4e0d\u540c&#xff0c;\u662f\u5197\u4f59\u7684\u540e\u7aef Web \u8282\u70b9&#xff08;\u7528\u4e8e\u540e\u7eed HAProxy \u8d1f\u8f7d\u5747\u8861\u6d4b\u8bd5&#xff09;\u3002<\/p>\n<p>1.\u00a0\u7f51\u7edc\u914d\u7f6e<\/p>\n<p>[root&#064;webserver2 ~]# vmset.sh eth0 192.168.0.20 webserver2 noroute <\/p>\n<p>2.\u00a0HTTP \u670d\u52a1\u90e8\u7f72<\/p>\n<p>[root&#064;webserver2 ~]# dnf install httpd -y<br \/>\n[root&#064;webserver2 ~]#  echo webserver2 &#8211; 192.168.0.20 &gt; \/var\/www\/html\/index.html<br \/>\n[root&#064;webserver2 ~]# systemctl enable &#8211;now httpd<br \/>\nCreated symlink \/etc\/systemd\/system\/multi-user.target.wants\/httpd.service \u2192 \/usr\/lib\/systemd\/system\/httpd.service. <\/p>\n<h4 id=\"2.4%C2%A0%E7%8E%AF%E5%A2%83%E9%AA%8C%E8%AF%81%E6%AD%A5%E9%AA%A4\">2.4\u00a0\u73af\u5883\u9a8c\u8bc1\u6b65\u9aa4<\/h4>\n<p>\u5728 HAProxy \u4e3b\u673a\u4e0a\u901a\u8fc7curl\u547d\u4ee4\u9a8c\u8bc1\u4e0e\u540e\u7aef Web \u8282\u70b9\u7684\u8fde\u901a\u6027<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143247-69a1ab0f68143.png\" \/><\/p>\n<h2 id=\"%E5%9B%9B%E3%80%81%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E4%B8%8E%E4%BB%A3%E7%90%86%E8%83%BD%E5%8A%9B\" style=\"background-color:transparent\">\u56db\u3001\u8d1f\u8f7d\u5747\u8861\u4e0e\u4ee3\u7406\u80fd\u529b<\/h2>\n<h3 id=\"1.%C2%A0HAProxy%20%E5%9B%9B%E5%B1%82%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1\">1.\u00a0HAProxy \u56db\u5c42\u8d1f\u8f7d\u5747\u8861<\/h3>\n<h4 id=\"1.1%C2%A0%E4%BB%80%E4%B9%88%E6%98%AF%20HAProxy%20%E5%9B%9B%E5%B1%82%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%EF%BC%9F\">1.1\u00a0\u4ec0\u4e48\u662f HAProxy \u56db\u5c42\u8d1f\u8f7d\u5747\u8861&#xff1f;<\/h4>\n<p>\u9996\u5148\u8981\u660e\u786e \u201c\u56db\u5c42\u201d \u6307\u7684\u662f<span style=\"color:#fe2c24\">TCP\/IP \u534f\u8bae\u6808\u7684\u4f20\u8f93\u5c42&#xff08;\u7b2c\u56db\u5c42&#xff09;<\/span>&#xff0c;\u5bf9\u5e94\u7684\u6838\u5fc3\u534f\u8bae\u662f TCP\/UDP\u3002HAProxy \u56db\u5c42\u8d1f\u8f7d\u5747\u8861&#xff0c;\u7b80\u5355\u8bf4\u5c31\u662f&#xff1a;<span style=\"background-color:#ffd900\">HAProxy \u53ea\u5173\u6ce8\u7f51\u7edc\u8bf7\u6c42\u7684 \u201cIP \u5730\u5740 &#043; \u7aef\u53e3\u53f7\u201d&#xff0c;\u4e0d\u89e3\u6790\u8bf7\u6c42\u7684\u5185\u5bb9&#xff08;\u6bd4\u5982 HTTP \u62a5\u6587\u3001SQL \u8bed\u53e5&#xff09;&#xff0c;\u7eaf\u7cb9\u628a TCP\/UDP \u6570\u636e\u5305\u8f6c\u53d1\u5230\u540e\u7aef\u670d\u52a1\u5668<\/span><span style=\"background-color:#ffd900\">\u3002<\/span><\/p>\n<p>\u53ef\u4ee5\u505a\u4e2a\u5f62\u8c61\u6bd4\u55bb&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u56db\u5c42\u8d1f\u8f7d &#061; \u5feb\u9012\u5206\u62e3\u5458&#xff1a;\u53ea\u770b\u5305\u88f9\u4e0a\u7684 \u201c\u6536\u4ef6\u5730\u5740&#xff08;IP&#xff09;&#043; \u95e8\u724c\u53f7&#xff08;\u7aef\u53e3&#xff09;\u201d&#xff0c;\u76f4\u63a5\u628a\u5305\u88f9\u9001\u5230\u5bf9\u5e94\u4ed3\u5e93&#xff0c;\u4e0d\u7ba1\u5305\u88f9\u91cc\u88c5\u7684\u662f\u6587\u4ef6\u8fd8\u662f\u96f6\u98df&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u6bd4\u4e03\u5c42\u8d1f\u8f7d&#xff08;HTTP&#xff09;&#xff1a;\u76f8\u5f53\u4e8e\u5feb\u9012\u5458\u4e0d\u4ec5\u770b\u5730\u5740&#xff0c;\u8fd8\u4f1a\u6253\u5f00\u5305\u88f9\u770b\u5185\u5bb9&#xff0c;\u518d\u51b3\u5b9a\u9001\u5230\u54ea\u4e2a\u8d27\u67b6\u3002<\/p>\n<\/li>\n<\/ul>\n<h4 id=\"1.2%C2%A0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%EF%BC%9A%E6%95%B0%E6%8D%AE%E5%8C%85%E6%98%AF%E6%80%8E%E4%B9%88%E8%BD%AC%E5%8F%91%E7%9A%84%EF%BC%9F\">1.2\u00a0\u5de5\u4f5c\u539f\u7406&#xff1a;\u6570\u636e\u5305\u662f\u600e\u4e48\u8f6c\u53d1\u7684&#xff1f;<\/h4>\n<li>\n<p>\u76d1\u542c\u7aef\u53e3&#xff1a;HAProxy \u5728\u6307\u5b9a\u7aef\u53e3&#xff08;\u6bd4\u5982 6663&#xff09;\u76d1\u542c\u5ba2\u6237\u7aef\u7684 TCP\/UDP \u8fde\u63a5\u8bf7\u6c42&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5efa\u7acb\u8fde\u63a5&#xff1a;\u5ba2\u6237\u7aef\u5148\u548c HAProxy \u5efa\u7acb TCP \u8fde\u63a5&#xff08;\u4e09\u6b21\u63e1\u624b&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u9009\u62e9\u540e\u7aef\u8282\u70b9&#xff1a;HAProxy \u6839\u636e\u9884\u8bbe\u7684\u8d1f\u8f7d\u5747\u8861\u7b97\u6cd5&#xff08;\u5982\u8f6e\u8be2&#xff09;&#xff0c;\u4ece\u540e\u7aef\u670d\u52a1\u5668\u6c60\u4e2d\u9009\u4e00\u4e2a\u8282\u70b9&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8f6c\u53d1\u6570\u636e\u5305&#xff1a;HAProxy \u548c\u9009\u4e2d\u7684\u540e\u7aef\u8282\u70b9\u5efa\u7acb TCP \u8fde\u63a5&#xff0c;\u628a\u5ba2\u6237\u7aef\u7684\u6570\u636e\u5305\u539f\u5c01\u4e0d\u52a8\u8f6c\u53d1\u8fc7\u53bb&#xff0c;\u540c\u65f6\u628a\u540e\u7aef\u8282\u70b9\u7684\u54cd\u5e94\u6570\u636e\u5305\u56de\u4f20\u7ed9\u5ba2\u6237\u7aef&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u7ef4\u6301\u8fde\u63a5&#xff1a;\u6574\u4e2a\u8fc7\u7a0b\u4e2d&#xff0c;HAProxy \u76f8\u5f53\u4e8e \u201c\u4e2d\u95f4\u4eba\u201d&#xff0c;\u53ea\u505a\u6570\u636e\u5305\u7684\u8f6c\u53d1&#xff0c;\u4e0d\u4fee\u6539\u3001\u4e0d\u89e3\u6790\u5185\u5bb9\u3002<\/p>\n<\/li>\n<p>\u6838\u5fc3\u7279\u70b9&#xff1a;<span style=\"color:#fe2c24\">\u901f\u5ea6\u5feb\u3001\u901a\u7528\u6027\u5f3a<\/span>&#xff08;\u56e0\u4e3a\u53ea\u5904\u7406\u4f20\u8f93\u5c42&#xff0c;\u4e0d\u505a\u5e94\u7528\u5c42\u89e3\u6790&#xff0c;\u6027\u80fd\u635f\u8017\u6781\u4f4e&#xff09;\u3002<\/p>\n<h4 id=\"1.3%C2%A0%E5%9F%BA%E4%BA%8E%20HAProxy%20%E5%AE%9E%E7%8E%B0%20MariaDB%20%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E5%9B%9B%E5%B1%82%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1\">1.3\u00a0\u57fa\u4e8e HAProxy \u5b9e\u73b0 MariaDB \u6570\u636e\u5e93\u7684\u56db\u5c42\u8d1f\u8f7d\u5747\u8861<\/h4>\n<p>HAProxy \u662f\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684 TCP\/HTTP \u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668&#xff0c;\u652f\u6301\u56db\u5c42&#xff08;TCP&#xff09;\u548c\u4e03\u5c42&#xff08;HTTP&#xff09;\u8d1f\u8f7d\u5747\u8861\u3002\u8fd9\u90e8\u5206\u805a\u7126\u56db\u5c42\u8d1f\u8f7d\u5747\u8861&#xff08;\u57fa\u4e8e TCP \u534f\u8bae&#xff09;&#xff0c;\u9488\u5bf9 MariaDB \u6570\u636e\u5e93\u96c6\u7fa4\u5b9e\u73b0\u8bf7\u6c42\u5206\u53d1&#xff0c;\u5e76\u9a8c\u8bc1\u6545\u969c\u81ea\u52a8\u5207\u6362\u80fd\u529b\u3002<\/p>\n<h5 id=\"1.3.1%20%E6%A8%A1%E5%9D%97%201%EF%BC%9A%E7%8E%AF%E5%A2%83%E8%AE%BE%E5%AE%9A%EF%BC%88MariaDB%20%E6%95%B0%E6%8D%AE%E5%BA%93%E9%83%A8%E7%BD%B2%EF%BC%89\">1.3.1 \u6a21\u5757 1&#xff1a;\u73af\u5883\u8bbe\u5b9a&#xff08;MariaDB \u6570\u636e\u5e93\u90e8\u7f72&#xff09;<\/h5>\n<p>1. \u6570\u636e\u5e93\u5b89\u88c5\u4e0e\u914d\u7f6e<\/p>\n<ul>\n<li>\n<p>server_id&#061;10&#xff1a;\u6570\u636e\u5e93\u5b9e\u4f8b\u7684\u552f\u4e00\u6807\u8bc6&#xff08;\u53e6\u4e00\u53f0\u6570\u636e\u5e93\u670d\u52a1\u5668\u9700\u8bbe\u4e3a20&#xff09;&#xff0c;\u7528\u4e8e\u533a\u5206\u96c6\u7fa4\u4e2d\u7684\u4e0d\u540c\u8282\u70b9\u3002<\/p>\n<\/li>\n<li>\n<p>\u5176\u4ed6\u9879&#xff08;datadir\/socket\/log-error \u7b49&#xff09;&#xff1a;\u5b9a\u4e49\u6570\u636e\u5e93\u6570\u636e\u76ee\u5f55\u3001\u5957\u63a5\u5b57\u6587\u4ef6\u3001\u65e5\u5fd7\u8def\u5f84\u7b49\u57fa\u7840\u53c2\u6570\u3002<\/p>\n<\/li>\n<\/ul>\n<p># \u5b89\u88c5mariadb\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef<br \/>\n[root&#064;webserver1&#043;2 ~]# dnf install mariadb-server mariadb  -y<br \/>\n# \u7f16\u8f91\u6570\u636e\u5e93\u914d\u7f6e\u6587\u4ef6<br \/>\n[root&#064;webserver1&#043;1 ~]# vim \/etc\/my.cnf.d\/mariadb-server.cnf<br \/>\n[mysqld]<br \/>\nserver_id&#061;10#\u8bbe\u5b9a\u6570\u636e\u5e93\u6240\u5728\u4e3b\u673a\u7684id\u6807\u8bc6&#xff0c;\u572820\u4e0a\u8bbe\u5b9aid\u4e3a20<br \/>\ndatadir&#061;\/var\/lib\/mysql<br \/>\nsocket&#061;\/var\/lib\/mysql\/mysql.sock<br \/>\nlog-error&#061;\/var\/log\/mariadb\/mariadb.log<br \/>\npid-file&#061;\/run\/mariadb\/mariadb.pid <\/p>\n<p>2. \u8fdc\u7a0b\u767b\u5f55\u7528\u6237\u6388\u6743<\/p>\n<p>\u4e3a\u4e86\u8ba9 HAProxy \u548c\u5ba2\u6237\u7aef\u80fd\u8fdc\u7a0b\u8bbf\u95ee\u6570\u636e\u5e93&#xff0c;\u521b\u5efa\u5e76\u6388\u6743\u901a\u7528\u7528\u6237&#xff1a;<\/p>\n<p># \u521b\u5efa\u7528\u6237lee&#xff0c;\u5141\u8bb8\u6240\u6709\u4e3b\u673a&#xff08;%&#xff09;\u8bbf\u95ee&#xff0c;\u5bc6\u7801\u4e3alee<br \/>\nMariaDB [(none)]&gt; CREATE USER &#039;lee&#039;&#064;&#039;%&#039; identified by &#039;lee&#039;;<br \/>\n# \u6388\u4e88\u8be5\u7528\u6237\u6240\u6709\u5e93\u3001\u6240\u6709\u8868\u7684\u5168\u90e8\u6743\u9650<br \/>\nMariaDB [(none)]&gt; GRANT ALL ON *.* TO &#039;lee&#039;&#064;&#039;%&#039;; <\/p>\n<p>3. \u8fde\u901a\u6027\u6d4b\u8bd5<\/p>\n<p>\u76ee\u7684&#xff1a;\u9a8c\u8bc1\u6570\u636e\u5e93\u8282\u70b9\u53ef\u88ab\u8fdc\u7a0b\u8bbf\u95ee&#xff0c;\u4e3a\u540e\u7eed HAProxy \u8f6c\u53d1\u505a\u57fa\u7840\u3002<\/p>\n<p>[root&#064;webserver2 ~]# mysql -ulee -plee -h 192.168.0.20   #\u6d4b\u8bd510\u65f6\u4fee\u6539ip\u5373\u53ef <\/p>\n<h5 id=\"1.3.2%20%E6%A8%A1%E5%9D%97%202%EF%BC%9A%E5%9B%9B%E5%B1%82%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E6%A0%B8%E5%BF%83%E9%85%8D%E7%BD%AE\">1.3.2 \u6a21\u5757 2&#xff1a;\u56db\u5c42\u8d1f\u8f7d\u5747\u8861\u6838\u5fc3\u914d\u7f6e<\/h5>\n<p>1. HAProxy \u914d\u7f6e\u6587\u4ef6\u4fee\u6539<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten mariadbcluster<br \/>\n    bind        *:6663<br \/>\n    mode        tcp<br \/>\n    balance     roundrobin<br \/>\n    server haha 192.168.0.10:3306  check inter 3s fall 3 rise 5 weight 1<br \/>\n    server hehe 192.168.0.20:3306  check inter 3s fall 3 rise 5 weight 1 <\/p>\n<p>\u5173\u952e\u53c2\u6570\u89e3\u91ca&#xff1a;<\/p>\n<ul>\n<li>mode tcp&#xff1a;\u56db\u5c42\u8d1f\u8f7d\u6838\u5fc3&#xff0c;HAProxy \u4ec5\u8f6c\u53d1 TCP \u6570\u636e\u5305&#xff0c;\u4e0d\u89e3\u6790\u5e94\u7528\u5c42&#xff08;\u5982 SQL&#xff09;&#xff1b;<\/li>\n<li>balance roundrobin&#xff1a;\u8f6e\u8be2\u7b97\u6cd5&#xff0c;\u8bf7\u6c42\u4f1a\u4f9d\u6b21\u5206\u53d1\u5230 192.168.0.10 \u548c 20&#xff1b;<\/li>\n<li>check inter 3s&#xff1a;\u6bcf 3 \u79d2\u68c0\u6d4b\u540e\u7aef\u670d\u52a1\u5668\u5065\u5eb7\u72b6\u6001&#xff1b;<\/li>\n<li>fall 3&#xff1a;\u8fde\u7eed 3 \u6b21\u68c0\u6d4b\u5931\u8d25&#xff0c;\u6807\u8bb0\u670d\u52a1\u5668\u4e0d\u53ef\u7528&#xff1b;<\/li>\n<li>rise 5&#xff1a;\u8fde\u7eed 5 \u6b21\u68c0\u6d4b\u6210\u529f&#xff0c;\u6062\u590d\u670d\u52a1\u5668\u4e3a\u53ef\u7528\u72b6\u6001&#xff1b;<\/li>\n<li>weight 1&#xff1a;\u6743\u91cd&#xff08;\u9ed8\u8ba4 1&#xff0c;\u6570\u503c\u8d8a\u9ad8\u88ab\u5206\u914d\u8bf7\u6c42\u8d8a\u591a&#xff09;\u3002<\/li>\n<\/ul>\n<p>2. \u91cd\u542f HAProxy \u5e76\u9a8c\u8bc1\u7aef\u53e3<\/p>\n<p>\u8f93\u51fa\u4e2d0.0.0.0:6663 LISTEN\u8868\u793a\u914d\u7f6e\u751f\u6548&#xff0c;HAProxy \u5df2\u76d1\u542c 6663 \u7aef\u53e3\u3002<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service<\/p>\n<p>#\u68c0\u6d4b\u7aef\u53e3<br \/>\n[root&#064;haproxy ~]# netstat -antlupe  | grep haproxy<br \/>\ntcp        0      0 0.0.0.0:6663            0.0.0.0:*               LISTEN      0          44430      2136\/haproxy<br \/>\ntcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          44429      2136\/haproxy<br \/>\ntcp        0      0 0.0.0.0:4321            0.0.0.0:*               LISTEN      0          44431      2136\/haproxy <\/p>\n<p>3. \u8d1f\u8f7d\u5747\u8861\u6548\u679c\u6d4b\u8bd5<\/p>\n<p>\u5ba2\u6237\u7aef\u8fdc\u7a0b\u8fde\u63a5 HAProxy \u7684 6663 \u7aef\u53e3&#xff08;\u800c\u975e\u76f4\u63a5\u8fde\u6570\u636e\u5e93 3306&#xff09;<\/p>\n<p>#\u6d4b\u8bd5&#xff1a;<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 mysql -ulee -plee -h172.25.254.100 -P 6663<br \/>\nWelcome to the MariaDB monitor.  Commands end with ; or \\\\g.<br \/>\nYour MariaDB connection id is 7<br \/>\nServer version: 10.5.27-MariaDB MariaDB Server<\/p>\n<p>Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.<\/p>\n<p>Type &#039;help;&#039; or &#039;\\\\h&#039; for help. Type &#039;\\\\c&#039; to clear the current input statement.<\/p>\n<p>MariaDB [(none)]&gt; SELECT &#064;&#064;server_id;<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n| &#064;&#064;server_id |<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n|          20 |<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n1 row in set (0.00 sec)<\/p>\n<p>[Administrator.DESKTOP-VJ307M3] \u27a4 mysql -ulee -plee -h172.25.254.100 -P 6663<br \/>\nWelcome to the MariaDB monitor.  Commands end with ; or \\\\g.<br \/>\nYour MariaDB connection id is 8<br \/>\nServer version: 10.5.27-MariaDB MariaDB Server<\/p>\n<p>Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.<\/p>\n<p>Type &#039;help;&#039; or &#039;\\\\h&#039; for help. Type &#039;\\\\c&#039; to clear the current input statement.<\/p>\n<p>MariaDB [(none)]&gt; SELECT &#064;&#064;server_id;<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n| &#064;&#064;server_id |<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n|          10 |<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n1 row in set (0.00 sec) <\/p>\n<p>\u6d4b\u8bd5\u7ed3\u679c&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7b2c\u4e00\u6b21\u67e5\u8be2\u8fd4\u56deserver_id&#061;20&#xff08;\u8fde\u63a5\u5230 192.168.0.20&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u7b2c\u4e8c\u6b21\u67e5\u8be2\u8fd4\u56deserver_id&#061;10&#xff08;\u8fde\u63a5\u5230 192.168.0.10&#xff09;&#xff1b;\u7ed3\u8bba&#xff1a;\u8f6e\u8be2\u7b97\u6cd5\u751f\u6548&#xff0c;\u8bf7\u6c42\u88ab\u4f9d\u6b21\u5206\u53d1\u5230\u4e24\u4e2a\u6570\u636e\u5e93\u8282\u70b9\u3002<\/p>\n<\/li>\n<\/ul>\n<h5 id=\"1.3.3%C2%A0%E6%A8%A1%E5%9D%97%203%EF%BC%9Abackup%20%E5%8F%82%E6%95%B0%EF%BC%88%E5%A4%87%E4%BB%BD%E8%8A%82%E7%82%B9%EF%BC%89%E9%AA%8C%E8%AF%81\">1.3.3\u00a0\u6a21\u5757 3&#xff1a;backup \u53c2\u6570&#xff08;\u5907\u4efd\u8282\u70b9&#xff09;\u9a8c\u8bc1<\/h5>\n<p>1. \u914d\u7f6e\u4fee\u6539&#xff08;\u6dfb\u52a0 backup \u53c2\u6570&#xff09;<\/p>\n<p>backup \u53c2\u6570\u4f5c\u7528&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6807\u8bb0\u8be5\u8282\u70b9\u4e3a \u201c\u5907\u4efd\u8282\u70b9\u201d&#xff0c;\u4ec5\u5f53\u6240\u6709\u975e\u5907\u4efd\u8282\u70b9&#xff08;haha&#xff09;\u4e0d\u53ef\u7528\u65f6&#xff0c;\u624d\u4f1a\u8f6c\u53d1\u8bf7\u6c42\u5230\u5907\u4efd\u8282\u70b9&#xff08;hehe&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6b63\u5e38\u60c5\u51b5\u4e0b&#xff0c;\u6240\u6709\u8bf7\u6c42\u90fd\u4f1a\u5206\u53d1\u5230\u975e\u5907\u4efd\u8282\u70b9&#xff08;192.168.0.10&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten mariadbcluster<br \/>\n    bind        *:6663<br \/>\n    mode        tcp<br \/>\n    balance     roundrobin<br \/>\n    server haha 192.168.0.10:3306  check inter 3s fall 3 rise 5 weight 1<br \/>\n    server hehe 192.168.0.20:3306  check inter 3s fall 3 rise 5 weight 1 backup<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<p>2. \u6b63\u5e38\u573a\u666f\u6d4b\u8bd5<\/p>\n<p>\u591a\u6b21\u8fde\u63a5 HAProxy \u7684 6663 \u7aef\u53e3&#xff0c;\u6267\u884cSELECT &#064;&#064;server_id;&#xff0c;\u7ed3\u679c\u5747\u4e3a10&#xff08;\u4ec5\u8bbf\u95ee\u4e3b\u8282\u70b9 192.168.0.10&#xff09;&#xff0c;\u9a8c\u8bc1\u5907\u4efd\u8282\u70b9\u672a\u88ab\u4f7f\u7528\u3002<\/p>\n<p>#\u6d4b\u8bd5<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 mysql -ulee -plee -h172.25.254.100 -P 6663<br \/>\nWelcome to the MariaDB monitor.  Commands end with ; or \\\\g.<br \/>\nYour MariaDB connection id is 4<br \/>\nServer version: 10.5.27-MariaDB MariaDB Server<\/p>\n<p>Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.<\/p>\n<p>Type &#039;help;&#039; or &#039;\\\\h&#039; for help. Type &#039;\\\\c&#039; to clear the current input statement.<\/p>\n<p>MariaDB [(none)]&gt; SELECT &#064;&#064;server_id;<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n| &#064;&#064;server_id |<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n|          10 |<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n1 row in set (0.00 sec)<\/p>\n<p>MariaDB [(none)]&gt; quit<br \/>\nBye<\/p>\n<p>[Administrator.DESKTOP-VJ307M3] \u27a4 mysql -ulee -plee -h172.25.254.100 -P 6663<br \/>\nWelcome to the MariaDB monitor.  Commands end with ; or \\\\g.<br \/>\nYour MariaDB connection id is 4<br \/>\nServer version: 10.5.27-MariaDB MariaDB Server<\/p>\n<p>Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.<\/p>\n<p>Type &#039;help;&#039; or &#039;\\\\h&#039; for help. Type &#039;\\\\c&#039; to clear the current input statement.<\/p>\n<p>MariaDB [(none)]&gt; SELECT &#064;&#064;server_id;<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n| &#064;&#064;server_id |<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n|          10 |<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n1 row in set (0.00 sec) <\/p>\n<p>3. \u6545\u969c\u8f6c\u79fb\u6d4b\u8bd5<\/p>\n<p>\u6b65\u9aa4 1&#xff1a;\u5173\u95ed\u4e3b\u8282\u70b9\u6570\u636e\u5e93<\/p>\n<p>#\u5173\u95ed10\u7684mariadb\u5e76\u7b49\u5f851\u5206\u949f<br \/>\n[root&#064;webserver1 ~]# systemctl stop mariadb<\/p>\n<p>\u6b65\u9aa4 2&#xff1a;\u7acb\u5373\u6d4b\u8bd5\u8fde\u63a5&#xff08;\u77ed\u6682\u5931\u8d25&#xff09;<\/p>\n<p>[Administrator.DESKTOP-VJ307M3] \u27a4 mysql -ulee -plee -h172.25.254.100 -P 6663<br \/>\nERROR 2013 (HY000): Lost connection to MySQL server at &#039;reading initial communication packet&#039;, system error: 1 &#034;Operation not permitted&#034;    #\u6807\u8bc6haproxy \u6ca1\u6709\u5b8c\u6210\u6545\u969c\u8f6c\u6362&#xff0c;\u9700\u8981\u7b49\u5f85 <\/p>\n<p>\u539f\u56e0&#xff1a;HAProxy \u7684\u5065\u5eb7\u68c0\u67e5\u9700\u8981\u65f6\u95f4&#xff08;inter 3s&#xff0c;fall 3 \u2192 \u7ea6 9 \u79d2&#xff09;&#xff0c;\u5c1a\u672a\u68c0\u6d4b\u5230\u4e3b\u8282\u70b9\u6545\u969c&#xff0c;\u4ecd\u5c1d\u8bd5\u8f6c\u53d1\u5230 10 \u8282\u70b9&#xff0c;\u56e0\u6b64\u62a5\u9519\u3002<\/p>\n<p>\u6b65\u9aa4 3&#xff1a;\u7b49\u5f85\u540e\u91cd\u8bd5&#xff08;\u6545\u969c\u5207\u6362\u6210\u529f&#xff09;<\/p>\n<p>\u7b49\u5f85\u5065\u5eb7\u68c0\u67e5\u5468\u671f\u540e\u518d\u6b21\u8fde\u63a5&#xff0c;\u6267\u884cSELECT &#064;&#064;server_id;&#xff0c;\u7ed3\u679c\u4e3a20&#xff08;\u8bf7\u6c42\u8f6c\u53d1\u5230\u5907\u4efd\u8282\u70b9 192.168.0.20&#xff09;&#xff0c;\u9a8c\u8bc1\u6545\u969c\u8f6c\u79fb\u751f\u6548\u3002<\/p>\n<p>[Administrator.DESKTOP-VJ307M3] \u27a4 mysql -ulee -plee -h172.25.254.100 -P 6663<br \/>\nWelcome to the MariaDB monitor.  Commands end with ; or \\\\g.<br \/>\nYour MariaDB connection id is 11<br \/>\nServer version: 10.5.27-MariaDB MariaDB Server<\/p>\n<p>Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.<\/p>\n<p>Type &#039;help;&#039; or &#039;\\\\h&#039; for help. Type &#039;\\\\c&#039; to clear the current input statement.<\/p>\n<p>MariaDB [(none)]&gt; SELECT &#064;&#064;server_id;<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n| &#064;&#064;server_id |<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n|          20 |<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n1 row in set (0.00 sec) <\/p>\n<p>4. \u6545\u969c\u6062\u590d\u6d4b\u8bd5<\/p>\n<p>\u5728192.168.0.10\u8282\u70b9\u91cd\u542fmariadb<\/p>\n<p>#\u8fd8\u539f\u6545\u969c\u4e3b\u673a\u7b49\u5f85\u7247\u523b<br \/>\n[root&#064;webserver1 ~]# systemctl start mariadb <\/p>\n<p>\u7b49\u5f85\u5065\u5eb7\u68c0\u67e5\u5b8c\u6210\u540e&#xff0c;\u518d\u6b21\u8fde\u63a5 HAProxy&#xff0c;server_id\u6062\u590d\u4e3a10&#xff0c;\u9a8c\u8bc1\u4e3b\u8282\u70b9\u6062\u590d\u540e\u8bf7\u6c42\u91cd\u65b0\u5207\u56de\u3002<\/p>\n<p>[Administrator.DESKTOP-VJ307M3] \u27a4 mysql -ulee -plee -h172.25.254.100 -P 6663<br \/>\nWelcome to the MariaDB monitor.  Commands end with ; or \\\\g.<br \/>\nYour MariaDB connection id is 4<br \/>\nServer version: 10.5.27-MariaDB MariaDB Server<\/p>\n<p>Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.<\/p>\n<p>Type &#039;help;&#039; or &#039;\\\\h&#039; for help. Type &#039;\\\\c&#039; to clear the current input statement.<\/p>\n<p>MariaDB [(none)]&gt; SELECT &#064;&#064;server_id;<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n| &#064;&#064;server_id |<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n|          10 |<br \/>\n&#043;&#8212;&#8212;&#8212;&#8212;-&#043;<br \/>\n1 row in set (0.00 sec) <\/p>\n<h5 id=\"1.3.4%20%E6%A0%B8%E5%BF%83%E6%80%BB%E7%BB%93\">1.3.4 \u6838\u5fc3\u603b\u7ed3<\/h5>\n<li>\n<p>\u56db\u5c42\u8d1f\u8f7d\u6838\u5fc3&#xff1a;HAProxy \u901a\u8fc7 TCP \u5c42\u8f6c\u53d1&#xff0c;\u9002\u914d\u6570\u636e\u5e93\u7b49\u57fa\u4e8e TCP \u7684\u670d\u52a1&#xff0c;\u4e0d\u611f\u77e5\u5e94\u7528\u5c42\u5185\u5bb9&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8f6e\u8be2\u7b97\u6cd5&#xff1a;\u9ed8\u8ba4\u5747\u5300\u5206\u53d1\u8bf7\u6c42&#xff0c;\u9002\u5408\u8282\u70b9\u6027\u80fd\u76f8\u8fd1\u7684\u96c6\u7fa4&#xff1b;<\/p>\n<\/li>\n<li>\n<p>backup \u53c2\u6570&#xff1a;\u5b9e\u73b0 \u201c\u4e3b\u5907\u6a21\u5f0f\u201d&#xff0c;\u63d0\u5347\u670d\u52a1\u53ef\u7528\u6027&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5065\u5eb7\u68c0\u67e5&#xff1a;check inter\/fall\/rise\u53c2\u6570\u63a7\u5236\u6545\u969c\u68c0\u6d4b\u548c\u6062\u590d\u7684\u7075\u654f\u5ea6&#xff0c;\u9700\u6839\u636e\u4e1a\u52a1\u8c03\u6574&#xff08;\u5982\u7f29\u77ed inter \u52a0\u5feb\u68c0\u6d4b&#xff0c;\u4f46\u589e\u52a0\u5f00\u9500&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u7aef\u53e3\u8bbe\u8ba1&#xff1a;HAProxy \u76d1\u542c 6663 \u7aef\u53e3&#xff0c;\u907f\u514d\u4e0e\u6570\u636e\u5e93\u9ed8\u8ba4 3306 \u7aef\u53e3\u51b2\u7a81&#xff0c;\u540c\u65f6\u5b9e\u73b0 \u201c\u7edf\u4e00\u5165\u53e3\u201d&#xff08;\u5ba2\u6237\u7aef\u53ea\u9700\u8fde HAProxy&#xff0c;\u65e0\u9700\u611f\u77e5\u540e\u7aef\u8282\u70b9&#xff09;\u3002<\/p>\n<\/li>\n<h3 id=\"2.%C2%A0HAProxy%20%E4%B8%83%E5%B1%82%E4%BB%A3%E7%90%86\" style=\"background-color:transparent\">2.\u00a0HAProxy \u4e03\u5c42\u4ee3\u7406<\/h3>\n<h4 id=\"2.1%20%E4%BB%80%E4%B9%88%E6%98%AF%20HAProxy%20%E4%B8%83%E5%B1%82%E4%BB%A3%E7%90%86%EF%BC%9F\">2.1 \u4ec0\u4e48\u662f HAProxy \u4e03\u5c42\u4ee3\u7406&#xff1f;<\/h4>\n<p>\u201c\u4e03\u5c42\u201d \u6307\u7684\u662f<span style=\"color:#fe2c24\">TCP\/IP \u534f\u8bae\u6808\u7684\u5e94\u7528\u5c42&#xff08;\u7b2c\u4e03\u5c42&#xff09;<\/span>&#xff0c;\u6700\u5178\u578b\u7684\u5c31\u662f HTTP\/HTTPS \u534f\u8bae\u3002<\/p>\n<p>HAProxy \u4e03\u5c42\u4ee3\u7406&#xff0c;\u7b80\u5355\u8bf4\u5c31\u662f&#xff1a;<span style=\"background-color:#ffd900\">HAProxy \u4e0d\u4ec5\u4f1a\u8bc6\u522b\u8bf7\u6c42\u7684 IP &#043; \u7aef\u53e3&#xff0c;\u8fd8\u4f1a\u6df1\u5ea6\u89e3\u6790\u5e94\u7528\u5c42\u7684\u5185\u5bb9&#xff08;\u6bd4\u5982 HTTP \u8bf7\u6c42\u5934\u3001URL\u3001Cookie\u3001\u57df\u540d&#xff09;&#xff0c;\u518d\u6839\u636e\u8fd9\u4e9b\u5185\u5bb9\u505a\u66f4\u667a\u80fd\u7684\u8f6c\u53d1 \/ \u5904\u7406<\/span><span style=\"background-color:#ffd900\">\u3002<\/span><\/p>\n<p>\u7ee7\u7eed\u7528\u5feb\u9012\u7684\u6bd4\u55bb&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4e03\u5c42\u4ee3\u7406 &#061; \u667a\u80fd\u5206\u62e3\u5458&#xff1a;\u4e0d\u4ec5\u770b\u6536\u4ef6\u5730\u5740&#xff08;IP &#043; \u7aef\u53e3&#xff09;&#xff0c;\u8fd8\u4f1a\u6253\u5f00\u5305\u88f9\u770b\u8be6\u60c5&#xff08;\u6bd4\u5982\u662f \u201c\u7535\u5546\u8ba2\u5355\u201d \u8fd8\u662f \u201c\u7269\u6d41\u5355\u636e\u201d&#xff09;&#xff0c;\u518d\u7cbe\u51c6\u9001\u5230\u5bf9\u5e94\u8d27\u67b6&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u6bd4\u56db\u5c42\u8d1f\u8f7d&#xff1a;\u53ea\u770b\u5730\u5740&#xff0c;\u4e0d\u7ba1\u5305\u88f9\u5185\u5bb9&#xff0c;\u7eaf\u8f6c\u53d1\u3002<\/p>\n<\/li>\n<\/ul>\n<h4 id=\"2.2%C2%A0%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86\">2.2\u00a0\u5de5\u4f5c\u539f\u7406<\/h4>\n<p>\u6bd4\u56db\u5c42\u591a\u4e86 \u201c\u89e3\u6790 &#043; \u51b3\u7b56\u201d \u73af\u8282<\/p>\n<li>\n<p>\u5efa\u7acb\u8fde\u63a5&#xff1a;\u5ba2\u6237\u7aef\u548c HAProxy \u5efa\u7acb TCP \u8fde\u63a5&#xff08;\u4e09\u6b21\u63e1\u624b&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u63a5\u6536\u5e76\u89e3\u6790\u8bf7\u6c42&#xff1a;HAProxy \u63a5\u6536\u5ba2\u6237\u7aef\u7684 HTTP \u8bf7\u6c42&#xff0c;\u89e3\u6790\u5176\u4e2d\u7684\u57df\u540d&#xff08;Host&#xff09;\u3001URL \u8def\u5f84\u3001Cookie\u3001\u8bf7\u6c42\u5934&#xff08;User-Agent\/Referer&#xff09; \u7b49\u5185\u5bb9&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u667a\u80fd\u8def\u7531\u51b3\u7b56&#xff1a;\u6839\u636e\u9884\u8bbe\u89c4\u5219&#xff08;\u6bd4\u5982 \u201c\/api\/\u201d \u8def\u5f84\u8f6c\u53d1\u5230 API \u670d\u52a1\u5668&#xff0c;\u201c\/static\/\u201d \u8f6c\u53d1\u5230\u9759\u6001\u8d44\u6e90\u670d\u52a1\u5668&#xff09;&#xff0c;\u9009\u62e9\u5bf9\u5e94\u7684\u540e\u7aef\u670d\u52a1\u5668\u6c60&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8f6c\u53d1\u8bf7\u6c42 &#043; \u5904\u7406\u54cd\u5e94&#xff1a;\u628a\u89e3\u6790\u540e\u7684\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u9009\u4e2d\u7684\u540e\u7aef\u8282\u70b9&#xff0c;\u8fd8\u80fd\u4fee\u6539\u8bf7\u6c42 \/ \u54cd\u5e94\u5934&#xff08;\u6bd4\u5982\u6dfb\u52a0 X-Forwarded-For \u8bb0\u5f55\u771f\u5b9e IP&#xff09;&#xff0c;\u518d\u628a\u540e\u7aef\u54cd\u5e94\u56de\u4f20\u7ed9\u5ba2\u6237\u7aef&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u7ef4\u6301\u8fde\u63a5&#xff1a;\u53ef\u590d\u7528\u8fde\u63a5&#xff08;\u6bd4\u5982\u957f\u8fde\u63a5&#xff09;&#xff0c;\u63d0\u5347\u6574\u4f53\u6027\u80fd\u3002<\/p>\n<\/li>\n<p>\u6838\u5fc3\u7279\u70b9&#xff1a;<span style=\"color:#fe2c24\">\u66f4\u667a\u80fd\u3001\u66f4\u7075\u6d3b&#xff0c;\u4f46\u6027\u80fd\u7565\u4f4e\u4e8e\u56db\u5c42<\/span>&#xff08;\u56e0\u4e3a\u591a\u4e86\u89e3\u6790\u6b65\u9aa4&#xff09;\u3002<\/p>\n<h4 id=\"2.3%C2%A0HAProxy%20%E4%B8%83%E5%B1%82%E4%BB%A3%E7%90%86%E7%9A%84%E6%A0%B8%E5%BF%83%E7%89%B9%E7%82%B9%EF%BC%88%E5%AF%B9%E6%AF%94%E5%9B%9B%E5%B1%82%EF%BC%89\">2.3\u00a0HAProxy \u4e03\u5c42\u4ee3\u7406\u7684\u6838\u5fc3\u7279\u70b9&#xff08;\u5bf9\u6bd4\u56db\u5c42&#xff09;<\/h4>\n<table>\n<tr>\u7279\u6027\u4e03\u5c42\u4ee3\u7406&#xff08;HTTP\/HTTPS&#xff09;\u56db\u5c42\u8d1f\u8f7d&#xff08;TCP\/UDP&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>\u89e3\u6790\u6df1\u5ea6<\/td>\n<td>\u89e3\u6790 HTTP \u8bf7\u6c42\u5934\u3001URL\u3001Cookie \u7b49<\/td>\n<td>\u4ec5\u89e3\u6790 IP &#043; \u7aef\u53e3<\/td>\n<\/tr>\n<tr>\n<td>\u8f6c\u53d1\u89c4\u5219<\/td>\n<td>\u53ef\u6309\u57df\u540d\u3001URL\u3001Cookie \u7b49\u8f6c\u53d1<\/td>\n<td>\u4ec5\u6309 IP &#043; \u7aef\u53e3 \/ \u7b97\u6cd5\u8f6c\u53d1<\/td>\n<\/tr>\n<tr>\n<td>\u529f\u80fd\u6269\u5c55<\/td>\n<td>\u652f\u6301 URL \u91cd\u5199\u3001\u8bf7\u6c42\u5934\u4fee\u6539\u3001\u4f1a\u8bdd\u4fdd\u6301\u3001HTTPS \u7ec8\u6b62<\/td>\n<td>\u65e0&#xff08;\u7eaf\u8f6c\u53d1&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u6027\u80fd<\/td>\n<td>\u7a0d\u4f4e&#xff08;\u89e3\u6790\u5f00\u9500&#xff09;<\/td>\n<td>\u6781\u9ad8&#xff08;\u7eaf\u8f6c\u53d1&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>Web \u670d\u52a1&#xff08;HTTP\/HTTPS&#xff09;<\/td>\n<td>\u6570\u636e\u5e93\u3001Redis\u3001SSH \u7b49 TCP \u670d\u52a1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"3.%20HAProxy%20%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95%E5%AE%9E%E9%AA%8C\">3. HAProxy \u8c03\u5ea6\u7b97\u6cd5\u5b9e\u9a8c<\/h3>\n<h4 id=\"3.1%E9%9D%99%E6%80%81%E7%AE%97%E6%B3%95%EF%BC%88%E6%9D%83%E9%87%8D%E5%9B%BA%E5%AE%9A%EF%BC%8C%E4%B8%8D%E6%94%AF%E6%8C%81%E7%83%AD%E6%9B%B4%E6%96%B0%EF%BC%89\">3.1\u9759\u6001\u7b97\u6cd5&#xff08;\u6743\u91cd\u56fa\u5b9a&#xff0c;\u4e0d\u652f\u6301\u70ed\u66f4\u65b0&#xff09;<\/h4>\n<h5 id=\"1.%C2%A0static-rr%EF%BC%88%E9%9D%99%E6%80%81%E8%BD%AE%E8%AF%A2%EF%BC%89\">1.\u00a0static-rr&#xff08;\u9759\u6001\u8f6e\u8be2&#xff09;<\/h5>\n<p>\u914d\u7f6e&#xff1a;\u6307\u5b9abalance static-rr&#xff0c;\u4e3a\u540e\u7aef\u670d\u52a1\u5668\u8bbe\u7f6e\u6743\u91cd&#xff08;\u5982 haha \u6743\u91cd 2\u3001hehe \u6743\u91cd 1&#xff09;\u3002<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten webcluster<br \/>\n    bind        *:80<br \/>\n    balance     static-rr<br \/>\n    server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2<br \/>\n    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<p>\u6d4b\u8bd5\u7ed3\u679c&#xff1a;\u8bf7\u6c42\u6309\u6743\u91cd\u6bd4\u4f8b\u5206\u914d&#xff0c;haha&#xff08;192.168.0.10&#xff09;\u63a5\u6536\u8bf7\u6c42\u6570\u7ea6\u4e3a hehe&#xff08;192.168.0.20&#xff09;\u7684 2 \u500d\u3002<\/p>\n<p>#\u6d4b\u8bd5<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 for i in {1..10}; do curl 172.25.254.100; done<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10 <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"410\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143247-69a1ab0f9058c.png\" width=\"399\" \/><\/p>\n<p>\u68c0\u6d4b\u662f\u5426\u652f\u6301\u70ed\u66f4\u65b0<\/p>\n<p>[root&#064;haproxy ~]# echo &#034;get  weight webcluster\/haha&#034; | socat  stdio \/var\/lib\/haproxy\/stats<br \/>\n2 (initial 2)<\/p>\n<p>[root&#064;haproxy ~]# echo &#034;set  weight  webcluster\/haha 1  &#034; | socat stdio \/var\/lib\/haproxy\/stats<br \/>\nBackend is using a static LB algorithm and only accepts weights &#039;0%&#039; and &#039;100%&#039; <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"288\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143247-69a1ab0faa47b.png\" width=\"835\" \/><\/p>\n<p>\u7279\u6027&#xff1a;\u4e0d\u652f\u6301\u70ed\u66f4\u65b0\u6743\u91cd&#xff08;\u4ec5\u5141\u8bb8\u8bbe\u7f6e 0%\/100%&#xff09;&#xff0c;\u6743\u91cd\u4fee\u6539\u9700\u91cd\u542f\u670d\u52a1\u3002<\/p>\n<h5 id=\"2.%20first%EF%BC%88%E4%BC%98%E5%85%88%E5%88%86%E9%85%8D%EF%BC%89\">2. first&#xff08;\u4f18\u5148\u5206\u914d&#xff09;<\/h5>\n<p>\u914d\u7f6e&#xff1a;\u6307\u5b9abalance first&#xff0c;\u53ef\u8bbe\u7f6emaxconn&#xff08;\u6700\u5927\u8fde\u63a5\u6570&#xff09;\u3002<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten webcluster<br \/>\n    bind        *:80<br \/>\n    balance     first<br \/>\n    server haha 192.168.0.10:80 maxconn 1 check inter 3s fall 3 rise 5 weight 2<br \/>\n    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<p>#\u6d4b\u8bd5&#xff1a;\u5728\u4e00\u4e2ashell\u4e2d\u6267\u884c\u6301\u7eed\u8bbf\u95ee<\/p>\n<p>[Administrator.DESKTOP-VJ307M3] \u27a4 while true; do curl 172.25.254.100; done<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\n&#8230;. &#8230;.. <\/p>\n<p>#\u5728\u5176\u4ed6\u8bbe\u7acb\u4e86\u4e2d\u5efa\u7acb\u6301\u7eed\u8bbf\u95ee\u5e76\u89c2\u5bdf<\/p>\n<p>Administrator.DESKTOP-VJ307M3] \u27a4  while true; do curl 172.25.254.100; done<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10#\u6b64\u5904\u51fa\u73b010\u4fe1\u606f<br \/>\nwebserver2 &#8211; 192.168.0.20 <\/p>\n<h4 id=\"3.2%20%E5%8A%A8%E6%80%81%E7%AE%97%E6%B3%95%EF%BC%88%E6%9D%83%E9%87%8D%E6%94%AF%E6%8C%81%E7%83%AD%E6%9B%B4%E6%96%B0%EF%BC%8C%E5%AE%9E%E6%97%B6%E8%B0%83%E6%95%B4%EF%BC%89\">3.2 \u52a8\u6001\u7b97\u6cd5&#xff08;\u6743\u91cd\u652f\u6301\u70ed\u66f4\u65b0&#xff0c;\u5b9e\u65f6\u8c03\u6574&#xff09;<\/h4>\n<h5 id=\"1.%C2%A0roundrobin%EF%BC%88%E5%8A%A8%E6%80%81%E8%BD%AE%E8%AF%A2%EF%BC%89\">1.\u00a0roundrobin&#xff08;\u52a8\u6001\u8f6e\u8be2&#xff09;<\/h5>\n<p>\u914d\u7f6e&#xff1a;\u6307\u5b9abalance roundrobin&#xff0c;\u652f\u6301\u6743\u91cd\u914d\u7f6e\u3002<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten webcluster<br \/>\n    bind        *:80<br \/>\n    balance     roundrobin<br \/>\n    server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2<br \/>\n    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service<br \/>\n#\u6d4b\u8bd5<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 for i in {1..10}; do curl 172.25.254.100; done<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10 <\/p>\n<p>\u52a8\u6001\u6743\u91cd\u66f4\u65b0<\/p>\n<p>[root&#064;haproxy ~]# echo &#034;get  weight webcluster\/haha&#034; | socat  stdio \/var\/lib\/haproxy\/stats<br \/>\n2 (initial 2)<\/p>\n<p>[root&#064;haproxy ~]# echo &#034;set  weight  webcluster\/haha 1  &#034; | socat stdio \/var\/lib\/haproxy\/stats<br \/>\n[root&#064;haproxy ~]# echo &#034;get  weight webcluster\/haha&#034; | socat  stdio \/var\/lib\/haproxy\/stats<br \/>\n1 (initial 2) <\/p>\n<p>\u6d4b\u8bd5\u7ed3\u679c&#xff1a;\u9ed8\u8ba4\u6309\u6743\u91cd\u6bd4\u4f8b\u5206\u914d\u8bf7\u6c42&#xff08;haha:hehe&#061;2:1&#xff09;&#xff1b;\u70ed\u66f4\u65b0\u6743\u91cd\u540e&#xff08;haha \u6743\u91cd\u6539\u4e3a 1&#xff09;&#xff0c;\u8bf7\u6c42\u6309 1:1 \u6bd4\u4f8b\u5206\u914d\u3002<\/p>\n<p>#\u6548\u679c<\/p>\n<p>[Administrator.DESKTOP-VJ307M3] \u27a4 for i in {1..10}; do curl 172.25.254.100; done<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10 <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"273\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143247-69a1ab0fcb30e.png\" width=\"833\" \/><\/p>\n<p>\u7279\u6027&#xff1a;\u652f\u6301\u901a\u8fc7socat\u547d\u4ee4\u5b9e\u65f6\u4fee\u6539\u6743\u91cd&#xff0c;\u65e0\u9700\u91cd\u542f\u670d\u52a1\u3002<\/p>\n<h5 id=\"2.%C2%A0leastconn%EF%BC%88%E6%9C%80%E5%B0%91%E8%BF%9E%E6%8E%A5%E6%95%B0%EF%BC%89\">2.\u00a0leastconn&#xff08;\u6700\u5c11\u8fde\u63a5\u6570&#xff09;<\/h5>\n<p>\u914d\u7f6e&#xff1a;\u6307\u5b9abalance leastconn&#xff0c;\u57fa\u4e8e\u540e\u7aef\u670d\u52a1\u5668\u5f53\u524d\u8fde\u63a5\u6570\u5206\u914d\u8bf7\u6c42\u3002<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten webcluster<br \/>\n    bind        *:80<br \/>\n    balance     leastconn<br \/>\n    server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2<br \/>\n    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 for i in {1..10}; do curl 172.25.254.100; done<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20 <\/p>\n<p>\u6d4b\u8bd5\u7ed3\u679c&#xff1a;\u8bf7\u6c42\u5728\u4e24\u53f0\u670d\u52a1\u5668\u95f4\u5747\u5300\u5206\u914d&#xff08;haha \u4e0e hehe \u4ea4\u66ff\u63a5\u6536\u8bf7\u6c42&#xff09;&#xff0c;\u4f18\u5148\u8f6c\u53d1\u81f3\u8fde\u63a5\u6570\u6700\u5c11\u7684\u670d\u52a1\u5668\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"250\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143247-69a1ab0feba1c.png\" width=\"835\" \/><\/p>\n<h4 id=\"3.3%20%E6%B7%B7%E5%90%88%E7%AE%97%E6%B3%95%EF%BC%88%E5%9F%BA%E4%BA%8E%E5%93%88%E5%B8%8C%20%2F%20%E7%89%B9%E5%BE%81%E5%80%BC%E5%88%86%E9%85%8D%EF%BC%8C%E4%BF%9D%E8%AF%81%E4%BC%9A%E8%AF%9D%E7%B2%98%E6%80%A7%EF%BC%89\">3.3 \u6df7\u5408\u7b97\u6cd5&#xff08;\u57fa\u4e8e\u54c8\u5e0c \/ \u7279\u5f81\u503c\u5206\u914d&#xff0c;\u4fdd\u8bc1\u4f1a\u8bdd\u7c98\u6027&#xff09;<\/h4>\n<h5 id=\"1.%C2%A0source%EF%BC%88%E5%9F%BA%E4%BA%8E%E5%AE%A2%E6%88%B7%E7%AB%AF%20IP%EF%BC%89\">1.\u00a0source&#xff08;\u57fa\u4e8e\u5ba2\u6237\u7aef IP&#xff09;<\/h5>\n<p>\u914d\u7f6e&#xff1a;\u9ed8\u8ba4balance source\u4e3a\u9759\u6001\u54c8\u5e0c&#xff1b;\u589e\u52a0hash-type consistent\u4e3a\u52a8\u6001\u4e00\u81f4\u6027\u54c8\u5e0c\u3002<\/p>\n<p>#\u9ed8\u8ba4\u9759\u6001\u7b97\u6cd5<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten webcluster<br \/>\n    bind        *:80<br \/>\n    balance     source<br \/>\n    server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2<br \/>\n    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<p>\u6d4b\u8bd5\u7ed3\u679c&#xff1a;\u540c\u4e00\u5ba2\u6237\u7aef IP \u7684\u6240\u6709\u8bf7\u6c42\u59cb\u7ec8\u8f6c\u53d1\u81f3\u540c\u4e00\u53f0\u670d\u52a1\u5668&#xff0c;\u4fdd\u8bc1\u4f1a\u8bdd\u7c98\u6027\u3002<\/p>\n<p>#\u6d4b\u8bd5&#xff1a;<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 for i in {1..10}; do curl 172.25.254.100; done<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20 <\/p>\n<p>#source\u52a8\u6001\u7b97\u6cd5<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten webcluster<br \/>\n    bind        *:80<br \/>\n    balance     source<br \/>\n    hash-type consistent<br \/>\n    server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2<br \/>\n    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service<br \/>\n#\u6d4b\u8bd5&#xff1a;<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 for i in {1..10}; do curl 172.25.254.100; done<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20 <\/p>\n<h5 id=\"2.%C2%A0uri%EF%BC%88%E5%9F%BA%E4%BA%8E%E8%AF%B7%E6%B1%82%20URI%EF%BC%89\">2.\u00a0<span style=\"font-size:22px\">uri<\/span>&#xff08;\u57fa\u4e8e\u8bf7\u6c42 URI&#xff09;<\/h5>\n<p>#\u4e3b\u5907\u5b9e\u9a8c\u73af\u5883<br \/>\n[root&#064;webserver1 ~]# echo RS1 &#8211; 192.168.0.10 &gt; \/var\/www\/html\/index1.html<br \/>\n[root&#064;webserver1 ~]# echo RS1 &#8211; 192.168.0.10 &gt; \/var\/www\/html\/index2.html<br \/>\n[root&#064;webserver2 ~]# echo RS2 &#8211; 192.168.0.20 &gt; \/var\/www\/html\/index1.html<br \/>\n[root&#064;webserver2 ~]# echo RS2 &#8211; 192.168.0.20 &gt; \/var\/www\/html\/index2.html <\/p>\n<p>\u914d\u7f6e&#xff1a;\u6307\u5b9abalance uri &#043; hash-type consistent\u3002<\/p>\n<p>#\u8bbe\u5b9auri\u7b97\u6cd5<br \/>\n[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten webcluster<br \/>\n    bind        *:80<br \/>\n    balance     uri<br \/>\n    hash-type consistent<br \/>\n    server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2<br \/>\n    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<p>\u6d4b\u8bd5\u7ed3\u679c&#xff1a;\u76f8\u540c URI&#xff08;\u5982 \/index.html\u3001\/index2.html&#xff09;\u7684\u8bf7\u6c42\u56fa\u5b9a\u8f6c\u53d1\u81f3\u540c\u4e00\u53f0\u670d\u52a1\u5668&#xff08;\/index.html\u2192haha&#xff0c;\/index2.html\u2192hehe&#xff09;\u3002<\/p>\n<p>#\u6d4b\u8bd5&#xff1a;<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 for i in {1..10}; do curl 172.25.254.100\/index.html; done<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\n                                                                                                    \u2714<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n[2026-01-25 14:51.59]  ~<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 for i in {1..10}; do curl 172.25.254.100\/index2.html; done<br \/>\nRS2 &#8211; 172.168.0.20<br \/>\nRS2 &#8211; 172.168.0.20<br \/>\nRS2 &#8211; 172.168.0.20<br \/>\nRS2 &#8211; 172.168.0.20<br \/>\nRS2 &#8211; 172.168.0.20<br \/>\nRS2 &#8211; 172.168.0.20<br \/>\nRS2 &#8211; 172.168.0.20<br \/>\nRS2 &#8211; 172.168.0.20<br \/>\nRS2 &#8211; 172.168.0.20<br \/>\nRS2 &#8211; 172.168.0.20 <\/p>\n<h5 id=\"3.%C2%A0url_param%EF%BC%88%E5%9F%BA%E4%BA%8E%20URL%20%E5%8F%82%E6%95%B0%EF%BC%89\">3.\u00a0url_param&#xff08;\u57fa\u4e8e URL \u53c2\u6570&#xff09;<\/h5>\n<p>#\u4e3b\u5907\u5b9e\u9a8c\u73af\u5883<br \/>\n[root&#064;webserver1 ~]# echo RS1 &#8211; 192.168.0.10 &gt; \/var\/www\/html\/index1.html<br \/>\n[root&#064;webserver1 ~]# echo RS1 &#8211; 192.168.0.10 &gt; \/var\/www\/html\/index2.html<br \/>\n[root&#064;webserver2 ~]# echo RS2 &#8211; 192.168.0.20 &gt; \/var\/www\/html\/index1.html<br \/>\n[root&#064;webserver2 ~]# echo RS2 &#8211; 192.168.0.20 &gt; \/var\/www\/html\/index2.html <\/p>\n<p>\u914d\u7f6e&#xff1a;\u6307\u5b9abalance url_param [\u53c2\u6570\u540d] &#043; hash-type consistent&#xff08;\u5982\u53c2\u6570 name&#xff09;\u3002<\/p>\n<p>#\u8bbe\u5b9aurl_param\u7b97\u6cd5<br \/>\n[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten webcluster<br \/>\n    bind        *:80<br \/>\n    balance     url_param name<br \/>\n    hash-type consistent<br \/>\n    server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2<br \/>\n    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<p>\u6d4b\u8bd5\u7ed3\u679c&#xff1a;\u76f8\u540c URL \u53c2\u6570\u503c&#xff08;\u5982 name&#061;lee\u2192hehe&#xff0c;name&#061;redhat\u2192haha&#xff09;\u7684\u8bf7\u6c42\u56fa\u5b9a\u8f6c\u53d1\u81f3\u540c\u4e00\u53f0\u670d\u52a1\u5668\u3002<\/p>\n<p>#\u6d4b\u8bd5&#xff1a;<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 for i in {1..10}; do curl 172.25.254.100\/index.html?name&#061;lee; done<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<\/p>\n<p>[Administrator.DESKTOP-VJ307M3] \u27a4 for i in {1..10}; do curl 172.25.254.100\/index.html?name&#061;redhat; done<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver1 &#8211; 192.168.0.10 <\/p>\n<h5 id=\"4.%C2%A0hdr%EF%BC%88%E5%9F%BA%E4%BA%8E%20HTTP%20%E8%AF%B7%E6%B1%82%E5%A4%B4%EF%BC%89\">4.\u00a0hdr&#xff08;\u57fa\u4e8e HTTP \u8bf7\u6c42\u5934&#xff09;<\/h5>\n<p>\u914d\u7f6e&#xff1a;\u6307\u5b9abalance hdr(User-Agent) &#043; hash-type consistent&#xff08;\u57fa\u4e8e User-Agent \u5934&#xff09;\u3002<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten webcluster<br \/>\n    bind        *:80<br \/>\n    balance     hdr&#xff08;User-Agent&#xff09;<br \/>\n    hash-type consistent<br \/>\n    server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2<br \/>\n    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<p>\u6d4b\u8bd5\u7ed3\u679c&#xff1a;\u76f8\u540c User-Agent \u503c&#xff08;\u5982 lee\u2192hehe&#xff0c;timing\u2192haha&#xff09;\u7684\u8bf7\u6c42\u56fa\u5b9a\u8f6c\u53d1\u81f3\u540c\u4e00\u53f0\u670d\u52a1\u5668\u3002<\/p>\n<p>#\u6d4b\u8bd5&#xff1a;<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 curl -A &#034;lee&#034; 172.25.254.100<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\n                                                                                                    \u2714<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n[2026-01-25 15:00.53]  ~<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 curl -A &#034;lee&#034; 172.25.254.100<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\n                                                                                                    \u2714<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n[2026-01-25 15:00.54]  ~<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 curl -A &#034;timinglee&#034; 172.25.254.100<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\n                                                                                                    \u2714<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n[2026-01-25 15:01.00]  ~<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 curl -A &#034;timing&#034; 172.25.254.100<br \/>\nwebserver1 &#8211; 192.168.0.10 <\/p>\n<p>\u6838\u5fc3\u7ed3\u8bba<\/p>\n<li>\n<p>\u9759\u6001\u7b97\u6cd5&#xff08;static-rr\u3001first&#xff09;\u9002\u7528\u4e8e\u6743\u91cd\u56fa\u5b9a\u3001\u65e0\u9700\u52a8\u6001\u8c03\u6574\u7684\u573a\u666f&#xff0c;\u914d\u7f6e\u7b80\u5355\u4f46\u7075\u6d3b\u6027\u4f4e&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u52a8\u6001\u7b97\u6cd5&#xff08;roundrobin\u3001leastconn&#xff09;\u9002\u7528\u4e8e\u9700\u5b9e\u65f6\u8c03\u6574\u6743\u91cd\u3001\u5747\u8861\u8fde\u63a5\u6570\u7684\u573a\u666f&#xff0c;\u7075\u6d3b\u6027\u9ad8&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6df7\u5408\u7b97\u6cd5&#xff08;source\/uri\/url_param\/hdr&#xff09;\u9002\u7528\u4e8e\u9700\u8981\u4f1a\u8bdd\u7c98\u6027\u7684\u573a\u666f&#xff0c;\u901a\u8fc7\u54c8\u5e0c\u7279\u5f81\u4fdd\u8bc1\u540c\u4e00\u5ba2\u6237\u7aef \/ \u8bf7\u6c42\u7279\u5f81\u7684\u8bf7\u6c42\u8f6c\u53d1\u81f3\u56fa\u5b9a\u670d\u52a1\u5668\u3002<\/p>\n<\/li>\n<h2 id=\"%E4%BA%94%E3%80%81%E5%AE%89%E5%85%A8%E3%80%81%E4%BC%9A%E8%AF%9D%E4%B8%8E%E9%AB%98%E5%8F%AF%E7%94%A8\">\u4e94\u3001\u5b89\u5168\u3001\u4f1a\u8bdd\u4e0e\u9ad8\u53ef\u7528<\/h2>\n<h3 id=\"1.%C2%A0HAProxy%20ACL%20%E8%AE%BF%E9%97%AE%E6%8E%A7%E5%88%B6\">1.\u00a0HAProxy ACL \u8bbf\u95ee\u63a7\u5236<\/h3>\n<h4 id=\"1.1%C2%A0%E4%BB%80%E4%B9%88%E6%98%AF%20HAProxy%20ACL%EF%BC%9F\">1.1\u00a0\u4ec0\u4e48\u662f HAProxy ACL&#xff1f;<\/h4>\n<p>HAProxy ACL \u672c\u8d28\u662f<span style=\"color:#fe2c24\">\u4e00\u7ec4 \u201c\u5339\u914d\u89c4\u5219\u201d<\/span> \u2014\u2014 \u901a\u8fc7\u5b9a\u4e49 ACL \u89c4\u5219&#xff0c;\u5339\u914d\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u7279\u5f81&#xff08;\u6bd4\u5982\u57df\u540d\u3001URL\u3001IP\u3001\u8bf7\u6c42\u5934\u3001Cookie \u7b49&#xff09;&#xff0c;\u518d\u6839\u636e \u201c\u5339\u914d\u7ed3\u679c\u201d \u6267\u884c\u5bf9\u5e94\u7684\u52a8\u4f5c&#xff08;\u6bd4\u5982\u8f6c\u53d1\u5230\u6307\u5b9a\u540e\u7aef\u3001\u62d2\u7edd\u8bbf\u95ee\u3001\u91cd\u5b9a\u5411\u7b49&#xff09;\u3002<\/p>\n<p>\u7b80\u5355\u6bd4\u55bb&#xff1a;<\/p>\n<ul>\n<li>\n<p>ACL &#061; \u673a\u573a\u5b89\u68c0\u7684 \u201c\u7b5b\u67e5\u89c4\u5219\u201d&#xff1a;\u6bd4\u5982 \u201c\u643a\u5e26\u6db2\u4f53\u7684\u65c5\u5ba2\u2192\u8d70\u6db2\u4f53\u68c0\u67e5\u901a\u9053\u201d\u201cVIP \u65c5\u5ba2\u2192\u8d70\u8d35\u5bbe\u901a\u9053\u201d\u201c\u9ed1\u540d\u5355\u65c5\u5ba2\u2192\u7981\u6b62\u767b\u673a\u201d&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u52a8\u4f5c &#061; \u89c4\u5219\u5339\u914d\u540e\u7684\u5904\u7406\u65b9\u5f0f&#xff1a;\u8f6c\u53d1\u3001\u62d2\u7edd\u3001\u91cd\u5b9a\u5411\u7b49\u3002<\/p>\n<\/li>\n<\/ul>\n<p>ACL \u662f HAProxy \u4e03\u5c42\u4ee3\u7406\u7684\u7075\u9b42&#xff0c;\u6ca1\u6709 ACL&#xff0c;\u4e03\u5c42\u4ee3\u7406\u5c31\u53ea\u80fd\u505a\u7b80\u5355\u7684\u8f6e\u8be2\u8f6c\u53d1&#xff0c;\u6709\u4e86 ACL \u624d\u80fd\u5b9e\u73b0 \u201c\u667a\u80fd\u5206\u6d41\u3001\u5b89\u5168\u7ba1\u63a7\u201d\u3002<\/p>\n<h4 id=\"1.2%C2%A0ACL%20%E7%9A%84%E6%A0%B8%E5%BF%83%E4%BD%9C%E7%94%A8\">1.2\u00a0ACL \u7684\u6838\u5fc3\u4f5c\u7528<\/h4>\n<ul>\n<li>\n<p>\u667a\u80fd\u8def\u7531&#xff1a;\u6309\u57df\u540d\u3001URL\u3001\u8bf7\u6c42\u5934\u7b49\u5206\u53d1\u8bf7\u6c42&#xff08;\u6bd4\u5982www.xxx.com\u2192Web \u670d\u52a1\u5668&#xff0c;api.xxx.com\u2192API \u670d\u52a1\u5668&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8bbf\u95ee\u63a7\u5236&#xff1a;\u9ed1\u767d\u540d\u5355&#xff08;\u6bd4\u5982\u7981\u6b62\u7279\u5b9a IP \u8bbf\u95ee\u3001\u53ea\u5141\u8bb8\u5185\u7f51 IP \u8bbf\u95ee&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5b89\u5168\u9632\u62a4&#xff1a;\u62e6\u622a\u6076\u610f\u8bf7\u6c42&#xff08;\u6bd4\u5982\u7981\u6b62\u8bbf\u95ee \/admin \u8def\u5f84\u7684\u975e\u6cd5 IP\u3001\u62e6\u622a\u722c\u866b UA&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6d41\u91cf\u8fc7\u6ee4&#xff1a;\u533a\u5206\u52a8\u9759\u8bf7\u6c42&#xff08;\u6bd4\u5982.jpg\/.css\u2192\u9759\u6001\u670d\u52a1\u5668&#xff0c;\/api\u2192\u52a8\u6001\u670d\u52a1\u5668&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6761\u4ef6\u89e6\u53d1&#xff1a;\u6bd4\u5982 \u201cHTTPS \u8bf7\u6c42\u2192\u5141\u8bb8\u8bbf\u95ee&#xff0c;HTTP \u8bf7\u6c42\u2192\u5f3a\u5236\u8df3\u8f6c HTTPS\u201d\u3002<\/p>\n<\/li>\n<\/ul>\n<h4 id=\"1.3%C2%A0%E5%AE%9E%E9%AA%8C%E5%87%86%E5%A4%87\">1.3\u00a0\u5b9e\u9a8c\u51c6\u5907<\/h4>\n<h5 id=\"1.%C2%A0%E6%9C%AC%E5%9C%B0%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90%E9%85%8D%E7%BD%AE%EF%BC%9A\">1.\u00a0\u672c\u5730\u57df\u540d\u89e3\u6790\u914d\u7f6e&#xff1a;<\/h5>\n<ul>\n<li>\n<p>Windows&#xff1a;\u901a\u8fc7\u4fee\u6539 hosts \u6587\u4ef6\u914d\u7f6e\u57df\u540d\u4e0e IP \u6620\u5c04&#xff1b;<\/p>\n<\/li>\n<li>\n<p>Linux&#xff1a;\u7f16\u8f91\/etc\/hosts&#xff0c;\u5c06\u591a\u4e2a\u57df\u540d&#xff08;\u5982www.timinglee.org\u3001bbs.timinglee.org\u3001www.lee.com\u7b49&#xff09;\u6307\u5411 172.25.254.100&#xff0c;\u5e76\u901a\u8fc7 ping \u6d4b\u8bd5\u89e3\u6790\u6709\u6548\u6027\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u627e\u5230\u8fd9\u4e2a\u6587\u4ef6&#xff0c;\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"210\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143248-69a1ab101532a.png\" width=\"1121\" \/><\/p>\n<h5 id=\"2.%C2%A0Haproxy%20%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE\">2.\u00a0Haproxy \u57fa\u7840\u914d\u7f6e<\/h5>\n<p>\u7f16\u5199\/etc\/haproxy\/haproxy.cfg&#xff0c;\u5b9a\u4e49\u524d\u7aefwebcluster&#xff08;\u7ed1\u5b9a 80 \u7aef\u53e3\u3001http \u6a21\u5f0f&#xff09;&#xff0c;\u4ee5\u53ca\u4e24\u4e2a\u540e\u7aef\u670d\u52a1\u5668\u7ec4webserver-80-web1&#xff08;\u6307\u5411 192.168.0.10:80&#xff09;\u3001webserver-80-web2&#xff08;\u6307\u5411 192.168.0.20:80&#xff09;&#xff0c;\u91cd\u542f haproxy \u670d\u52a1\u751f\u6548\u3002<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nfrontend webcluster<br \/>\n    bind            *:80<br \/>\n    mode            http<br \/>\n    use_backend     webserver-80-web1<\/p>\n<p>backend webserver-80-web1<br \/>\n    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5<\/p>\n<p>backend webserver-80-web2<br \/>\n    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<h4 id=\"1.2%C2%A0%E6%A0%B8%E5%BF%83%20ACL%20%E8%A7%84%E5%88%99%E5%AE%9E%E6%93%8D\">1.2\u00a0\u6838\u5fc3 ACL \u89c4\u5219\u5b9e\u64cd<\/h4>\n<h5 id=\"1.%C2%A0%E5%9F%9F%E5%90%8D%E5%90%8E%E7%BC%80%E5%8C%B9%E9%85%8D\">1.\u00a0\u57df\u540d\u540e\u7f00\u5339\u914d<\/h5>\n<p>acl test hdr_end(host) -i .com&#xff0c;\u5339\u914d\u540e\u8f6c\u53d1\u81f3 web1&#xff0c;\u5426\u5219\u8f6c\u53d1\u81f3 web2<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nfrontend webcluster<br \/>\n    bind            *:80<br \/>\n    mode            http<\/p>\n<p>    acl test hdr_end(host) -i .com#acl\u5217\u8868<\/p>\n<p>    use_backend  webserver-80-web1 if test#acl\u5217\u8868\u8bbf\u95ee\u5339\u914d<br \/>\n    default_backend webserver-80-web2#acl\u5217\u8868\u8bbf\u95ee\u4e0d\u5339\u914d<\/p>\n<p>backend webserver-80-web1<br \/>\n    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5<\/p>\n<p>backend webserver-80-web2<br \/>\n    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5 <\/p>\n<p>\u6548\u679c\u8bf4\u660e&#xff1a;.com \u7ed3\u5c3e\u57df\u540d&#xff08;\u5982www.lee.com&#xff09;\u8bbf\u95ee web1&#xff0c;\u5176\u4ed6&#xff08;\u5982www.lee.org&#xff09;\u8bbf\u95ee web2<\/p>\n<p>[2026-01-26 15:50.45]  ~<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 curl www.lee.com<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\n                                                                                                    \u2714<br \/>\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n[2026-01-26 15:50.45]  ~<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 curl www.lee.org<br \/>\nwebserver2 &#8211; 192.168.0.20 <\/p>\n<h5 id=\"2.%C2%A0%E5%9F%9F%E5%90%8D%E5%89%8D%E7%BC%80%E5%8C%B9%E9%85%8D\">2.\u00a0\u57df\u540d\u524d\u7f00\u5339\u914d<\/h5>\n<p>acl head hdr_beg(host) -i bbs.&#xff0c;\u5339\u914d\u540e\u8f6c\u53d1\u81f3 web1<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nfrontend webcluster<br \/>\n    bind            *:80<br \/>\n    mode            http<\/p>\n<p>    acl head hdr_beg(host) -i bbs.<\/p>\n<p>    use_backend  webserver-80-web1 if head<br \/>\n    default_backend webserver-80-web2<\/p>\n<p>backend webserver-80-web1<br \/>\n    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5<\/p>\n<p>backend webserver-80-web2<br \/>\n    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5 <\/p>\n<p>\u6548\u679c\u8bf4\u660e&#xff1a;bbs \u5f00\u5934\u57df\u540d&#xff08;\u5982bbs.timinglee.org&#xff09;\u4f18\u5148\u8f6c\u53d1\u81f3 web1<\/p>\n<h5 id=\"3.%C2%A0%E8%AF%B7%E6%B1%82%E8%B7%AF%E5%BE%84%E5%8C%B9%E9%85%8D\">3.\u00a0\u8bf7\u6c42\u8def\u5f84\u5339\u914d<\/h5>\n<p>acl pathdir base_dir -i \/lee&#xff0c;\u5339\u914d\u540e\u8f6c\u53d1\u81f3 web1<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nfrontend webcluster<br \/>\n    bind            *:80<br \/>\n    mode            http<\/p>\n<p>    acl pathdir base_dir -i \/lee<\/p>\n<p>    use_backend  webserver-80-web1 if pathdir<br \/>\n    default_backend webserver-80-web2#acl\u5217\u8868\u8bbf\u95ee\u4e0d\u5339\u914d<\/p>\n<p>backend webserver-80-web1<br \/>\n    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5<\/p>\n<p>backend webserver-80-web2<br \/>\n    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5 <\/p>\n<p>\u6548\u679c\u8bf4\u660e&#xff1a;\u8bbf\u95ee \/lee \u8def\u5f84&#xff08;\u5982 172.25.254.100\/lee\/&#xff09;\u8f6c\u53d1\u81f3 web1&#xff0c;\u5176\u4ed6\u8def\u5f84\u8f6c\u53d1\u81f3 web2<\/p>\n<h5 id=\"4.%C2%A0%E6%9D%A5%E6%BA%90%20IP%EF%BC%88%E9%BB%91%E5%90%8D%E5%8D%95%EF%BC%89\">4.\u00a0\u6765\u6e90 IP&#xff08;\u9ed1\u540d\u5355&#xff09;<\/h5>\n<p>acl invalid_src src 172.25.254.1&#xff0c;http-request deny if invalid_src <\/p>\n<p>\u62d2\u7edd 172.25.254.1 \u7684\u6240\u6709\u8bf7\u6c42&#xff0c;\u8fd4\u56de 403 Forbidden<\/p>\n<h5 id=\"5.%C2%A0%E6%9D%A5%E6%BA%90%20IP%EF%BC%88%E7%99%BD%E5%90%8D%E5%8D%95%EF%BC%89\">5.\u00a0\u6765\u6e90 IP&#xff08;\u767d\u540d\u5355&#xff09;<\/h5>\n<p>http-request deny if ! invalid_src <\/p>\n<p>\u4ec5\u5141\u8bb8 172.25.254.1 \u8bbf\u95ee&#xff0c;\u5176\u4ed6 IP \u5747\u88ab\u62d2\u7edd<\/p>\n<p>ACL \u4f7f\u7528\u7684\u5173\u952e\u6ce8\u610f\u4e8b\u9879<\/p>\n<li>\n<p>\u89c4\u5219\u4f18\u5148\u7ea7&#xff1a;HAProxy \u6309 \u201c\u914d\u7f6e\u987a\u5e8f\u201d \u5339\u914d ACL&#xff0c;\u5148\u5b9a\u4e49\u7684\u89c4\u5219\u5148\u751f\u6548&#xff08;\u6bd4\u5982\u5148\u5339\u914d IP \u9ed1\u540d\u5355&#xff0c;\u518d\u5339\u914d\u57df\u540d\u5206\u6d41&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5927\u5c0f\u5199\u654f\u611f&#xff1a;\u9ed8\u8ba4\u5339\u914d\u533a\u5206\u5927\u5c0f\u5199&#xff0c;\u52a0-i\u53c2\u6570\u53ef\u5ffd\u7565&#xff08;\u5982hdr(host) -i www.xxx.com&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6027\u80fd\u4f18\u5316&#xff1a;ACL \u89c4\u5219\u8d8a\u591a&#xff0c;\u5339\u914d\u8017\u65f6\u8d8a\u957f&#xff0c;\u5efa\u8bae\u5c06 \u201c\u9ad8\u9891\u5339\u914d\u7684\u89c4\u5219&#xff08;\u5982 IP \u9ed1\u767d\u540d\u5355&#xff09;\u201d \u653e\u5728\u524d\u9762&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6d4b\u8bd5\u9a8c\u8bc1&#xff1a;\u914d\u7f6e\u540e\u7528curl\u6d4b\u8bd5&#xff08;\u5982curl -H &#034;Host: bbs.timinglee.org&#034; http:\/\/172.25.254.100&#xff09;&#xff0c;\u9a8c\u8bc1\u89c4\u5219\u662f\u5426\u751f\u6548&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u65e5\u5fd7\u8f85\u52a9&#xff1a;\u5f00\u542foption httplog&#xff0c;\u901a\u8fc7\u65e5\u5fd7\u67e5\u770b ACL \u5339\u914d\u7ed3\u679c&#xff0c;\u4fbf\u4e8e\u6392\u9519\u3002<\/p>\n<\/li>\n<h3 id=\"2.%C2%A0HAProxy%20%E5%9F%BA%E4%BA%8E%20Cookie%20%E7%9A%84%E4%BC%9A%E8%AF%9D%E4%BF%9D%E6%8C%81\">2.\u00a0HAProxy \u57fa\u4e8e Cookie \u7684\u4f1a\u8bdd\u4fdd\u6301<\/h3>\n<p>HAProxy \u57fa\u4e8e Cookie \u7684\u4f1a\u8bdd\u4fdd\u6301\u673a\u5236&#xff0c;\u6838\u5fc3\u662f\u901a\u8fc7\u5411\u5ba2\u6237\u7aef\u6d4f\u89c8\u5668\u690d\u5165 Cookie&#xff0c;\u8ba9<span style=\"color:#fe2c24\">\u540c\u4e00\u6d4f\u89c8\u5668<\/span>\u59cb\u7ec8\u8bf7\u6c42\u540e\u7aef\u540c\u4e00\u53f0\u670d\u52a1\u5668&#xff0c;\u800c\u540c\u4e00\u4e3b\u673a\u7684\u4e0d\u540c\u6d4f\u89c8\u5668\u5219\u53ef\u8def\u7531\u5230\u4e0d\u540c\u670d\u52a1\u5668&#xff0c;\u76f8\u6bd4\u4ec5\u57fa\u4e8e\u6e90 IP&#xff08;source \u7b97\u6cd5&#xff09;\u7684\u4f1a\u8bdd\u4fdd\u6301\u66f4\u7cbe\u7ec6\u5316\u3002<\/p>\n<h4 id=\"2.1%C2%A0%E6%A0%B8%E5%BF%83%E9%85%8D%E7%BD%AE\">2.1\u00a0\u6838\u5fc3\u914d\u7f6e<\/h4>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten webcluster<br \/>\n    bind        *:80                # \u76d1\u542c\u6240\u6709\u7f51\u5361\u768480\u7aef\u53e3<br \/>\n    balance     roundrobin          # \u57fa\u7840\u8d1f\u8f7d\u5747\u8861\u7b97\u6cd5\u4e3a\u8f6e\u8be2&#xff08;Cookie \u4f1a\u8986\u76d6\u6b64\u7b97\u6cd5\u7684\u9ed8\u8ba4\u8f6e\u8be2\u903b\u8f91&#xff09;<br \/>\n    hash-type   consistent          # \u4e00\u81f4\u6027\u54c8\u5e0c&#xff08;\u4f18\u5316\u540e\u7aef\u670d\u52a1\u5668\u589e\u51cf\u65f6\u7684\u4f1a\u8bdd\u7a33\u5b9a\u6027&#xff09;<br \/>\n    cookie WEBCOOKIE insert nocache indirect<br \/>\n    # \u540e\u7aef\u670d\u52a1\u5668\u914d\u7f6e&#xff0c;\u6bcf\u4e2a\u670d\u52a1\u5668\u7ed1\u5b9a\u552f\u4e00 Cookie \u6807\u8bc6<br \/>\n    server haha 192.168.0.10:80 cookie web1 check inter 3s fall 3 rise 5 weight 2<br \/>\n    server hehe 192.168.0.20:80 cookie web2 check inter 3s fall 3 rise 5 weight 1<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<p>1.\u00a0cookie \u6307\u4ee4&#xff08;\u6838\u5fc3&#xff09;<\/p>\n<ul>\n<li>\n<p>WEBCOOKIE&#xff1a;\u81ea\u5b9a\u4e49 Cookie \u540d\u79f0&#xff0c;\u7528\u4e8e\u6807\u8bc6\u4f1a\u8bdd\u7ed1\u5b9a\u7684\u540e\u7aef\u670d\u52a1\u5668&#xff1b;<\/p>\n<\/li>\n<li>\n<p>insert&#xff1a;HAProxy \u81ea\u52a8\u5411\u5ba2\u6237\u7aef\u54cd\u5e94\u5934\u63d2\u5165\u8be5 Cookie&#xff1b;<\/p>\n<\/li>\n<li>\n<p>nocache&#xff1a;\u544a\u77e5\u5ba2\u6237\u7aef \/ \u4ee3\u7406\u4e0d\u8981\u7f13\u5b58\u8be5 Cookie&#xff0c;\u786e\u4fdd\u6bcf\u6b21\u8bf7\u6c42\u90fd\u643a\u5e26\u6700\u65b0\u503c&#xff1b;<\/p>\n<\/li>\n<li>\n<p>indirect&#xff1a;\u4ec5\u5f53\u5ba2\u6237\u7aef\u8bf7\u6c42\u4e2d\u65e0\u8be5 Cookie \u65f6&#xff0c;HAProxy \u624d\u63d2\u5165&#xff0c;\u907f\u514d\u8986\u76d6\u5ba2\u6237\u7aef\u5df2\u6709\u903b\u8f91\u3002<\/p>\n<\/li>\n<\/ul>\n<p>2.\u00a0server \u6307\u4ee4\u7684 cookie \u53c2\u6570<\/p>\n<p>\u4e3a\u6bcf\u4e2a\u540e\u7aef\u670d\u52a1\u5668\u6307\u5b9a\u552f\u4e00\u6807\u8bc6&#xff08;\u5982 web1\u3001web2&#xff09;&#xff0c;HAProxy \u4f1a\u5c06\u8be5\u6807\u8bc6\u5199\u5165\u5ba2\u6237\u7aef Cookie&#xff0c;\u540e\u7eed\u5ba2\u6237\u7aef\u8bf7\u6c42\u643a\u5e26\u6b64 Cookie \u65f6&#xff0c;HAProxy \u4f1a\u8def\u7531\u5230\u5bf9\u5e94\u670d\u52a1\u5668\u3002<\/p>\n<h4 id=\"2.2%20%E7%89%B9%E6%80%A7%E4%B8%8E%E6%95%88%E6%9E%9C\">2.2 \u7279\u6027\u4e0e\u6548\u679c<\/h4>\n<ul>\n<li>\n<p>\u7cbe\u7ec6\u5316\u4f1a\u8bdd\u4fdd\u6301&#xff1a;\u540c\u4e00\u5ba2\u6237\u7aef\u4e3b\u673a\u7684\u4e0d\u540c\u6d4f\u89c8\u5668&#xff08;\u5982 Firefox\u3001Edge&#xff09;\u4f1a\u56e0 Cookie \u72ec\u7acb&#xff0c;\u88ab\u8def\u7531\u5230\u4e0d\u540c\u540e\u7aef\u670d\u52a1\u5668&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u540c\u4e00\u6d4f\u89c8\u5668\u4f1a\u8bdd\u7a33\u5b9a&#xff1a;\u53ea\u8981 Cookie \u6709\u6548&#xff0c;\u540c\u4e00\u6d4f\u89c8\u5668\u7684\u6240\u6709\u8bf7\u6c42\u90fd\u4f1a\u56fa\u5b9a\u5230\u67d0\u4e00\u53f0\u540e\u7aef\u670d\u52a1\u5668&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u517c\u5bb9\u57fa\u7840\u8d1f\u8f7d\u5747\u8861&#xff1a;\u672a\u643a\u5e26 Cookie \u7684\u9996\u6b21\u8bf7\u6c42&#xff0c;\u4ecd\u6309 roundrobin \u8f6e\u8be2\u5206\u914d&#xff0c;\u5206\u914d\u540e\u690d\u5165 Cookie \u5b9e\u73b0\u540e\u7eed\u7ed1\u5b9a&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u53ef\u914d\u7f6e\u5065\u5eb7\u68c0\u67e5&#xff1a;check inter 3s fall 3 rise 5 \u5b9a\u4e49\u4e86\u670d\u52a1\u5668\u5065\u5eb7\u68c0\u67e5\u89c4\u5219&#xff08;\u6bcf 3 \u79d2\u68c0\u67e5&#xff0c;\u8fde\u7eed 3 \u6b21\u5931\u8d25\u6807\u8bb0\u4e3a\u4e0d\u53ef\u7528&#xff0c;\u8fde\u7eed 5 \u6b21\u6210\u529f\u6062\u590d&#xff09;&#xff0c;\u7ed3\u5408 weight \u53ef\u8c03\u6574\u670d\u52a1\u5668\u6743\u91cd\u3002<\/p>\n<\/li>\n<\/ul>\n<h4 id=\"2.3%20%E7%94%9F%E6%95%88%E6%B5%81%E7%A8%8B\">2.3 \u751f\u6548\u6d41\u7a0b<\/h4>\n<li>\n<p>\u5ba2\u6237\u7aef\u9996\u6b21\u8bf7\u6c42&#xff08;\u65e0 Cookie&#xff09;&#xff1a;HAProxy \u6309\u8f6e\u8be2\u7b97\u6cd5\u5206\u914d\u540e\u7aef\u670d\u52a1\u5668&#xff0c;\u540c\u65f6\u5728\u54cd\u5e94\u5934\u63d2\u5165\u5305\u542b\u670d\u52a1\u5668\u6807\u8bc6\u7684 Cookie&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aef\u540e\u7eed\u8bf7\u6c42&#xff1a;\u643a\u5e26\u8be5 Cookie \u8bbf\u95ee&#xff0c;HAProxy \u89e3\u6790 Cookie \u4e2d\u7684\u670d\u52a1\u5668\u6807\u8bc6&#xff0c;\u76f4\u63a5\u8def\u7531\u5230\u5bf9\u5e94\u540e\u7aef\u670d\u52a1\u5668&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u670d\u52a1\u5668\u6545\u969c\u65f6&#xff1a;HAProxy \u4f1a\u6839\u636e\u5065\u5eb7\u68c0\u67e5\u7ed3\u679c&#xff0c;\u5c06\u8bf7\u6c42\u91cd\u65b0\u5206\u914d\u5230\u53ef\u7528\u670d\u52a1\u5668&#xff0c;\u5e76\u66f4\u65b0\u5ba2\u6237\u7aef Cookie\u3002<\/p>\n<\/li>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143248-69a1ab1039e4d.png\" \/><\/p>\n<h3 id=\"3.%C2%A0HAProxy%20%E5%85%A8%E7%AB%99%E5%8A%A0%E5%AF%86\">3.\u00a0HAProxy \u5168\u7ad9\u52a0\u5bc6<\/h3>\n<p>HAProxy \u5168\u7ad9\u52a0\u5bc6\u6838\u5fc3\u662f<span style=\"color:#fe2c24\">\u5c06 HTTP \u6d41\u91cf\u5f3a\u5236\u91cd\u5b9a\u5411\u81f3 HTTPS<\/span>&#xff0c;\u5e76\u901a\u8fc7 SSL\/TLS \u8bc1\u4e66\u5b9e\u73b0 HTTPS \u52a0\u5bc6\u4f20\u8f93&#xff0c;\u6574\u4f53\u5206\u4e3a\u300c\u8bc1\u4e66\u5236\u4f5c\u300d\u548c\u300c\u914d\u7f6e\u5168\u7ad9\u52a0\u5bc6\u300d\u4e24\u5927\u6b65\u9aa4\u3002<\/p>\n<h4 id=\"3.1%C2%A0%E5%88%B6%E4%BD%9C%20SSL%2FTLS%20%E8%AF%81%E4%B9%A6\">3.1\u00a0\u5236\u4f5c SSL\/TLS \u8bc1\u4e66<\/h4>\n<p>1. \u521b\u5efa\u8bc1\u4e66\u5b58\u653e\u76ee\u5f55<\/p>\n<p>[root&#064;haproxy ~]# mkdir \/etc\/haproxy\/certs\/ <\/p>\n<p>2.\u00a0\u751f\u6210 RSA \u79c1\u94a5\u548c\u81ea\u7b7e\u540d\u8bc1\u4e66&#xff08;\u6709\u6548\u671f 365 \u5929&#xff09;<\/p>\n<p>[root&#064;haproxy ~]# openssl req -newkey rsa:2048 -nodes -sha256  -keyout \/etc\/haproxy\/certs\/timinglee.org.key -x509 -days 365 -out \/etc\/haproxy\/certs\/timinglee.org.crt<\/p>\n<p>You are about to be asked to enter information that will be incorporated<br \/>\ninto your certificate request.<br \/>\nWhat you are about to enter is what is called a Distinguished Name or a DN.<br \/>\nThere are quite a few fields but you can leave some blank<br \/>\nFor some fields there will be a default value,<br \/>\nIf you enter &#039;.&#039;, the field will be left blank.<br \/>\n&#8212;&#8211;<br \/>\nCountry Name (2 letter code) [XX]:CN<br \/>\nState or Province Name (full name) []:Shaanxi<br \/>\nLocality Name (eg, city) [Default City]:Xi&#039;an<br \/>\nOrganization Name (eg, company) [Default Company Ltd]:timinglee<br \/>\nOrganizational Unit Name (eg, section) []:linux<br \/>\nCommon Name (eg, your name or your server&#039;s hostname) []:www.timinglee.org<br \/>\nEmail Address []:admin&#064;timinglee.org <\/p>\n<p>\u6267\u884c\u540e\u9700\u586b\u5199\u8bc1\u4e66\u4fe1\u606f&#xff08;Distinguished Name&#xff09;&#xff0c;\u5173\u952e\u5b57\u6bb5\u8bf4\u660e&#xff1a;<\/p>\n<ul>\n<li>\n<p>Country Name&#xff1a;\u56fd\u5bb6\u4ee3\u7801&#xff08;\u5982 CN&#xff09;<\/p>\n<\/li>\n<li>\n<p>Common Name&#xff1a;\u670d\u52a1\u5668\u57df\u540d \/ \u4e3b\u673a\u540d&#xff08;\u5982 www.timinglee.org&#xff0c;\u9700\u4e0e\u8bbf\u95ee\u57df\u540d\u4e00\u81f4&#xff09;<\/p>\n<\/li>\n<\/ul>\n<p>3.\u00a0\u5408\u5e76\u79c1\u94a5\u548c\u8bc1\u4e66\u4e3a PEM \u683c\u5f0f<\/p>\n<p>[root&#064;haproxy ~]# cat \/etc\/haproxy\/certs\/timinglee.org.{key,crt} &gt; \/etc\/haproxy\/certs\/timinglee.pem <\/p>\n<h4 id=\"3.2%C2%A0%E9%85%8D%E7%BD%AE%20HAProxy%20%E5%AE%9E%E7%8E%B0%E5%85%A8%E7%AB%99%E5%8A%A0%E5%AF%86\">3.2\u00a0\u914d\u7f6e HAProxy \u5b9e\u73b0\u5168\u7ad9\u52a0\u5bc6<\/h4>\n<p>\u4fee\u6539 HAProxy \u4e3b\u914d\u7f6e\u6587\u4ef6 \/etc\/haproxy\/haproxy.cfg&#xff0c;\u901a\u8fc7\u300cHTTP \u91cd\u5b9a\u5411\u300d&#043;\u300cHTTPS \u76d1\u542c\u300d\u5b9e\u73b0\u5168\u7ad9\u52a0\u5bc6&#xff1a;<\/p>\n<p>1. \u914d\u7f6e HTTP \u524d\u7aef&#xff08;80 \u7aef\u53e3&#xff09;&#xff1a;\u5f3a\u5236\u5c06\u6240\u6709 HTTP \u8bf7\u6c42\u91cd\u5b9a\u5411\u81f3 HTTPS&#xff1a;<\/p>\n<p>frontend webcluster-http<br \/>\n    bind        *:80<br \/>\n    redirect scheme https if ! { ssl_fc }  # \u975e HTTPS \u8bf7\u6c42\u91cd\u5b9a\u5411\u5230 HTTPS <\/p>\n<p>2.\u00a0\u914d\u7f6e HTTPS \u76d1\u542c&#xff08;443 \u7aef\u53e3&#xff09;&#xff1a;\u52a0\u8f7d\u8bc1\u4e66\u5e76\u8f6c\u53d1\u6d41\u91cf\u81f3\u540e\u7aef\u670d\u52a1\u5668&#xff1a;<\/p>\n<p>listen webcluster-https<br \/>\n    bind        *:443 ssl  crt \/etc\/haproxy\/certs\/timinglee.pem  # \u7ed1\u5b9a 443 \u7aef\u53e3\u5e76\u52a0\u8f7d PEM \u8bc1\u4e66<br \/>\n    mode        http  # HTTP \u6a21\u5f0f&#xff08;\u53ef\u89e3\u6790 HTTP \u5934&#xff09;<br \/>\n    balance     roundrobin  # \u8d1f\u8f7d\u5747\u8861\u7b97\u6cd5&#xff08;\u8f6e\u8be2&#xff09;<br \/>\n    # \u540e\u7aef\u670d\u52a1\u5668\u914d\u7f6e&#xff08;\u5065\u5eb7\u68c0\u67e5 &#043; \u6743\u91cd&#xff09;<br \/>\n    server haha 192.168.0.10:80  check inter 3s fall 3 rise 5 weight 1<br \/>\n    server hehe 192.168.0.20:80  check inter 3s fall 3 rise 5 weight 1 <\/p>\n<p>3.\u00a0\u91cd\u542f HAProxy \u4f7f\u914d\u7f6e\u751f\u6548&#xff1a;<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<h4 id=\"3.3%C2%A0%E9%AA%8C%E8%AF%81%E6%95%88%E6%9E%9C\">3.3\u00a0\u9a8c\u8bc1\u6548\u679c<\/h4>\n<p>\u901a\u8fc7 curl \u6d4b\u8bd5 HTTP \u91cd\u5b9a\u5411\u548c HTTPS \u8bbf\u95ee<\/p>\n<p>curl -v -k -L http:\/\/172.25.254.100 <\/p>\n<p>\u5173\u952e\u9a8c\u8bc1\u70b9&#xff1a;<\/p>\n<li>\n<p>HTTP \u8bf7\u6c42\u8fd4\u56de 302 Found&#xff0c;location \u5b57\u6bb5\u6307\u5411 https:\/\/172.25.254.100\/&#xff08;\u91cd\u5b9a\u5411\u751f\u6548&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>HTTPS \u8fde\u63a5\u6210\u529f\u5efa\u7acb&#xff0c;SSL \u534f\u8bae\u4e3a TLSv1.2&#xff0c;\u8bc1\u4e66\u4fe1\u606f\u4e0e\u751f\u6210\u65f6\u4e00\u81f4&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6700\u7ec8\u8fd4\u56de\u540e\u7aef\u670d\u52a1\u5668\u7684\u54cd\u5e94\u5185\u5bb9&#xff08;\u5982 webserver2 &#8211; 192.168.0.20&#xff09;&#xff0c;\u8bf4\u660e\u6d41\u91cf\u8f6c\u53d1\u6b63\u5e38\u3002<\/p>\n<\/li>\n<p>\u6838\u5fc3\u8981\u70b9\u603b\u7ed3<\/p>\n<li>\n<p>\u300c\u5f3a\u5236\u91cd\u5b9a\u5411\u300d&#xff1a;\u901a\u8fc7 redirect scheme https \u786e\u4fdd\u6240\u6709 HTTP \u6d41\u91cf\u81ea\u52a8\u8f6c\u4e3a HTTPS&#xff0c;\u5b9e\u73b0 \u201c\u5168\u7ad9\u52a0\u5bc6\u201d&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u300c\u8bc1\u4e66\u52a0\u8f7d\u300d&#xff1a;HAProxy \u9700\u5c06\u79c1\u94a5\u548c\u8bc1\u4e66\u5408\u5e76\u4e3a PEM \u683c\u5f0f&#xff0c;\u7ed1\u5b9a 443 \u7aef\u53e3\u5e76\u542f\u7528 ssl \u9009\u9879&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u300c\u517c\u5bb9\u6027\u300d&#xff1a;\u652f\u6301 TLSv1.2 \u53ca\u4ee5\u4e0a\u534f\u8bae&#xff0c;\u53ef\u914d\u7f6e cipher \u5957\u4ef6\u589e\u5f3a\u5b89\u5168\u6027&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u300c\u8d1f\u8f7d\u5747\u8861\u300d&#xff1a;HTTPS \u89e3\u5bc6\u540e\u4ecd\u53ef\u57fa\u4e8e HTTP \u5c42\u505a\u8d1f\u8f7d\u5747\u8861\u548c\u5065\u5eb7\u68c0\u67e5&#xff0c;\u4e0d\u5f71\u54cd\u540e\u7aef\u670d\u52a1\u5668\u7684 HTTP \u670d\u52a1\u90e8\u7f72\u3002<\/p>\n<\/li>\n<hr \/>\n<h3 id=\"4.%C2%A0IP%20%E9%80%8F%E4%BC%A0\">4.\u00a0IP \u900f\u4f20<\/h3>\n<p>HAProxy \u7684 IP \u900f\u4f20\u4e3b\u8981\u5206\u4e3a<span style=\"color:#fe2c24\">\u4e03\u5c42&#xff08;HTTP&#xff09;IP \u900f\u4f20<\/span>\u548c<span style=\"color:#fe2c24\">\u56db\u5c42&#xff08;TCP&#xff09;IP \u900f\u4f20<\/span>\u4e24\u79cd\u573a\u666f&#xff0c;\u6838\u5fc3\u76ee\u7684\u662f\u8ba9\u540e\u7aef\u771f\u5b9e\u670d\u52a1\u5668&#xff08;RS&#xff09;\u80fd\u83b7\u53d6\u5230\u5ba2\u6237\u7aef\u7684\u771f\u5b9e IP&#xff0c;\u800c\u975e HAProxy \u7684 IP \u5730\u5740\u3002<\/p>\n<h4 id=\"4.1%20%E4%B8%83%E5%B1%82%EF%BC%88HTTP%EF%BC%89IP%20%E9%80%8F%E4%BC%A0\">4.1 \u4e03\u5c42&#xff08;HTTP&#xff09;IP \u900f\u4f20<\/h4>\n<p>\u57fa\u4e8e HTTP \u534f\u8bae\u5c42\u5b9e\u73b0&#xff0c;\u4f9d\u8d56X-Forwarded-For\u8bf7\u6c42\u5934\u4f20\u9012\u5ba2\u6237\u7aef\u771f\u5b9e IP&#xff0c;\u9002\u7528\u4e8e HTTP\/HTTPS \u4e1a\u52a1\u573a\u666f\u3002<\/p>\n<p>1.\u00a0\u6838\u5fc3\u914d\u7f6e&#xff08;HAProxy \u4fa7&#xff09;<\/p>\n<p>\u4fee\u6539 HAProxy \u4e3b\u914d\u7f6e\u6587\u4ef6\/etc\/haproxy\/haproxy.cfg&#xff0c;\u5728defaults\u6bb5\u5f00\u542f\u900f\u4f20\u529f\u80fd&#xff1a;<\/p>\n<p>defaults<br \/>\n    mode                    http  # \u660e\u786e\u4e3a\u4e03\u5c42HTTP\u6a21\u5f0f<br \/>\n    option forwardfor       except 127.0.0.0\/8  # \u5f00\u542fIP\u900f\u4f20&#xff0c;\u6392\u9664\u672c\u5730\u56de\u73af\u5730\u5740<br \/>\n    # \u5176\u4ed6\u57fa\u7840\u914d\u7f6e&#xff08;\u65e5\u5fd7\u3001\u8d85\u65f6\u3001\u8fde\u63a5\u6570\u7b49&#xff09;\u4fdd\u6301\u9ed8\u8ba4\u6216\u6309\u9700\u8c03\u6574 <\/p>\n<p>2.\u00a0\u540e\u7aef\u670d\u52a1\u5668&#xff08;RS&#xff09;\u914d\u7f6e<\/p>\n<p>[root&#064;webserver2 ~]#  vim \/etc\/httpd\/conf\/httpd.conf<br \/>\n201     LogFormat &#034;%h %l %u %t \\\\&#034;%r\\\\&#034; %&gt;s %b \\\\&#034;%{X-Forwarded-For}i\\\\&#034; \\\\&#034;%{Referer}i\\\\&#034; \\\\&#034;%{User-Agent}i    \\\\&#034;&#034; combined <\/p>\n<ul>\n<li>\n<p>%{X-Forwarded-For}i&#xff1a;\u8868\u793a\u63d0\u53d6 HTTP \u8bf7\u6c42\u5934\u4e2dX-Forwarded-For\u7684\u503c&#xff08;\u5373\u5ba2\u6237\u7aef\u771f\u5b9e IP&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u672a\u914d\u7f6e\u524d&#xff0c;Apache \u65e5\u5fd7\u4e2d\u4ec5\u663e\u793a HAProxy \u7684 IP&#xff08;\u5982 192.168.0.100&#xff09;&#xff1b;\u914d\u7f6e\u540e\u53ef\u770b\u5230\u5ba2\u6237\u7aef\u771f\u5b9e IP&#xff08;\u5982 172.25.254.1&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>3. \u6548\u679c\u9a8c\u8bc1<\/p>\n<p>\u5ba2\u6237\u7aef\u591a\u6b21\u8bbf\u95ee HAProxy \u5730\u5740\u540e&#xff0c;\u540e\u7aef Apache \u65e5\u5fd7\u4f1a\u8bb0\u5f55\u5ba2\u6237\u7aef\u771f\u5b9e IP&#xff1a;<\/p>\n<p>[root&#064;webserver2 ~]# cat \/etc\/httpd\/logs\/access_log<br \/>\n192.168.0.100 &#8211; &#8211; [26\/Jan\/2026:10:10:29 &#043;0800] &#034;GET \/ HTTP\/1.1&#034; 200 26 &#034;172.25.254.1&#034; &#034;-&#034; &#034;curl\/7.65.0&#034;<br \/>\n192.168.0.100 &#8211; &#8211; [26\/Jan\/2026:10:10:30 &#043;0800] &#034;GET \/ HTTP\/1.1&#034; 200 26 &#034;172.25.254.1&#034; &#034;-&#034; &#034;curl\/7.65.0&#034;<br \/>\n192.168.0.100 &#8211; &#8211; [26\/Jan\/2026:10:10:30 &#043;0800] &#034;GET \/ HTTP\/1.1&#034; 200 26 &#034;172.25.254.1&#034; &#034;-&#034; &#034;curl\/7.65.0&#034;<\/p>\n<h4 id=\"4.2%C2%A0%E5%9B%9B%E5%B1%82%EF%BC%88TCP%EF%BC%89IP%20%E9%80%8F%E4%BC%A0\">4.2\u00a0\u56db\u5c42&#xff08;TCP&#xff09;IP \u900f\u4f20<\/h4>\n<p>\u57fa\u4e8e TCP \u534f\u8bae\u5c42\u5b9e\u73b0&#xff0c;\u4f9d\u8d56Proxy Protocol&#xff08;\u4ee3\u7406\u534f\u8bae&#xff09;\u4f20\u9012\u5ba2\u6237\u7aef\u771f\u5b9e IP&#xff0c;\u9002\u7528\u4e8e\u975e HTTP \u7c7b TCP \u4e1a\u52a1&#xff08;\u5982 MySQL\u3001Redis\u3001Nginx \u56db\u5c42\u6a21\u5f0f\u7b49&#xff09;\u3002<\/p>\n<p>1. \u524d\u7f6e\u73af\u5883\u51c6\u5907<\/p>\n<p>\u5148\u5c06\u540e\u7aef RS \u7684 Apache \u66ff\u6362\u4e3a Nginx<\/p>\n<p># \u505c\u6b62Apache&#xff0c;\u5b89\u88c5\u5e76\u542f\u52a8Nginx<br \/>\nsystemctl disable &#8211;now httpd<br \/>\ndnf install nginx -y<br \/>\necho &#034;RS1 &#8211; 192.168.0.10&#034; &gt; \/usr\/share\/nginx\/html\/index.html  # \u540e\u7aef1<br \/>\necho &#034;RS2 &#8211; 192.168.0.20&#034; &gt; \/usr\/share\/nginx\/html\/index.html  # \u540e\u7aef2<br \/>\nsystemctl enable &#8211;now nginx <\/p>\n<p>2. \u540e\u7aef Nginx \u5f00\u542f Proxy Protocol<\/p>\n<p>\u4fee\u6539 Nginx \u914d\u7f6e\u6587\u4ef6\/etc\/nginx\/nginx.conf&#xff0c;\u8ba9\u76d1\u542c\u7aef\u53e3\u542f\u7528 Proxy Protocol&#xff1a;<\/p>\n<p>[root&#064;webserver1 ~]# vim \/etc\/nginx\/nginx.conf<br \/>\nserver {<br \/>\n    listen       80 proxy_protocol;  # \u5f00\u542f\u56db\u5c42Proxy Protocol<br \/>\n    listen       [::]:80;<br \/>\n    server_name  _;<br \/>\n    root         \/usr\/share\/nginx\/html;<br \/>\n    # \u5176\u4ed6\u57fa\u7840\u914d\u7f6e&#xff08;\u9519\u8bef\u9875\u3001\u9ed8\u8ba4\u914d\u7f6e\u7b49&#xff09;<br \/>\n}<br \/>\n# \u91cd\u542fNginx<br \/>\n[root&#064;webserver2 ~]# systemctl restart nginx <\/p>\n<p>\u4ec5\u5f00\u542f\u6b64\u914d\u7f6e\u540e&#xff0c;\u76f4\u63a5\u8bbf\u95ee\u4f1a\u62a5 502 \u9519\u8bef&#xff08;\u56e0 HAProxy \u5c1a\u672a\u9002\u914d\u56db\u5c42\u900f\u4f20&#xff09;&#xff0c;\u9700\u7ee7\u7eed\u914d\u7f6e HAProxy\u3002<\/p>\n<p>3. HAProxy \u56db\u5c42\u900f\u4f20\u914d\u7f6e<\/p>\n<p>\u4fee\u6539 HAProxy \u914d\u7f6e\u6587\u4ef6&#xff0c;\u5207\u6362\u4e3a TCP \u6a21\u5f0f\u5e76\u542f\u7528send-proxy&#xff1a;<\/p>\n<p>#\u8bbe\u5b9ahaproxy\u8bbf\u95ee4\u5c42<br \/>\n[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten webcluster<br \/>\n    bind        *:80<br \/>\n    mode        tcp#\u5207\u6362\u4e3a\u56db\u5c42TCP\u6a21\u5f0f<br \/>\n    balance     roundrobin<br \/>\n    # \u7ed9\u540e\u7aef\u670d\u52a1\u5668\u6dfb\u52a0send-proxy\u53c2\u6570&#xff0c;\u4f20\u9012Proxy Protocol\u4fe1\u606f<br \/>\n    server haha 192.168.0.10:80 send-proxy check inter 3s fall 3 rise 5 weight 1<br \/>\n    server hehe 192.168.0.20:80 send-proxy check inter 3s fall 3 rise 5 weight 1<\/p>\n<p># \u91cd\u542fHAProxy\u751f\u6548<br \/>\n[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<ul>\n<li>\n<p>mode tcp&#xff1a;HAProxy \u5de5\u4f5c\u5728\u56db\u5c42 TCP \u6a21\u5f0f&#xff1b;<\/p>\n<\/li>\n<li>\n<p>send-proxy&#xff1a;HAProxy \u5411\u540e\u7aef Nginx \u53d1\u9001 Proxy Protocol \u534f\u8bae\u5305&#xff0c;\u5305\u542b\u5ba2\u6237\u7aef\u771f\u5b9e IP\u3002<\/p>\n<\/li>\n<\/ul>\n<p>4. \u540e\u7aef Nginx \u91c7\u96c6\u771f\u5b9e IP<\/p>\n<p>\u4fee\u6539 Nginx \u65e5\u5fd7\u683c\u5f0f&#xff0c;\u63d0\u53d6$proxy_protocol_addr&#xff08;Proxy Protocol \u4f20\u9012\u7684\u5ba2\u6237\u7aef IP&#xff09;&#xff1a;<\/p>\n<p>#\u8bbe\u7f6e4\u5c42ip\u900f\u4f20<br \/>\n[root&#064;webserver1&amp;2 ~]# vim \/etc\/nginx\/nginx.conf<\/p>\n<p>    log_format  main  &#039;$remote_addr &#8211; $remote_user [$time_local] &#034;$request&#034; &#039;<br \/>\n                      &#039;&#034;$proxy_protocol_addr&#034;&#039;#\u91c7\u96c6\u56db\u5c42\u900f\u4f20\u7684\u5ba2\u6237\u7aef\u771f\u5b9eIP<br \/>\n                      &#039;$status $body_bytes_sent &#034;$http_referer&#034; &#039;<br \/>\n                      &#039;&#034;$http_user_agent&#034; &#034;$http_x_forwarded_for&#034;&#039;;<\/p>\n<p># \u91cd\u542fNginx<br \/>\n[root&#064;webserver1&amp;2 ~]# systemctl restart nginx.service <\/p>\n<p>5. \u6548\u679c\u9a8c\u8bc1<\/p>\n<p>\u5ba2\u6237\u7aef\u8bbf\u95ee HAProxy \u5730\u5740\u540e&#xff0c;\u540e\u7aef Nginx \u65e5\u5fd7\u53ef\u770b\u5230\u5ba2\u6237\u7aef\u771f\u5b9e IP&#xff1a;<\/p>\n<p>[root&#064;webserver1 ~]# cat \/var\/log\/nginx\/access.log<br \/>\n192.168.0.100 &#8211; &#8211; [26\/Jan\/2026:10:52:40 &#043;0800] &#034;GET \/ HTTP\/1.1&#034; &#034;172.25.254.1&#034;200 19 &#034;-&#034; &#034;curl\/7.65.0&#034; &#034;-&#034;<br \/>\n192.168.0.100 &#8211; &#8211; [26\/Jan\/2026:10:53:49 &#043;0800] &#034;GET \/ HTTP\/1.1&#034;  &#034;172.25.254.1&#034;200 19 &#034;-&#034; &#034;curl\/7.65.0&#034; &#034;-&#034;<br \/>\n192.168.0.100 &#8211; &#8211; [26\/Jan\/2026:10:53:50 &#043;0800] &#034;GET \/ HTTP\/1.1&#034;  &#034;172.25.254.1&#034;200 19 &#034;-&#034; &#034;curl\/7.65.0&#034; &#034;-&#034;<br \/>\n192.168.0.100 &#8211; &#8211; [26\/Jan\/2026:10:53:50 &#043;0800] &#034;GET \/ HTTP\/1.1&#034;  &#034;172.25.254.1&#034;200 19 &#034;-&#034; &#034;curl\/7.65.0&#034; &#034;-&#034; <\/p>\n<h4 id=\"4.3%20%E6%A0%B8%E5%BF%83%E5%8C%BA%E5%88%AB%E6%80%BB%E7%BB%93\">4.3 \u6838\u5fc3\u533a\u522b\u603b\u7ed3<\/h4>\n<table>\n<tr>\u7ef4\u5ea6\u4e03\u5c42 IP \u900f\u4f20&#xff08;HTTP&#xff09;\u56db\u5c42 IP \u900f\u4f20&#xff08;TCP&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>\u4f9d\u8d56\u534f\u8bae<\/td>\n<td>HTTP \u534f\u8bae&#xff08;X-Forwarded-For\u5934&#xff09;<\/td>\n<td>Proxy Protocol&#xff08;TCP \u5c42\u534f\u8bae&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>HAProxy \u6a21\u5f0f<\/td>\n<td>mode http<\/td>\n<td>mode tcp<\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u914d\u7f6e<\/td>\n<td>option forwardfor<\/td>\n<td>\u540e\u7aef\u670d\u52a1\u5668\u914d\u7f6esend-proxy<\/td>\n<\/tr>\n<tr>\n<td>\u540e\u7aef\u91c7\u96c6\u5b57\u6bb5<\/td>\n<td>X-Forwarded-For\u8bf7\u6c42\u5934<\/td>\n<td>$proxy_protocol_addr&#xff08;Proxy Protocol \u5730\u5740&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>HTTP\/HTTPS \u4e1a\u52a1<\/td>\n<td>\u6240\u6709 TCP \u5c42\u4e1a\u52a1&#xff08;\u5982 Nginx\u3001MySQL\u3001Redis&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5173\u952e\u6ce8\u610f\u4e8b\u9879<\/p>\n<li>\n<p>\u4e03\u5c42\u900f\u4f20\u4ec5\u9002\u7528\u4e8e HTTP\/HTTPS&#xff0c;\u56db\u5c42\u900f\u4f20\u662f\u901a\u7528\u7684 TCP \u5c42\u65b9\u6848&#xff0c;\u9002\u7528\u8303\u56f4\u66f4\u5e7f&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u540e\u7aef\u670d\u52a1\u5668\u5fc5\u987b\u9002\u914d\u5bf9\u5e94\u7684\u900f\u4f20\u65b9\u5f0f&#xff08;\u5982 Apache\/Nginx \u914d\u7f6e\u65e5\u5fd7\u91c7\u96c6\u5b57\u6bb5\u3001\u5f00\u542f Proxy Protocol&#xff09;&#xff0c;\u5426\u5219\u65e0\u6cd5\u83b7\u53d6\u771f\u5b9e IP&#xff1b;<\/p>\n<\/li>\n<li>\n<p>send-proxy\u548cproxy_protocol\u9700\u6210\u5bf9\u914d\u7f6e&#xff08;HAProxy \u4fa7 send-proxy &#043; \u540e\u7aef\u4fa7 proxy_protocol&#xff09;&#xff0c;\u5426\u5219\u4f1a\u51fa\u73b0 502 \u7b49\u9519\u8bef\u3002<\/p>\n<\/li>\n<hr \/>\n<h2 id=\"%E5%85%AD%E3%80%81%C2%A0%E8%BF%90%E7%BB%B4%E4%B8%8E%E6%89%A9%E5%B1%95\">\u516d\u3001\u00a0\u8fd0\u7ef4\u4e0e\u6269\u5c55<\/h2>\n<h3 id=\"1.%C2%A0HAProxy%20%E7%8A%B6%E6%80%81%E9%A1%B5%E7%9B%91%E6%8E%A7\">1.\u00a0HAProxy \u72b6\u6001\u9875\u76d1\u63a7<\/h3>\n<p>HAProxy \u72b6\u6001\u9875&#xff08;stats page&#xff09;\u662f\u7528\u4e8e\u76d1\u63a7 HAProxy \u8fd0\u884c\u72b6\u6001\u7684\u91cd\u8981\u529f\u80fd&#xff0c;\u53ef\u76f4\u89c2\u67e5\u770b\u540e\u7aef\u8282\u70b9\u5065\u5eb7\u72b6\u6001\u3001\u6d41\u91cf\u8f6c\u53d1\u3001\u8fde\u63a5\u6570\u7b49\u6838\u5fc3\u6307\u6807\u3002<\/p>\n<h4 id=\"1.1%C2%A0%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE%E6%AD%A5%E9%AA%A4\">1.1\u00a0\u57fa\u7840\u914d\u7f6e\u6b65\u9aa4<\/h4>\n<p>1.\u00a0\u7f16\u8f91 HAProxy \u4e3b\u914d\u7f6e\u6587\u4ef6<\/p>\n<p>\u914d\u7f6e\u6587\u4ef6\u8def\u5f84\u4e3a \/etc\/haproxy\/haproxy.cfg&#xff0c;\u901a\u8fc7 listen \u5757\u5b9a\u4e49\u72b6\u6001\u9875\u76d1\u542c\u89c4\u5219&#xff1a;<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten stats          # \u5b9a\u4e49stats\u76d1\u542c\u5757\u540d\u79f0<br \/>\n    mode        http  # \u72b6\u6001\u9875\u57fa\u4e8eHTTP\u534f\u8bae<br \/>\n    bind 0.0.0.0:4321 # \u76d1\u542c\u6240\u6709\u7f51\u5361\u76844321\u7aef\u53e3&#xff08;\u53ef\u81ea\u5b9a\u4e49\u7aef\u53e3&#xff09;<br \/>\n    stats       enable # \u542f\u7528\u72b6\u6001\u9875\u529f\u80fd<br \/>\n    log         global # \u7ee7\u627f\u5168\u5c40\u65e5\u5fd7\u914d\u7f6e<br \/>\n    stats uri   \/status # \u8bbf\u95ee\u72b6\u6001\u9875\u7684URI\u8def\u5f84&#xff08;\u793a\u4f8b&#xff1a;http:\/\/IP:4321\/status&#xff09;<br \/>\n    stats auth  lee:lee # \u72b6\u6001\u9875\u8bbf\u95ee\u8ba4\u8bc1&#xff08;\u8d26\u53f7&#xff1a;lee&#xff0c;\u5bc6\u7801&#xff1a;lee&#xff09; <\/p>\n<p>2. \u91cd\u542f HAProxy \u670d\u52a1\u4f7f\u914d\u7f6e\u751f\u6548<\/p>\n<p>systemctl restart haproxy.service <\/p>\n<p>3. \u767b\u9646\u6d4b\u8bd5<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"383\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143248-69a1ab10a890d.png\" width=\"1188\" \/><\/p>\n<h4 id=\"1.2%C2%A0%E5%8A%9F%E8%83%BD%E6%89%A9%E5%B1%95%EF%BC%9A%E5%BC%80%E5%90%AF%E8%87%AA%E5%8A%A8%E5%88%B7%E6%96%B0\">1.2\u00a0\u529f\u80fd\u6269\u5c55&#xff1a;\u5f00\u542f\u81ea\u52a8\u5237\u65b0<\/h4>\n<p>\u9ed8\u8ba4\u72b6\u6001\u9875\u9700\u624b\u52a8\u5237\u65b0&#xff0c;\u53ef\u914d\u7f6e\u81ea\u52a8\u5237\u65b0\u95f4\u9694&#xff08;\u5355\u4f4d&#xff1a;\u79d2&#xff09;&#xff0c;\u5b9e\u65f6\u76d1\u63a7\u8282\u70b9\u72b6\u6001&#xff1a;<\/p>\n<p>1. \u4fee\u6539\u914d\u7f6e\u6587\u4ef6&#xff0c;\u65b0\u589e stats refresh \u9879&#xff1a;<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten stats<br \/>\n    mode        http<br \/>\n    bind 0.0.0.0:4321<br \/>\n    stats       enable<br \/>\n    log         global<br \/>\n    stats       refresh   1  # \u914d\u7f6e1\u79d2\u81ea\u52a8\u5237\u65b0&#xff08;\u53ef\u81ea\u5b9a\u4e49\u95f4\u9694&#xff09;<br \/>\n    stats uri   \/status<br \/>\n    stats auth  lee:lee <\/p>\n<p>2.\u00a0\u91cd\u542f\u670d\u52a1<\/p>\n<p>systemctl restart haproxy.service <\/p>\n<p>3.\u00a0\u72b6\u6001\u76d1\u63a7\u573a\u666f<\/p>\n<ul>\n<li>\n<p>\u5b9e\u65f6\u67e5\u770b\u540e\u7aef\u8282\u70b9\u5728\u7ebf \/ \u79bb\u7ebf\u72b6\u6001&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6a21\u62df\u8bbe\u5907\u4e0b\u7ebf\u65f6&#xff0c;\u72b6\u6001\u9875\u4f1a\u81ea\u52a8\u5237\u65b0\u5e76\u5c55\u793a\u8282\u70b9\u5f02\u5e38\u72b6\u6001&#xff08;\u7ed3\u5408\u81ea\u52a8\u5237\u65b0\u914d\u7f6e&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143248-69a1ab10db8e5.png\" \/><\/p>\n<p>\u6838\u5fc3\u4f5c\u7528<\/p>\n<ul>\n<li>\n<p>\u53ef\u89c6\u5316\u76d1\u63a7 HAProxy \u8fd0\u884c\u72b6\u6001\u3001\u540e\u7aef\u670d\u52a1\u5668\u5065\u5eb7\u5ea6&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5feb\u901f\u5b9a\u4f4d\u8282\u70b9\u6545\u969c&#xff0c;\u8f85\u52a9\u8fd0\u7ef4\u6392\u969c&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u81ea\u5b9a\u4e49\u5237\u65b0\u9891\u7387&#xff0c;\u6ee1\u8db3\u5b9e\u65f6\u76d1\u63a7\u9700\u6c42\u3002<\/p>\n<\/li>\n<\/ul>\n<h3 id=\"2.%C2%A0Socat%20%E7%83%AD%E6%9B%B4%E6%96%B0%E5%B7%A5%E5%85%B7\">2.\u00a0Socat \u70ed\u66f4\u65b0\u5de5\u5177<\/h3>\n<p>Socat \u672c\u8eab\u662f\u4e00\u6b3e\u591a\u529f\u80fd\u7684\u7f51\u7edc\u5de5\u5177&#xff0c;\u800c\u7ed3\u5408 HAProxy \u573a\u666f\u7684 \u201cSocat \u70ed\u66f4\u65b0\u5de5\u5177\u201d&#xff0c;\u6838\u5fc3\u662f\u5229\u7528 Socat \u4e0e HAProxy \u7684 stats socket \u4ea4\u4e92&#xff0c;\u5b9e\u73b0 HAProxy \u914d\u7f6e \/ \u8282\u70b9\u6743\u91cd\u7684\u70ed\u66f4\u65b0&#xff08;\u5373\u4e0d\u505c\u6b62 HAProxy \u670d\u52a1\u7684\u524d\u63d0\u4e0b\u5b8c\u6210\u914d\u7f6e\u8c03\u6574&#xff09;&#xff0c;\u907f\u514d\u670d\u52a1\u4e2d\u65ad\u3002<\/p>\n<h4 id=\"2.1%C2%A0%E6%A0%B8%E5%BF%83%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF\">2.1\u00a0\u6838\u5fc3\u5e94\u7528\u573a\u666f<\/h4>\n<p>\u57fa\u4e8e\u6587\u6863\u4e2d\u7684\u5b9e\u64cd&#xff0c;\u5176\u6838\u5fc3\u4f5c\u7528\u662f\u5bf9 HAProxy \u540e\u7aef\u8282\u70b9\u8fdb\u884c<span style=\"color:#fe2c24\">\u6743\u91cd\u8c03\u6574<\/span>&#xff08;\u70ed\u66f4\u65b0&#xff09;&#xff0c;\u65e0\u9700\u91cd\u542f HAProxy \u670d\u52a1&#xff0c;\u4fdd\u8bc1\u4e1a\u52a1\u8fde\u7eed\u6027\u3002<\/p>\n<h4 id=\"2.2%C2%A0%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8%E5%89%8D%E6%8F%90\">2.2\u00a0\u57fa\u7840\u4f7f\u7528\u524d\u63d0<\/h4>\n<p>1.\u00a0\u5b89\u88c5 Socat<\/p>\n<p>[root&#064;haproxy ~]# dnf install socat -y<br \/>\n[root&#064;haproxy ~]# socat  -h    #\u9a8c\u8bc1\u5b89\u88c5\u6210\u529f <\/p>\n<p>2.\u00a0HAProxy \u914d\u7f6e\u51c6\u5907<\/p>\n<p>HAProxy \u9700\u5f00\u542f stats socket&#xff08;\u7edf\u8ba1\u5957\u63a5\u5b57&#xff09;&#xff0c;\u5e76\u8d4b\u4e88\u8db3\u591f\u6743\u9650&#xff08;\u5426\u5219\u65e0\u6cd5\u4fee\u6539\u914d\u7f6e&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\u7f16\u8f91 HAProxy \u914d\u7f6e\u6587\u4ef6 \/etc\/haproxy\/haproxy.cfg&#xff0c;\u6dfb\u52a0 \/ \u4fee\u6539 stats socket \u914d\u7f6e<\/li>\n<\/ul>\n<p>#\u5bf9socket\u8fdb\u884c\u6388\u6743<br \/>\n[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nstats socket \/var\/lib\/haproxy\/stats mode 600 level admin <\/p>\n<ul>\n<li>\u6e05\u7406\u65e7\u7684 stats \u6587\u4ef6\u5e76\u91cd\u542f HAProxy \u4f7f\u914d\u7f6e\u751f\u6548&#xff1a;<\/li>\n<\/ul>\n<p>[root&#064;haproxy ~]# rm -rf \/var\/lib\/haproxy\/*<br \/>\n[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<p>\u751f\u6548\u540e\u4f1a\u751f\u6210\u6743\u9650\u4e3a srw&#8212;&#8212;- \u7684 \/var\/lib\/haproxy\/stats \u5957\u63a5\u5b57\u6587\u4ef6&#xff0c;\u4f5c\u4e3a Socat \u4e0e HAProxy \u4ea4\u4e92\u7684\u901a\u9053\u3002<\/p>\n<h4 id=\"2.3%C2%A0%E6%A0%B8%E5%BF%83%E6%93%8D%E4%BD%9C%EF%BC%88%E4%B8%8E%20HAProxy%20%E4%BA%A4%E4%BA%92%EF%BC%89\">2.3\u00a0\u6838\u5fc3\u64cd\u4f5c&#xff08;\u4e0e HAProxy \u4ea4\u4e92&#xff09;<\/h4>\n<p>\u901a\u8fc7 Socat \u5411 HAProxy \u7684 stats socket \u53d1\u9001\u6307\u4ee4&#xff0c;\u5b9e\u73b0 \u201c\u67e5\u770b\u4fe1\u606f\u201d \u548c \u201c\u4fee\u6539\u914d\u7f6e\u201d \u4e24\u7c7b\u6838\u5fc3\u64cd\u4f5c&#xff1a;<\/p>\n<p>1.\u00a0\u67e5\u770b HAProxy \u540e\u7aef\u8282\u70b9\u4fe1\u606f<\/p>\n<ul>\n<li>\n<p>\u67e5\u770b\u6240\u6709\u540e\u7aef\u670d\u52a1\u5668\u72b6\u6001&#xff08;\u5305\u542b\u8282\u70b9 ID\u3001\u5730\u5740\u3001\u6743\u91cd\u3001\u8fd0\u884c\u72b6\u6001\u7b49&#xff09;&#xff1a;<\/p>\n<\/li>\n<\/ul>\n<p>#\u6267\u884c\u6743\u91cd\u66f4\u6539<br \/>\n[root&#064;haproxy ~]# echo &#034;show servers state&#034; | socat stdio \/var\/lib\/haproxy\/stats<\/p>\n<ul>\n<li>\n<p>\u67e5\u770b\u6307\u5b9a\u540e\u7aef\u8282\u70b9\u7684\u6743\u91cd&#xff1a;<\/p>\n<\/li>\n<\/ul>\n<p>[root&#064;haproxy ~]# echo &#034;get  weight webcluster\/hehe&#034; | socat  stdio \/var\/lib\/haproxy\/stats<br \/>\n1 (initial 1) <\/p>\n<p>2. \u70ed\u66f4\u65b0&#xff08;\u4fee\u6539&#xff09;HAProxy \u8282\u70b9\u6743\u91cd<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u76f4\u63a5\u4fee\u6539\u6743\u91cd&#xff08;\u65e0\u6743\u9650\u65f6\u4f1a\u62a5\u9519&#xff09;&#xff1a;<\/p>\n<p># \u683c\u5f0f&#xff1a;echo &#034;set weight \u540e\u7aef\u96c6\u7fa4\u540d\/\u8282\u70b9\u540d \u65b0\u6743\u91cd&#034; | socat stdio \/var\/lib\/haproxy\/stats<br \/>\n[root&#064;haproxy ~]# echo &#034;set  weight  webcluster\/hehe 4 &#034; | socat stdio \/var\/lib\/haproxy\/stats <\/p>\n<ul>\n<li>\n<p>\u82e5\u63d0\u793a Permission denied&#xff1a;\u9700\u68c0\u67e5 HAProxy \u7684 stats socket \u914d\u7f6e\u662f\u5426\u5305\u542b level admin&#xff08;\u7ba1\u7406\u5458\u6743\u9650&#xff09;&#xff0c;\u5e76\u91cd\u542f HAProxy\u3002<\/p>\n<\/li>\n<li>\n<p>\u4fee\u6539\u540e\u53ef\u518d\u6b21\u6267\u884c get weight \u6307\u4ee4\u9a8c\u8bc1\u6743\u91cd\u662f\u5426\u751f\u6548\u3002<\/p>\n<\/li>\n<\/ul>\n<h4 id=\"2.4%C2%A0%E6%95%88%E6%9E%9C%E9%AA%8C%E8%AF%81\">2.4\u00a0\u6548\u679c\u9a8c\u8bc1<\/h4>\n<p>\u4fee\u6539\u8282\u70b9\u6743\u91cd\u540e&#xff0c;\u53ef\u901a\u8fc7\u591a\u6b21\u8bbf\u95ee HAProxy \u524d\u7aef\u5730\u5740&#xff0c;\u9a8c\u8bc1\u6d41\u91cf\u5206\u53d1\u6bd4\u4f8b\u662f\u5426\u7b26\u5408\u6743\u91cd\u914d\u7f6e&#xff1a;<\/p>\n<p>#\u6d4b\u8bd5<br \/>\n[Administrator.DESKTOP-VJ307M3] \u27a4 for i in {1..10}; do curl 172.25.254.100; done<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver1 &#8211; 192.168.0.10<br \/>\nwebserver2 &#8211; 192.168.0.20<br \/>\nwebserver2 &#8211; 192.168.0.20 <\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143249-69a1ab1150f39.png\" \/><\/p>\n<p>\u6838\u5fc3\u4ef7\u503c<\/p>\n<li>\n<p>\u65e0\u505c\u673a\u66f4\u65b0&#xff1a;\u8c03\u6574 HAProxy \u540e\u7aef\u8282\u70b9\u6743\u91cd\u65f6&#xff0c;\u65e0\u9700\u91cd\u542f HAProxy \u670d\u52a1&#xff0c;\u907f\u514d\u4e1a\u52a1\u4e2d\u65ad&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u7075\u6d3b\u8c03\u4f18&#xff1a;\u53ef\u6839\u636e\u670d\u52a1\u5668\u8d1f\u8f7d\u3001\u4e1a\u52a1\u9700\u6c42\u52a8\u6001\u8c03\u6574\u8282\u70b9\u6743\u91cd&#xff0c;\u4f18\u5316\u6d41\u91cf\u5206\u53d1&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8f7b\u91cf\u4ea4\u4e92&#xff1a;\u501f\u52a9 Socat \u5de5\u5177\u5b9e\u73b0\u4e0e HAProxy \u5957\u63a5\u5b57\u7684\u7b80\u5355\u4ea4\u4e92&#xff0c;\u64cd\u4f5c\u6210\u672c\u4f4e\u3002<\/p>\n<\/li>\n<h3 id=\"3.%C2%A0%E8%87%AA%E5%AE%9A%E4%B9%89%20HAProxy%20%E9%94%99%E8%AF%AF%E7%95%8C%E9%9D%A2\">3.\u00a0\u81ea\u5b9a\u4e49 HAProxy \u9519\u8bef\u754c\u9762<\/h3>\n<p>HAProxy \u63d0\u4f9b\u4e86\u591a\u79cd\u81ea\u5b9a\u4e49\u9519\u8bef\u754c\u9762\u7684\u65b9\u5f0f&#xff0c;\u6838\u5fc3\u7528\u4e8e\u540e\u7aef\u670d\u52a1\u4e0d\u53ef\u7528\u65f6&#xff0c;\u66ff\u6362\u9ed8\u8ba4\u7684\u9519\u8bef\u63d0\u793a&#xff0c;\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u6216\u5b9e\u73b0\u7279\u5b9a\u7684\u6545\u969c\u5f15\u5bfc\u903b\u8f91&#xff0c;\u4e3b\u8981\u5305\u542b<span style=\"color:#fe2c24\">sorryserver&#xff08;\u5907\u7528\u670d\u52a1\u5668&#xff09;\u3001\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762\u6587\u4ef6\u3001\u91cd\u5b9a\u5411\u5230\u6307\u5b9a\u7f51\u7ad9<\/span> \u4e09\u79cd\u573a\u666f\u3002<\/p>\n<h4 id=\"3.1%C2%A0%E5%9F%BA%E7%A1%80%E6%96%B9%E6%A1%88%EF%BC%9Asorryserver%EF%BC%88%E5%A4%87%E7%94%A8%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%89\">3.1\u00a0\u57fa\u7840\u65b9\u6848&#xff1a;sorryserver&#xff08;\u5907\u7528\u670d\u52a1\u5668&#xff09;<\/h4>\n<p>\u5f53\u6b63\u5e38\u4e1a\u52a1\u540e\u7aef\u670d\u52a1\u5668\u5168\u90e8\u5b95\u673a\u65f6&#xff0c;\u5c06\u8bf7\u6c42\u5b9a\u5411\u5230\u9884\u5148\u914d\u7f6e\u7684\u5907\u7528\u670d\u52a1\u5668&#xff08;sorryserver&#xff09;&#xff0c;\u662f\u6545\u969c\u65f6\u7684 \u201c\u515c\u5e95\u8bbf\u95ee\u8282\u70b9\u201d&#xff0c;\u800c\u975e\u76f4\u63a5\u8fd4\u56de\u9519\u8bef\u9875\u9762\u3002<\/p>\n<p>\u3010\u914d\u7f6e\u6b65\u9aa4\u3011<\/p>\n<p>1. \u90e8\u7f72\u5907\u7528\u670d\u52a1\u5668&#xff08;\u53ef\u590d\u7528 HAProxy \u4e3b\u673a&#xff09;&#xff1a;\u5b89\u88c5 Web \u670d\u52a1&#xff08;\u5982 Apache&#xff09;&#xff0c;\u4fee\u6539\u76d1\u542c\u7aef\u53e3&#xff08;\u793a\u4f8b\u4e3a 8080&#xff09;&#xff0c;\u5e76\u914d\u7f6e\u81ea\u5b9a\u4e49\u63d0\u793a\u5185\u5bb9&#xff1b;<\/p>\n<p>#\u5728\u65b0\u4e3b\u673a\u4e2d\u5b89\u88c5apache&#xff08;\u53ef\u4ee5\u7528haproxy\u4e3b\u673a\u4ee3\u66ff&#xff09;<br \/>\n[root&#064;haproxy ~]# dnf install httpd -y<br \/>\n[root&#064;haproxy ~]# vim \/etc\/httpd\/conf\/httpd.conf<br \/>\n47 Listen 8080<br \/>\n[root&#064;haproxy ~]# systemctl enable &#8211;now httpd<br \/>\nCreated symlink \/etc\/systemd\/system\/multi-user.target.wants\/httpd.service \u2192 \/usr\/lib\/systemd\/system\/httpd.service.<\/p>\n<p>[root&#064;haproxy ~]# echo &#034;\u674e\u54e5\u5728&#xff0c;\u6ca1\u610f\u5916&#034; &gt; \/var\/www\/html\/index.html <\/p>\n<p>2. \u4fee\u6539 HAProxy \u914d\u7f6e\u6587\u4ef6&#xff08;\/etc\/haproxy\/haproxy.cfg&#xff09;&#xff0c;\u5728\u540e\u7aef\u96c6\u7fa4\u4e2d\u6dfb\u52a0 backup \u6807\u8bc6\u7684\u5907\u7528\u670d\u52a1\u5668&#xff1a;<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\nlisten webcluster<br \/>\n    bind        *:80<br \/>\n    mode        http<br \/>\n    balance     roundrobin<br \/>\n    server haha 192.168.0.10:80  check inter 3s fall 3 rise 5 weight 1<br \/>\n    server hehe 192.168.0.20:80  check inter 3s fall 3 rise 5 weight 1<br \/>\n    server wuwu 192.168.0.100:8080  backup#sorryserver <\/p>\n<p>3.\u00a0\u91cd\u542f HAProxy \u751f\u6548&#xff0c;\u5f53\u6240\u6709\u4e1a\u52a1\u670d\u52a1\u5668\u5b95\u673a\u65f6&#xff0c;\u8bf7\u6c42\u4f1a\u88ab\u8f6c\u53d1\u5230\u5907\u7528\u670d\u52a1\u5668\u3002<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<p>4. \u6d4b\u8bd5<\/p>\n<p>[Administrator.DESKTOP-VJ307M3] \u27a4 curl 172.25.254.100<br \/>\nwebserver1 &#8211; 192.168.0.10<\/p>\n<p>[Administrator.DESKTOP-VJ307M3] \u27a4 curl 172.25.254.100<br \/>\nwebserver2 &#8211; 192.168.0.20<\/p>\n<p>#\u5173\u95ed\u4e24\u53f0\u6b63\u5e38\u7684\u4e1a\u52a1\u4e3b\u673a<br \/>\n[root&#064;webserver1&#043;2 ~]# systemctl stop httpd<\/p>\n<p>[Administrator.DESKTOP-VJ307M3] \u27a4 curl 172.25.254.100<br \/>\n\u674e\u54e5\u5728&#xff0c;\u6ca1\u610f\u5916 <\/p>\n<h4 id=\"3.2%C2%A0%E6%A0%B8%E5%BF%83%E6%96%B9%E6%A1%88%EF%BC%9A%E8%87%AA%E5%AE%9A%E4%B9%89%E9%94%99%E8%AF%AF%E9%A1%B5%E9%9D%A2%E6%96%87%E4%BB%B6\">3.2\u00a0\u6838\u5fc3\u65b9\u6848&#xff1a;\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762\u6587\u4ef6<\/h4>\n<p>\u5f53<span style=\"color:#9c8ec1\">\u5305\u62ec\u5907\u7528\u670d\u52a1\u5668\u5728\u5185\u7684\u6240\u6709\u540e\u7aef\u670d\u52a1\u90fd\u5b95\u673a<\/span>\u65f6&#xff0c;HAProxy \u4f1a\u8fd4\u56de\u9ed8\u8ba4\u9519\u8bef\u9875\u9762&#xff08;\u5982 503&#xff09;&#xff0c;\u53ef\u901a\u8fc7\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762\u6587\u4ef6\u66ff\u6362\u9ed8\u8ba4\u5185\u5bb9\u3002<\/p>\n<p>\u3010\u914d\u7f6e\u6b65\u9aa4\u3011<\/p>\n<p>1. \u521b\u5efa\u9519\u8bef\u9875\u9762\u5b58\u653e\u76ee\u5f55&#xff0c;\u5e76\u7f16\u5199\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762\u6587\u4ef6&#xff08;\u9700\u9075\u5faa HTTP \u683c\u5f0f&#xff09;&#xff1a;<\/p>\n<p>[root&#064;haproxy ~]# mkdir \/errorpage\/html\/ -p<br \/>\n[root&#064;haproxy ~]# vim \/errorpage\/html\/503.http<br \/>\nHTTP\/1.0 503 Service Unavailable<br \/>\nCache-Control: no-cache<br \/>\nConnection: close<br \/>\nContent-Type: text\/html;charset&#061;UTF-8<\/p>\n<p>&lt;html&gt;&lt;body&gt;&lt;h1&gt;\u4ec0\u4e48\u52a8\u7269\u751f\u6c14\u6700\u5b89\u9759&lt;\/h1&gt;<br \/>\n\u5927\u7329\u7329&#xff01;&#xff01;<br \/>\n&lt;\/body&gt;&lt;\/html&gt; <\/p>\n<p>2.\u00a0\u4fee\u6539 HAProxy \u7684defaults\u6bb5&#xff0c;\u6dfb\u52a0errorfile\u6307\u4ee4\u5173\u8054\u9519\u8bef\u7801\u4e0e\u81ea\u5b9a\u4e49\u6587\u4ef6&#xff1a;<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfg<br \/>\ndefaults<br \/>\n    mode                    http<br \/>\n    # \u5176\u4ed6\u9ed8\u8ba4\u914d\u7f6e&#xff08;\u65e5\u5fd7\u3001\u8d85\u65f6\u3001\u8fde\u63a5\u6570\u7b49&#xff09;<br \/>\n    errorfile 503           \/errorpage\/html\/503.http  # 503\u9519\u8bef\u5173\u8054\u81ea\u5b9a\u4e49\u6587\u4ef6 <\/p>\n<p>3. \u91cd\u542f HAProxy&#xff0c;\u5f53\u89e6\u53d1 503 \u9519\u8bef\u65f6&#xff0c;\u4f1a\u8fd4\u56de\u81ea\u5b9a\u4e49\u7684\u9875\u9762\u5185\u5bb9\u3002<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<p>4. \u6d4b\u8bd5<\/p>\n<p>[Administrator.DESKTOP-VJ307M3] \u27a4 curl 172.25.254.100<br \/>\n&lt;html&gt;&lt;body&gt;&lt;h1&gt;\u4ec0\u4e48\u52a8\u7269\u751f\u6c14\u6700\u5b89\u9759&lt;\/h1&gt;<br \/>\n\u5927\u7329\u7329&#xff01;&#xff01;<br \/>\n&lt;\/body&gt;&lt;\/html&gt; <\/p>\n<h4 id=\"3.3%C2%A0%E6%89%A9%E5%B1%95%E6%96%B9%E6%A1%88%EF%BC%9A%E9%87%8D%E5%AE%9A%E5%90%91%E9%94%99%E8%AF%AF%E5%88%B0%E6%8C%87%E5%AE%9A%E7%BD%91%E7%AB%99\">3.3\u00a0\u6269\u5c55\u65b9\u6848&#xff1a;\u91cd\u5b9a\u5411\u9519\u8bef\u5230\u6307\u5b9a\u7f51\u7ad9<\/h4>\n<p>\u9664\u4e86\u8fd4\u56de\u672c\u5730\u81ea\u5b9a\u4e49\u9875\u9762&#xff0c;\u8fd8\u53ef\u5c06\u9519\u8bef\u8bf7\u6c42\u76f4\u63a5\u91cd\u5b9a\u5411\u5230\u5916\u90e8\u6307\u5b9a\u7f51\u7ad9&#xff08;\u5982\u767e\u5ea6&#xff09;\u3002<\/p>\n<p>\u3010\u914d\u7f6e\u6b65\u9aa4\u3011<\/p>\n<p>1. \u4fee\u6539 HAProxy \u7684defaults\u6bb5&#xff0c;\u5c06errorfile\u66ff\u6362\u4e3aerrorloc&#xff0c;\u6307\u5b9a\u91cd\u5b9a\u5411\u7684 URL&#xff1a;<\/p>\n<p>[root&#064;haproxy ~]# vim \/etc\/haproxy\/haproxy.cfgdefaults<br \/>\n    mode                    http<br \/>\n    # \u5176\u4ed6\u9ed8\u8ba4\u914d\u7f6e<br \/>\n    errorloc 503            http:\/\/www.baidu.com  # 503\u9519\u8bef\u91cd\u5b9a\u5411\u5230\u767e\u5ea6 <\/p>\n<p>2.\u00a0\u91cd\u542f HAProxy&#xff0c;\u5f53\u89e6\u53d1 503 \u9519\u8bef\u65f6&#xff0c;\u6d4f\u89c8\u5668\u4f1a\u76f4\u63a5\u8df3\u8f6c\u5230\u914d\u7f6e\u7684\u5916\u90e8\u7f51\u7ad9\u3002<\/p>\n<p>[root&#064;haproxy ~]# systemctl restart haproxy.service <\/p>\n<p>\u6838\u5fc3\u8bf4\u660e<\/p>\n<li>\n<p>\u4f18\u5148\u7ea7&#xff1a;sorryserver &gt; \u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762 \/ \u91cd\u5b9a\u5411&#xff0c;\u4ec5\u5f53\u5907\u7528\u670d\u52a1\u5668\u4e5f\u4e0d\u53ef\u7528\u65f6&#xff0c;\u624d\u4f1a\u89e6\u53d1\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762\u6216\u91cd\u5b9a\u5411&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u9519\u8bef\u7801&#xff1a;HAProxy \u652f\u6301\u4e3a\u4e0d\u540c\u9519\u8bef\u7801&#xff08;\u5982 403\u3001500\u3001503 \u7b49&#xff09;\u914d\u7f6e\u4e0d\u540c\u7684\u81ea\u5b9a\u4e49\u903b\u8f91&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u683c\u5f0f\u8981\u6c42&#xff1a;\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762\u6587\u4ef6&#xff08;.http&#xff09;\u5fc5\u987b\u5305\u542b\u5b8c\u6574\u7684 HTTP \u54cd\u5e94\u5934&#xff08;\u5982HTTP\/1.0 503 Service Unavailable&#xff09;&#xff0c;\u5426\u5219\u4f1a\u5bfc\u81f4 HAProxy \u89e3\u6790\u5931\u8d25\u3002<\/p>\n<\/li>\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001HAProxy \u662f\u4ec0\u4e48&#xff1f;\u4e3a\u4ec0\u4e48\u8981\u5b66\u5b83&#xff1f;<br \/>\nHAProxy  \u9ad8\u6027\u80fd\u3001\u5f00\u6e90\u7684\u8d1f\u8f7d\u5747\u8861  \u53cd\u5411\u4ee3\u7406\u8f6f\u4ef6<br \/>\n1. HAProxy \u662f\u4ec0\u4e48&#xff1f;<br \/>\n\u4e3b\u8981\u7528\u6765&#xff1a;<br \/>\n\u628a\u7528\u6237\u8bf7\u6c42\u5747\u5300\u5206\u7ed9\u591a\u53f0\u670d\u52a1\u5668\u505a\u6d41\u91cf\u5165\u53e3\u3001\u7f51\u5173\u3001\u9ad8\u53ef\u7528\u652f\u6301 HTTP\/HTTPS\/TCP \u7b49&#xff0c;\u6027\u80fd\u6781\u5f3a\u3001\u5f88\u7a33\u5b9a \u7b80\u5355\u7406\u89e3&#xff1a;\u4f60\u6709 10 \u53f0 Web \u670d\u52a1\u5668&#xff0c;\u7528\u6237\u4e0d\u77e5\u9053\u4e5f\u4e0d\u7528\u7ba1\u8fde\u54ea\u53f0&#xff0c;\u5168\u90fd\u5148\u8fde HAProxy&amp;#xff0<\/p>\n","protected":false},"author":2,"featured_media":78652,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2204,258,44],"topic":[],"class_list":["post-78665","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-haproxy","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>\u4ece\u5165\u95e8\u5230\u5b9e\u6218\uff1aHAProxy \u8d1f\u8f7d\u5747\u8861\u4e0e\u4ee3\u7406\u5168\u89e3\u6790 - \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\/78665.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece\u5165\u95e8\u5230\u5b9e\u6218\uff1aHAProxy \u8d1f\u8f7d\u5747\u8861\u4e0e\u4ee3\u7406\u5168\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001HAProxy \u662f\u4ec0\u4e48&#xff1f;\u4e3a\u4ec0\u4e48\u8981\u5b66\u5b83&#xff1f; HAProxy \u9ad8\u6027\u80fd\u3001\u5f00\u6e90\u7684\u8d1f\u8f7d\u5747\u8861 \u53cd\u5411\u4ee3\u7406\u8f6f\u4ef6 1. HAProxy \u662f\u4ec0\u4e48&#xff1f; \u4e3b\u8981\u7528\u6765&#xff1a; \u628a\u7528\u6237\u8bf7\u6c42\u5747\u5300\u5206\u7ed9\u591a\u53f0\u670d\u52a1\u5668\u505a\u6d41\u91cf\u5165\u53e3\u3001\u7f51\u5173\u3001\u9ad8\u53ef\u7528\u652f\u6301 HTTP\/HTTPS\/TCP \u7b49&#xff0c;\u6027\u80fd\u6781\u5f3a\u3001\u5f88\u7a33\u5b9a \u7b80\u5355\u7406\u89e3&#xff1a;\u4f60\u6709 10 \u53f0 Web \u670d\u52a1\u5668&#xff0c;\u7528\u6237\u4e0d\u77e5\u9053\u4e5f\u4e0d\u7528\u7ba1\u8fde\u54ea\u53f0&#xff0c;\u5168\u90fd\u5148\u8fde HAProxy&amp;#xff0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/78665.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-27T14:32:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143246-69a1ab0eecc0e.png\" \/>\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=\"28 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78665.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/78665.html\",\"name\":\"\u4ece\u5165\u95e8\u5230\u5b9e\u6218\uff1aHAProxy \u8d1f\u8f7d\u5747\u8861\u4e0e\u4ee3\u7406\u5168\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-27T14:32:50+00:00\",\"dateModified\":\"2026-02-27T14:32:50+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/78665.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/78665.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78665.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece\u5165\u95e8\u5230\u5b9e\u6218\uff1aHAProxy \u8d1f\u8f7d\u5747\u8861\u4e0e\u4ee3\u7406\u5168\u89e3\u6790\"}]},{\"@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":"\u4ece\u5165\u95e8\u5230\u5b9e\u6218\uff1aHAProxy \u8d1f\u8f7d\u5747\u8861\u4e0e\u4ee3\u7406\u5168\u89e3\u6790 - \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\/78665.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece\u5165\u95e8\u5230\u5b9e\u6218\uff1aHAProxy \u8d1f\u8f7d\u5747\u8861\u4e0e\u4ee3\u7406\u5168\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001HAProxy \u662f\u4ec0\u4e48&#xff1f;\u4e3a\u4ec0\u4e48\u8981\u5b66\u5b83&#xff1f; HAProxy \u9ad8\u6027\u80fd\u3001\u5f00\u6e90\u7684\u8d1f\u8f7d\u5747\u8861 \u53cd\u5411\u4ee3\u7406\u8f6f\u4ef6 1. HAProxy \u662f\u4ec0\u4e48&#xff1f; \u4e3b\u8981\u7528\u6765&#xff1a; \u628a\u7528\u6237\u8bf7\u6c42\u5747\u5300\u5206\u7ed9\u591a\u53f0\u670d\u52a1\u5668\u505a\u6d41\u91cf\u5165\u53e3\u3001\u7f51\u5173\u3001\u9ad8\u53ef\u7528\u652f\u6301 HTTP\/HTTPS\/TCP \u7b49&#xff0c;\u6027\u80fd\u6781\u5f3a\u3001\u5f88\u7a33\u5b9a \u7b80\u5355\u7406\u89e3&#xff1a;\u4f60\u6709 10 \u53f0 Web \u670d\u52a1\u5668&#xff0c;\u7528\u6237\u4e0d\u77e5\u9053\u4e5f\u4e0d\u7528\u7ba1\u8fde\u54ea\u53f0&#xff0c;\u5168\u90fd\u5148\u8fde HAProxy&amp;#xff0","og_url":"https:\/\/www.wsisp.com\/helps\/78665.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-27T14:32:50+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260227143246-69a1ab0eecc0e.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"28 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/78665.html","url":"https:\/\/www.wsisp.com\/helps\/78665.html","name":"\u4ece\u5165\u95e8\u5230\u5b9e\u6218\uff1aHAProxy \u8d1f\u8f7d\u5747\u8861\u4e0e\u4ee3\u7406\u5168\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-27T14:32:50+00:00","dateModified":"2026-02-27T14:32:50+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/78665.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/78665.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/78665.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece\u5165\u95e8\u5230\u5b9e\u6218\uff1aHAProxy \u8d1f\u8f7d\u5747\u8861\u4e0e\u4ee3\u7406\u5168\u89e3\u6790"}]},{"@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\/78665","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=78665"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78665\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/78652"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=78665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=78665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=78665"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=78665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}