4台Rocky 8.4 10.0.0.101,10.0.0.102,10.0.0.103 - Apache 10.0.0.104 - Haproxy ============================================= haproxy主配置global, default [21:10:45 root@localhost ~]#vim /etc/haproxy/haproxy.cfg global log 127.0.0.1 local2 info chroot /apps/haproxy pidfile /var/lib/haproxy/haproxy.pid maxconn 100000 user haproxy group haproxy daemon cpu-map auto:1/1-2 0-1 maxsslconn 10 maxconnrate 10 spread-checks 2 stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 header X-Forwarded-For option redispatch option http-keep-alive retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 ============ Haproxy其他配置 listen stats mode http bind 0.0.0.0:9999 stats enable log global stats uri /haproxy-status stats auth haadmin:123456 #listen web_port # mode http # bind 0.0.0.0:80 # log global # server web1 10.0.0.103:80 check inter 3000 fall 2 rise 5 # server web2 10.0.0.102:80 check inter 3000 fall 2 rise 5 frontend main bind 0.0.0.0:80 mode http # balance roundrobin log global option httplog # acl url_static path_beg -i /static /images /javascript /stylesheets # acl url_static path_end -i .jpg .gif .png .css .js # use_backend static if url_static acl url_static path_beg -i /text use_backend web2 if url_static default_backend web_servers backend web_servers # balance roundrobin mode http default-server inter 1000 weight 6 server web1 10.0.0.102:80 weight 2 check inter 3000 fall 2 rise 5 server web2 10.0.0.103:80 weight 1 check inter 3000 fall 2 rise 5 backend web2 mode http server web3 10.0.0.101:80 check inter 3000 fall 2 rise 5 ~ ~ ============ service文件 [21:21:16 root@localhost ~]#cat /lib/systemd/system/haproxy.service [Unit] Description=HAProxy Load Balancer After=network-online.target Wants=network-online.target [Service] ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/ -c -q ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/ -p /var/lib/haproxy/haproxy.pid ExecReload=/bin/kill -USR2 SuccessExitStatus=143 KillMode=mixed Type=notify LimitNOFILE=100000 [Install] WantedBy=multi-user.target ===================== # 权重分配,2:1访问 [21:18:28 root@localhost ~]#curl 10.0.0.104 10.0.0.102 test server [21:18:31 root@localhost ~]#curl 10.0.0.104 10.0.0.102 test server [21:18:31 root@localhost ~]#curl 10.0.0.104 10.0.0.103 test server ### acl 调用和IP透传 [21:19:23 root@localhost ~]#curl http://10.0.0.104/text/index.html 10.0.0.101 test2 server [21:13:14 root@localhost ~]#tail -f /var/log/httpd/access_log 10.0.0.104 - - [04/Oct/2021:21:09:49 +0800] "GET / HTTP/1.1" 200 23 "-" "curl/7.61.1" 10.0.0.104 - - [04/Oct/2021:21:09:53 +0800] "HEAD / HTTP/1.1" 200 - "-" "curl/7.61.1" 10.0.0.104 - - [04/Oct/2021:21:09:54 +0800] "HEAD / HTTP/1.1" 200 - "-" "curl/7.61.1" 10.0.0.104 - - [04/Oct/2021:21:09:55 +0800] "HEAD / HTTP/1.1" 200 - "-" "curl/7.61.1" 10.0.0.104 - - [04/Oct/2021:21:11:04 +0800] "HEAD / HTTP/1.1" 200 - "-" "curl/7.61.1" 10.0.0.104 - - [04/Oct/2021:21:11:05 +0800] "HEAD / HTTP/1.1" 200 - "-" "curl/7.61.1" 10.0.0.104 - - [04/Oct/2021:21:11:06 +0800] "HEAD / HTTP/1.1" 200 - "-" "curl/7.61.1" 10.0.0.104 - - [04/Oct/2021:21:12:29 +0800] "HEAD / HTTP/1.1" 200 - "-" "curl/7.61.1" "10.0.0.101" 10.0.0.104 - - [04/Oct/2021:21:12:34 +0800] "GET / HTTP/1.1" 200 23 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "10.0.0.1" 10.0.0.104 - - [04/Oct/2021:21:12:35 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36" "10.0.0.1" 10.0.0.104 - - [04/Oct/2021:21:13:41 +0800] "HEAD / HTTP/1.1" 200 - "-" "curl/7.61.1" "10.0.0.101" 10.0.0.104 - - [04/Oct/2021:21:13:41 +0800] "HEAD / HTTP/1.1" 200 - "-" "curl/7.61.1" "10.0.0.101"