坚持不懈之linux haproxy 配置文件 详情

  1 ####################全局配置信息######################## 
  2 #######参数是进程级的,通常和操作系统(OS)相关######### 
  3 global 
  4        maxconn 20480 #默认最大连接数 
  5        log 127.0.0.1 local3 #[err warning info debug] 
  6        chroot /var/haproxy #chroot运行的路径 
  7        uid 99 #所属运行的用户uid 
  8        gid 99 #所属运行的用户组 
  9        daemon #以后台形式运行haproxy 
 10        nbproc 1 #进程数量(可以设置多个进程提高性能) 
 11        pidfile /var/run/haproxy.pid #haproxy的pid存放路径,启动进程的用户必须有权限访问此文件 
 12        ulimit-n 65535 #ulimit的数量限制 
 13  
 14  
 15 #####################默认的全局设置###################### 
 16 ##这些参数可以被利用配置到frontend,backend,listen组件## 
 17 defaults 
 18        log global 
 19        mode http #所处理的类别 (#7层 http;4层tcp ) 
 20        maxconn 20480 #最大连接数 
 21        option httplog #日志类别http日志格式 
 22        option httpclose #每次请求完毕后主动关闭http通道 
 23        option dontlognull #不记录健康检查的日志信息 
 24        option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip 
 25        option redispatch #serverId对应的服务器挂掉后,强制定向到其他健康的服务器 
 26        option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接 
 27        stats refresh 30 #统计页面刷新间隔 
 28        retries 3 #3次连接失败就认为服务不可用,也可以通过后面设置 
 29        balance roundrobin #默认的负载均衡的方式,轮询方式 
 30       #balance source #默认的负载均衡的方式,类似nginx的ip_hash 
 31       #balance leastconn #默认的负载均衡的方式,最小连接 
 32        contimeout 5000 #连接超时 
 33        clitimeout 50000 #客户端超时 
 34        srvtimeout 50000 #服务器超时 
 35        timeout check 2000 #心跳检测超时 
 36  
 37 ####################监控页面的设置####################### 
 38 listen admin_status #Frontend和Backend的组合体,监控组的名称,按需自定义名称 
 39         bind 0.0.0.0:65532 #监听端口 
 40         mode http #http的7层模式 
 41         log 127.0.0.1 local3 err #错误日志记录 
 42         stats refresh 5s #每隔5秒自动刷新监控页面 
 43         stats uri /admin?stats #监控页面的url 
 44         stats realm itnihao\ itnihao #监控页面的提示信息 
 45         stats auth admin:admin #监控页面的用户和密码admin,可以设置多个用户名 
 46         stats auth admin1:admin1 #监控页面的用户和密码admin1 
 47         stats hide-version #隐藏统计页面上的HAproxy版本信息 
 48         stats admin if TRUE #手工启用/禁用,后端服务器(haproxy-1.4.9以后版本) 
 49  
 50  
 51        errorfile 403 /etc/haproxy/errorfiles/403.http 
 52        errorfile 500 /etc/haproxy/errorfiles/500.http 
 53        errorfile 502 /etc/haproxy/errorfiles/502.http 
 54        errorfile 503 /etc/haproxy/errorfiles/503.http 
 55        errorfile 504 /etc/haproxy/errorfiles/504.http 
 56  
 57 #################HAProxy的日志记录内容设置################### 
 58        capture request header Host len 40 
 59        capture request header Content-Length len 10 
 60        capture request header Referer len 200 
 61        capture response header Server len 40 
 62        capture response header Content-Length len 10 
 63        capture response header Cache-Control len 8 
 64      
 65 #######################网站监测listen配置##################### 
 66 ###########此用法主要是监控haproxy后端服务器的监控状态############ 
 67 listen site_status 
 68        bind 0.0.0.0:1081 #监听端口 
 69        mode http #http的7层模式 
 70        log 127.0.0.1 local3 err #[err warning info debug] 
 71        monitor-uri /site_status #网站健康检测URL,用来检测HAProxy管理的网站是否可以用,正常返回200,不正常返回503 
 72        acl site_dead nbsrv(server_web) lt 2 #定义网站down时的策略当挂在负载均衡上的指定backend的中有效机器数小于1台时返回true 
 73        acl site_dead nbsrv(server_blog) lt 2 
 74        acl site_dead nbsrv(server_bbs) lt 2 
 75        monitor fail if site_dead #当满足策略的时候返回503,网上文档说的是500,实际测试为503 
 76        monitor-net 192.168.16.2/32 #来自192.168.16.2的日志信息不会被记录和转发 
 77        monitor-net 192.168.16.3/32 
 78  
 79 ########frontend配置############ 
 80 #####注意,frontend配置里面可以定义多个acl进行匹配操作######## 
 81 frontend http_80_in 
 82        bind 0.0.0.0:80 #监听端口,即haproxy提供web服务的端口,和lvs的vip端口类似 
 83        mode http #http的7层模式 
 84        log global #应用全局的日志配置 
 85        option httplog #启用http的log 
 86        option httpclose #每次请求完毕后主动关闭http通道,HA-Proxy不支持keep-alive模式 
 87        option forwardfor #如果后端服务器需要获得客户端的真实IP需要配置次参数,将可以从Http Header中获得客户端IP 
 88        ########acl策略配置############# 
 89        acl itnihao_web hdr_reg(host) -i ^(www.itnihao.cn|ww1.itnihao.cn)$ 
 90        #如果请求的域名满足正则表达式中的2个域名返回true -i是忽略大小写 
 91        acl itnihao_blog hdr_dom(host) -i blog.itnihao.cn 
 92        #如果请求的域名满足www.itnihao.cn返回true -i是忽略大小写 
 93        #acl itnihao hdr(host) -i itnihao.cn 
 94        #如果请求的域名满足itnihao.cn返回true -i是忽略大小写 
 95        #acl file_req url_sub -i killall= 
 96        #在请求url中包含killall=,则此控制策略返回true,否则为false 
 97        #acl dir_req url_dir -i allow 
 98        #在请求url中存在allow作为部分地址路径,则此控制策略返回true,否则返回false 
 99        #acl missing_cl hdr_cnt(Content-length) eq 0 
100        #当请求的header中Content-length等于0时返回true 
101  
102 ########acl策略匹配相应############# 
103        #block if missing_cl 
104        #当请求中header中Content-length等于0阻止请求返回403 
105        #block if !file_req || dir_req 
106        #block表示阻止请求,返回403错误,当前表示如果不满足策略file_req,或者满足策略dir_req,则阻止请求 
107        use_backend server_web if itnihao_web 
108        #当满足itnihao_web的策略时使用server_web的backend 
109        use_backend server_blog if itnihao_blog 
110        #当满足itnihao_blog的策略时使用server_blog的backend 
111        #redirect prefix http://blog.itniaho.cn code 301 if itnihao 
112        #当访问itnihao.cn的时候,用http的301挑转到http://192.168.16.3 
113        default_backend server_bbs 
114        #以上都不满足的时候使用默认server_bbs的backend 
115  
116  
117 ##########backend的设置############## 
118 #下面我将设置三组服务器 server_web,server_blog,server_bbs
119 ##################backend server_web####################
120 backend server_web 
121        mode http #http的7层模式 
122        balance roundrobin #负载均衡的方式,roundrobin平均方式 
123        cookie SERVERID #允许插入serverid到cookie中,serverid后面可以定义 
124        option httpchk GET /index.html #心跳检测的文件 
125        server web1 192.168.16.2:80 cookie web1 check inter 1500 rise 3 fall 3 weight 1 
126        #服务器定义,cookie 1表示serverid为web1,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用, 
127        #fall 3是3次失败认为服务器不可用,weight代表权重 
128        server web2 192.168.16.3:80 cookie web2 check inter 1500 rise 3 fall 3 weight 2 
129        #服务器定义,cookie 1表示serverid为web2,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用, 
130        #fall 3是3次失败认为服务器不可用,weight代表权重 
131  
132 ###################backend server_blog######################
133 backend server_blog 
134        mode http #http的7层模式 
135        balance roundrobin #负载均衡的方式,roundrobin平均方式 
136        cookie SERVERID #允许插入serverid到cookie中,serverid后面可以定义 
137        option httpchk GET /index.html #心跳检测的文件 
138        server blog1 192.168.16.2:80 cookie blog1 check inter 1500 rise 3 fall 3 weight 1 
139        #服务器定义,cookie 1表示serverid为blog1,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重 
140        server blog2 192.168.16.3:80 cookie blog2 check inter 1500 rise 3 fall 3 weight 2 
141        #服务器定义,cookie 1表示serverid为blog2,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重 
142  
143 ##################backend server_bbs######################## 
144  
145 backend server_bbs 
146        mode http #http的7层模式 
147        balance roundrobin #负载均衡的方式,roundrobin平均方式 
148        cookie SERVERID #允许插入serverid到cookie中,serverid后面可以定义 
149        option httpchk GET /index.html #心跳检测的文件 
150        server bbs1 192.168.16.2:80 cookie bbs1 check inter 1500 rise 3 fall 3 weight 1 
151        #服务器定义,cookie 1表示serverid为bbs1,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重
152        server bbs2 192.168.16.3:80 cookie bbs2 check inter 1500 rise 3 fall 3 weight 2 
153        #服务器定义,cookie 1表示serverid为bbs2,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重

以上是配置文件的每一行的具体含义

-------------------------------------------------------------------------------------------

 1 global
 2     log 127.0.0.1 local3
 3     maxconn 20480
 4     chroot /usr/local/haproxy
 5     uid 1004 #1004为haproxy 用户的uid ,haproxy用户需要自己手动创建
 6     gid 1004
 7     daemon
 8     quiet
 9     nbproc 1
10     pidfile /var/run/haproxy.pid
11 defaults
12     log global
13     mode http
14     maxconn 20480
15     option httplog
16     option httpclose
17     option forwardfor
18     option dontlognull
19     option redispatch
20     retries 3
21     balance roundrobin
22     contimeout 5000
23     clitimeout 50000
24     srvtimeout 50000
25 listen web_poll 192.168.0.14:80
26     mode http
27     option httplog
28     option dontlognull
29     option logasap
30     option forwardfor
31     option httpclose
32 # option httpchk GET /index.html
33 server web1 192.168.0.24:80 cookie 1 check inter 2000 rise 3 fall 3
34 server web2 192.168.0.64:80 cookie 1 check inter 2000 rise 3 fall 3
35 listen status 192.168.0.14:8080
36     stats enable
37     stats uri /stats
38     stats auth admin:123456
39     stats realm (Haproxy\ statistic)

以上是根据自己需求而配置的配置文件

----------------------------------------------------------------

 

  1 #!/bin/bash
  2 #
  3 # haproxy
  4 #
  5 # chkconfig: 35 85 15
  6 # description: HAProxy is a free, very fast and reliable solution \
  7 # offering high availability, load balancing, and \
  8 # proxying for TCP and HTTP-based applications
  9 # processname: haproxy
 10 # config: /etc/haproxy.cfg
 11 # pidfile: /var/run/haproxy.pid
 12 
 13 # Source function library.
 14 . /etc/rc.d/init.d/functions
 15 
 16 # Source networking configuration.
 17 . /etc/sysconfig/network
 18 
 19 # Check that networking is up.
 20 [ "$NETWORKING" = "no" ] && exit 0
 21 
 22 config="/etc/haproxy.cfg"
 23 exec="/usr/local/haproxy/sbin/haproxy"
 24 prog=$(basename $exec)
 25 
 26 [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
 27 
 28 lockfile=/var/lock/subsys/haproxy
 29 
 30 check() {
 31     $exec -c -V -f $config
 32 }
 33 
 34 start() {
 35     $exec -c -q -f $config
 36     if [ $? -ne 0 ]; then
 37         echo "Errors in configuration file, check with $prog check."
 38         return 1
 39     fi
 40  
 41     echo -n $"Starting $prog: "
 42     # start it up here, usually something like "daemon $exec"
 43     daemon $exec -D -f $config -p /var/run/$prog.pid
 44     retval=$?
 45     echo
 46     [ $retval -eq 0 ] && touch $lockfile
 47     return $retval
 48 }
 49 
 50 stop() {
 51     echo -n $"Stopping $prog: "
 52     # stop it here, often "killproc $prog"
 53     killproc $prog 
 54     retval=$?
 55     echo
 56     [ $retval -eq 0 ] && rm -f $lockfile
 57     return $retval
 58 }
 59 
 60 restart() {
 61     $exec -c -q -f $config
 62     if [ $? -ne 0 ]; then
 63         echo "Errors in configuration file, check with $prog check."
 64         return 1
 65     fi
 66     stop
 67     start
 68 }
 69 
 70 reload() {
 71     $exec -c -q -f $config
 72     if [ $? -ne 0 ]; then
 73         echo "Errors in configuration file, check with $prog check."
 74         return 1
 75     fi
 76     echo -n $"Reloading $prog: "
 77     $exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)
 78     retval=$?
 79     echo
 80     return $retval
 81 }
 82 
 83 force_reload() {
 84     restart
 85 }
 86 
 87 fdr_status() {
 88     status $prog
 89 }
 90 
 91 case "$1" in
 92     start|stop|restart|reload)
 93         $1
 94         ;;
 95     force-reload)
 96         force_reload
 97         ;;
 98     checkconfig)
 99         check
100         ;;
101     status)
102         fdr_status
103         ;;
104     condrestart|try-restart)
105       [ ! -f $lockfile ] || restart
106     ;;
107     *)
108         echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}"
109         exit 2
110 esac

 

以上是haproxy的启动脚本linux 

 

posted @ 2015-08-23 20:40  Dus  阅读(644)  评论(0编辑  收藏  举报