haproxy小结(二)配置文件篇 中对 haproxy.conf中所涉及的主要参数做一个解析。本篇列举一些现网中使用的真实示例。

一、http服务器配置示例

 1#---------------------------------------------------------------------
 2# Global settings
 3# code by www.361way.com
 4#---------------------------------------------------------------------
 5global
 6    # to have these messages end up in /var/log/haproxy.log you will
 7    # need to:
 8    #
 9    # 1) configure syslog to accept network log events.  This is done
10    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
11    #    /etc/sysconfig/syslog
12    #
13    # 2) configure local2 events to go to the /var/log/haproxy.log
14    #   file. A line like the following can be added to
15    #   /etc/sysconfig/syslog
16    #
17    #    local2.*                       /var/log/haproxy.log
18    #
19    log         127.0.0.1 local2
20    chroot      /var/lib/haproxy
21    pidfile     /var/run/haproxy.pid
22    maxconn     4000
23    user        haproxy
24    group       haproxy
25    daemon
26defaults
27    mode                    http
28    log                     global
29    option                  httplog
30    option                  dontlognull
31    option http-server-close
32    option forwardfor       except 127.0.0.0/8
33    option                  redispatch
34    retries                 3
35    timeout http-request    10s
36    timeout queue           1m
37    timeout connect         10s
38    timeout client          1m
39    timeout server          1m
40    timeout http-keep-alive 10s
41    timeout check           10s
42    maxconn                 30000
43listen stats
44    mode http
45    bind 0.0.0.0:1080        
46    stats enable
47    stats hide-version
48    stats uri     /haproxyadmin?stats
49    stats realm   Haproxy\ Statistics
50    stats auth    admin:admin
51    stats admin if TRUE
52frontend http-in
53    bind *:80
54    mode http
55    log global
56    option httpclose
57    option logasap
58    option dontlognull
59    capture request  header Host len 20
60    capture request  header Referer len 60
61    default_backend servers
62frontend healthcheck
63    bind :1099
64    mode http
65    option httpclose
66    option forwardfor
67    default_backend servers
68backend servers
69  balance roundrobin
70    server websrv1 192.168.10.11:80 check maxconn 2000
71    server websrv2 192.168.10.12:80 check maxconn 2000

二、MySQL服务器配置示例

global 、defaults、listen部分同上面http配置部分相同,forntend与backend部分配置如下:

 1# code from www.361way.com
 2frontend mysql
 3    bind *:3306
 4    mode tcp
 5    log global
 6    default_backend mysqlservers
 7backend mysqlservers
 8    balance leastconn
 9    server dbsrv1 192.168.10.11:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
10    server dbsrv2 192.168.10.12:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300

三、动静分离配置示例

 1# code from www.361way.com
 2global
 3    log 127.0.0.1 local2
 4    chroot      /var/lib/haproxy
 5    pidfile     /var/run/haproxy.pid
 6    maxconn     4000
 7    user        haproxy
 8    group       haproxy
 9    daemon
10# turn on stats unix socket
11    stats socket /var/lib/haproxy/stats
12defaults
13    mode http
14    log global
15    option httplog
16    option dontlognull
17    option http-server-close
18    option forwardfor except 127.0.0.0/8
19    option redispatch
20    retries 3
21    timeout http-request 10s
22    timeout queue 1m
23    timeout connect 10s
24    timeout client 1m
25    timeout server 1m
26    timeout http-keep-alive 10s
27    timeout check 10s
28    maxconn 30000
29listen stats
30    mode http
31    bind 0.0.0.0:1080
32    stats enable
33    stats hide-version
34    stats uri /haproxyadmin?stats
35    stats realm Haproxy\ Statistics
36    stats auth admin:admin
37    stats admin if TRUE
38frontend http-in
39    bind *:80
40    mode http
41    log global
42    option httpclose
43    option logasap
44    option dontlognull
45    capture request header Host len 20
46    capture request header Referer len 60
47    acl url_static path_beg -i /static /images /javascript /stylesheets
48    acl url_static path_end -i .jpg .jpeg .gif .png .css .js
49    use_backend static_servers          if url_static
50    default_backend dynamic_servers
51backend static_servers
52    balance roundrobin
53    server imgsrv1 172.16.200.7:80 check maxconn 6000
54    server imgsrv2 172.16.200.8:80 check maxconn 6000
55backend dynamic_servers
56    cookie srv insert nocache
57    balance roundrobin
58    server websrv1 172.16.200.7:80 check maxconn 1000 cookie websrv1
59    server websrv2 172.16.200.8:80 check maxconn 1000 cookie websrv2
60    server websrv3 172.16.200.9:80 check maxconn 1000 cookie websrv3