一、http服务器配置示例

#---------------------------------------------------------------------

#Global settings

#---------------------------------------------------------------------

global

    # to have these messages end up in/var/log/haproxy.log you will

    # need to:

    #

    # 1) configure syslog to accept network logevents.  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

 

defaults

    mode                    http

    log                     global

    option                  httplog

    option                  dontlognull

    option http-server-close

    option forwardfor       except 127.0.0.0/8

    option                  redispatch

    retries                 3

    timeout http-request    10s

    timeout queue           1m

    timeout connect         10s

    timeout client          1m

    timeout server          1m

    timeout http-keep-alive 10s

    timeout check           10s

    maxconn                 30000

 

listenstats

    mode http

    bind 0.0.0.0:1080

    stats enable

    stats hide-version

    stats uri     /haproxyadmin?stats

    stats realm   Haproxy\ Statistics

    stats auth    admin:admin

    stats admin if TRUE

 

frontendhttp-in

    bind *:80

    mode http

    log global

    option httpclose

    option logasap

    option dontlognull

    capture request  header Host len 20

    capture request  header Referer len 60

    default_backend servers

 

frontendhealthcheck

    bind :1099

    mode http

    option httpclose

    option forwardfor

    default_backend servers

 

backendservers

  balance roundrobin

    server websrv1 192.168.10.11:80 checkmaxconn 2000

    server websrv2 192.168.10.12:80 checkmaxconn 2000

   

二、负载均衡MySQL服务的配置示例

#---------------------------------------------------------------------

#Global settings

#---------------------------------------------------------------------

global

    # to have these messages end up in/var/log/haproxy.log you will

    # need to:

    #

    # 1) configure syslog to accept network logevents.  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

 

defaults

    mode                    tcp

    log                     global

    option                  httplog

    option                  dontlognull

    retries                 3

    timeout http-request    10s

    timeout queue           1m

    timeout connect         10s

    timeout client          1m

    timeout server          1m

    timeout http-keep-alive 10s

    timeout check           10s

    maxconn                 600

 

listenstats

    mode http

    bind 0.0.0.0:1080

    stats enable

    stats hide-version

    stats uri     /haproxyadmin?stats

    stats realm   Haproxy\ Statistics

    stats auth    admin:admin

    stats admin if TRUE

 

 

frontendmysql

    bind *:3306

    mode tcp

    log global

    default_backend mysqlservers

 

backendmysqlservers

    balance leastconn

    server dbsrv1 192.168.10.11:3306 check port3306 intval 2 rise 1 fall 2 maxconn 300

    server dbsrv2 192.168.10.12:3306 check port3306 intval 2 rise 1 fall 2 maxconn 300

 

 

三、动静分离示例

global

    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

 

defaults

    mode                    http

    log                     global

    option                  httplog

    option                  dontlognull

    option http-server-close

    option forwardfor       except 127.0.0.0/8

    option                  redispatch

    retries                 3

    timeout http-request    10s

    timeout queue           1m

    timeout connect         10s

    timeout client          1m

    timeout server          1m

    timeout http-keep-alive 10s

    timeout check           10s

    maxconn                 30000

 

listenstats

    mode http

    bind 0.0.0.0:1080

    stats enable

    stats hide-version

    stats uri     /haproxyadmin?stats

    stats realm   Haproxy\ Statistics

    stats auth    admin:admin

    stats admin if TRUE

 

 

frontendhttp-in

    bind *:80

    mode http

    log global

    option httpclose

    option logasap

    option dontlognull

    capture request  header Host len 20

    capture request  header Referer len 60

    acl url_static       path_beg       -i /static /p_w_picpaths /javascript/stylesheets

    acl url_static       path_end       -i .jpg .jpeg .gif .png .css .js

 

    use_backend static_servers          if url_static

    default_backend dynamic_servers

 

backendstatic_servers

    balance roundrobin

    server imgsrv1 172.16.200.7:80 checkmaxconn 6000

    server imgsrv2 172.16.200.8:80 checkmaxconn 6000

 

backenddynamic_servers

         cookie srv insert nocache

    balance roundrobin

    server websrv1 172.16.200.7:80 checkmaxconn 1000 cookie websrv1

    server websrv2 172.16.200.8:80 checkmaxconn 1000 cookie websrv2

    server websrv3 172.16.200.9:80 checkmaxconn 1000 cookie websrv3