haproxy 配置

时间:2022-08-24 16:40:26

1、环境:
操作系统:CentOS 6.4
haproxy: 1.3.15.10 [下载:http://download.chinaunix.net/download.php?id=25784&ResourceID=12508]
服务器IP: 10.5.24.51

修改HOST,添加如下配置
10.5.24.51 w1.rhythmk.com
10.5.24.51 w2.rhythmk.com

2、安装

  tar -zxvf  haproxy-1.3.15.10.tar.gz
mv haproxy-1.3.15.10 haproxy
cd haproxy
make TARGET=linux26 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy

    默认安装后,/usr/local/haproxy/conf 路径是没有haproxy.cfg配置 ,添加配置文件如下:

global
maxconn 51200
chroot /usr/local/haproxy
uid 99
gid 99
daemon
#quiet
nbproc 1
pidfile /usr/local/haproxy/logs/haproxy.pid defaults
mode http
#retries 2
option redispatch
option abortonclose
timeout connect 5000ms
timeout client 30000ms
timeout server 30000ms
#timeout check 2000
log 127.0.0.1 local0 err #[err warning info debug]
balance roundrobin
# option httplog
# option httpclose
# option dontlognull
# option forwardfor listen admin_stats
bind 0.0.0.0:8888
option httplog
stats refresh 30s
stats uri /stats
stats realm Haproxy Manager
stats auth admin:admin frontend rhythmk.com
bind 0.0.0.0:80
option httpclose ## hdr_reg 采用正则匹配对应请求域名 ,进行映射
## hdr_dom 全相等匹配域名 进行映射 acl w1.rhythmk hdr_dom(host) -i w1.rhythmk.com
use_backend w1.rhythmk.com if w1.rhythmk acl w2.rhythmk hdr_reg(host) -i w2.rhythmk.com
use_backend w2.rhythmk.com if w2.rhythmk ######## web1 ##############
backend w1.rhythmk.com
mode http
balance roundrobin
appsession 9ai9 len 64 timeout 5h request-learn
balance roundrobin
#cookie LOGONCOOKIE insert nocache
#option httpchk GET /index.php
# 端口 92xx 为主干,93xx 为分支1(dev),94xx 为分支2(branch2)
server w1.rhythmk 127.0.0.1:6055 check inter 10000 rise 3 fall 3 weight 100 ######## web2 ##############
backend w2.rhythmk.com
mode http
balance roundrobin
appsession 9ai9 len 64 timeout 5h request-learn
balance roundrobin
#cookie LOGONCOOKIE insert nocache
#option httpchk GET /index.php
server w2.rhythmk 127.0.0.1:6056 check inter 10000 rise 3 fall 3 weight 100

   添加启动脚本 restart.sh:

#!/bin/bash

pkill -9 haproxy
dirpath=/usr/local/haproxy $dirpath/sbin/haproxy -f $dirpath/conf/haproxy.cfg

    为 restart.sh 添加执行权限。启动haproxy

3、验证:

已经配置好的站点 :

w1站点:

haproxy 配置

w2站点:

haproxy 配置

浏览地址:

http://10.5.24.51:8888/stats

haproxy 配置