#---------------------------------------------------------------------# Example configuration for a possible web application. See the# full configuration options online.## https://www.haproxy.org/download/1.4/doc/configuration.txt##---------------------------------------------------------------------#---------------------------------------------------------------------# Global settings#---------------------------------------------------------------------global
# to have these messages end up in /var/log/haproxy.log you will# need to:## 1) configure syslog to accept network log events. This is done# by adding the '-r' option to the SYSLOGD\_OPTIONS in# /etc/sysconfig/syslog## 2) configure local2 events to go to the /var/log/haproxy.log# file. A line like the following can be added to# /etc/sysconfig/syslog## local2.* /var/log/haproxy.log# log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000#限制單個進程的最大連接數 user haproxy #用戶和用戶組,需手動添加 group haproxy
daemon #進程在後台作為守護進程運行# turn on stats unix socket stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------# common defaults that all the 'listen' and 'backend' sections will# use if not designated in their block#---------------------------------------------------------------------defaults
log global
option dontlognull
option redispatch #當客戶端連接到出現故障的服務器時,將請求轉發給其他機器 retries 3 timeout http-request 10s
timeout queue 1m
timeout connect 10s #連接server超時時間 timeout client 1m #客戶端相應超時時間 timeout server 1m #server端嚮應超時時間 timeout http-keep-alive 10s
timeout check 10s
maxconn 3000#---------------------------------------------------------------------# main frontend which proxys to the backends# 接收請求的前端虛擬節點#---------------------------------------------------------------------listen webservice #HTTP 做 HA,webservice 為此組設定的名稱,可以自行調整bind *:80
mode http
option httplog
option http-server-close
option forwardfor except 127.0.0.0/8 #在http請求頭中添加X-Forwarded-For,把客戶端IP發送給服務器 balance roundrobin
cookie SERVERID insert indirect nocache #可以讓某個瀏覽器發出的 Request 統一導向同一台機器,避免一些資料交換的問題 server webserver1 192.168.0.167:80 check #第一台機器 server webserver2 192.168.0.196:80 check #第二台機器# HAProxy 監控儀表板 stats enable stats hide-version
stats realm "Haproxy Statistics" stats uri /ha?stats #之後在 HTTP 的網域後方加上 /ha?stats 即可以看到監控畫面# 帳號密碼 stats auth admin:admin721
stats refresh 10s
listen dbservice #MySQL 做 HA,dbservice 為此組設定的名稱,可以自行調整bind *:3306
mode tcp
option mysql-check user haproxy\_check
balance roundrobin
server dbserver1 192.168.0.167:3306 check #第一台機器 server dbserver2 192.168.0.196:3306 check #第二台機器