首页 > 代码库 > HAproxy的安装与配置讲解

HAproxy的安装与配置讲解

1,安装

 1 官网下载
 2 http://www.haproxy.org/#down
 3 cd /usr/local/src/
 4 wget http://www.haproxy.org/download/1.4/src/haproxy-1.4.27.tar.gz
 5 tar -zxvf haproxy-1.4.27.tar.gz
 6 cd haproxy-1.4.27
 7 make TARGET=linux26 PREFIX=/usr/local/haproxy
 8 make install  PREFIX=/usr/local/haproxy
 9 mkdir /usr/local/haproxy/conf
10 cp examples/haproxy.cfg /usr/local/haproxy/conf
11 
12 完成

HAproxy默认不创建配置文件目录,我们自己创建  mkdir /usr/local/haproxy/conf

拷贝事例配置文件到我们创建的配置文件目录

2配置文件讲解

默认配置文件如下:

技术分享
 1 # this config needs haproxy-1.1.28 or haproxy-1.2.1
 2 
 3 global
 4         log 127.0.0.1   local0
 5         log 127.0.0.1   local1 notice
 6         #log loghost    local0 info
 7         maxconn 4096
 8         chroot /usr/share/haproxy
 9         uid 99
10         gid 99
11         daemon
12         #debug
13         #quiet
14 
15 defaults
16         log     global
17         mode    http
18         option  httplog
19         option  dontlognull
20         retries 3
21         redispatch
22         maxconn 2000
23         contimeout      5000
24         clitimeout      50000
25         srvtimeout      50000
26 
27 listen  appli1-rewrite 0.0.0.0:10001
28         cookie  SERVERID rewrite
29         balance roundrobin
30         server  app1_1 192.168.34.23:8080 cookie app1inst1 check inter 2000 rise 2 fall 5
31         server  app1_2 192.168.34.32:8080 cookie app1inst2 check inter 2000 rise 2 fall 5
32         server  app1_3 192.168.34.27:8080 cookie app1inst3 check inter 2000 rise 2 fall 5
33         server  app1_4 192.168.34.42:8080 cookie app1inst4 check inter 2000 rise 2 fall 5
34 
35 listen  appli2-insert 0.0.0.0:10002
36         option  httpchk
37         balance roundrobin
38         cookie  SERVERID insert indirect nocache
39         server  inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
40         server  inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
41         capture cookie vgnvisitor= len 32
42 
43         option  httpclose               # disable keep-alive
44         rspidel ^Set-cookie:\ IP=       # do not let this cookie tell our internal IP address
45 
46 listen  appli3-relais 0.0.0.0:10003
47         dispatch 192.168.135.17:80
48 
49 listen  appli4-backup 0.0.0.0:10004
50         option  httpchk /index.html
51         option  persist
52         balance roundrobin
53         server  inst1 192.168.114.56:80 check inter 2000 fall 3
54         server  inst2 192.168.114.56:81 check inter 2000 fall 3 backup
55 
56 listen  ssl-relay 0.0.0.0:8443
57         option  ssl-hello-chk
58         balance source
59         server  inst1 192.168.110.56:443 check inter 2000 fall 3
60         server  inst2 192.168.110.57:443 check inter 2000 fall 3
61         server  back1 192.168.120.58:443 backup
62 
63 listen  appli5-backup 0.0.0.0:10005
64         option  httpchk *
65         balance roundrobin
66         cookie  SERVERID insert indirect nocache
67         server  inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
68         server  inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
69         server  inst3 192.168.114.57:80 backup check inter 2000 fall 3
70         capture cookie ASPSESSION len 32
71         srvtimeout      20000
72 
73         option  httpclose               # disable keep-alive
74         option  checkcache              # block response if set-cookie & cacheable
75 
76         rspidel ^Set-cookie:\ IP=       # do not let this cookie tell our internal IP address
77 
78         errorloc        502     http://192.168.114.58/error502.html
79         errorfile       503     /etc/haproxy/errors/503.http
View Code

 

HAproxy的安装与配置讲解