1. [root@node1 haproxy]# vim /etc/rsyslog.conf #为其添加日志功能
    2. # Provides UDP syslog reception
    3. $ModLoad imudp
    4. $UDPServerRun 514 ------>启动udp,启动端口后将作为服务器工作
    5.  
    6. # Provides TCP syslog reception
    7. $ModLoad imtcp
    8. $InputTCPServerRun 514 ------>启动tcp监听端口
    9. local2.* /var/log/haproxy.log
    10.  
    11. [root@node1 haproxy]# service rsyslog restar
    12.  
    13. [root@LB haproxy]# vim haproxy.cfg
    14.  
    15. log 127.0.0.1 local2 --------->在global端中添加此行
    16.  

    2. 一个最简单的http服务的配置:

    1. frontend webser
    2. log 127.0.0.1 local3
    3. option forwardfor
    4. bind *:80
    5. default_backend app
    6. backend app
    7. cookie node insert nocache
    8. balance roundrobin
    9. server app1 192.168.1.111:80 check cookie node1 intval 2 rise 1 fall 2
    10. server app2 192.168.1.112:80 check cookie node2 intval 2 rise 1 fall 2
    11. server backup 127.0.0.1:8010 check backup
    12.  
    13. listen statistics
    14. bind *:8009 # 自定义监听端口
    15. stats enable # 启用基于程序编译时默认设置的统计报告
    16. stats auth admin:admin # 统计页面用户名和密码设置
    17. stats uri /admin?stats # 自定义统计页面的URL,默认为/haproxy?stats
    18. stats refresh 30s # 统计页面自动刷新时间
    19. stats admin if TRUE #如果认证通过就做管理功能,可以管理后端的服务器
    20. stats realm Hapadmin # 统计页面密码框上提示文本,默认为Haproxy\ Statistics

    4. 动静分离示例:

    1. #---------------------------------------------------------------------
    2. # Global settings
    3. #---------------------------------------------------------------------
    4. global
    5. # to have these messages end up in /var/log/haproxy.log you will
    6. # need to:
    7. #
    8. # 1) configure syslog to accept network log events. This is done
    9. # by adding the '-r' option to the SYSLOGD_OPTIONS in
    10. # /etc/sysconfig/syslog
    11. #
    12. # 2) configure local2 events to go to the /var/log/haproxy.log
    13. # file. A line like the following can be added to
    14. # /etc/sysconfig/syslog
    15. #
    16. # local2.* /var/log/haproxy.log
    17. #
    18. log 127.0.0.1 local2
    19.  
    20. chroot /var/lib/haproxy
    21. pidfile /var/run/haproxy.pid
    22. maxconn 4000
    23. user haproxy
    24. group haproxy
    25. daemon
    26.  
    27. defaults
    28. mode http
    29. log global
    30. option httplog
    31. option dontlognull
    32. option http-server-close
    33. option forwardfor except 127.0.0.0/8
    34. option redispatch
    35. retries 3
    36. timeout http-request 10s
    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
    43.  
    44. listen stats
    45. mode http
    46. bind 0.0.0.0:1080
    47. stats enable
    48. stats hide-version
    49. stats uri /haproxyadmin?stats
    50. stats realm Haproxy\ Statistics
    51. stats auth admin:admin
    52. stats admin if TRUE
    53.  
    54.  
    55. frontend http-in
    56. bind *:80
    57. mode http
    58. log global
    59. option httpclose
    60. option logasap #不等待响应结束就记录日志,表示提前记录日志,一般日志会记录响应时长,此不记录响应时长
    61. option dontlognull #不记录空信息
    62. capture request header Host len 20 #记录请求首部的前20个字符
    63. capture request header Referer len 60 #referer跳转引用,就是上一级
    64. default_backend servers
    65.  
    66. frontend healthcheck
    67. bind :1099 #定义外部检测机制
    68. mode http
    69. option httpclose
    70. option forwardfor
    71. default_backend servers
    72.  
    73. backend servers
    74. balance roundrobin
    75. server websrv1 192.168.1.111:80 check maxconn 2000

    6. 负载均衡MySQL服务的配置示例