一、环境说明
实验环境
OS CentOS5.4
192.168.0.14    proxy
192.168.0.24    web1
192.168.0.64    web2


官方地址:http://haproxy.1wt.eu/
下载地址:http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz

二、软件安装

点击(此处)折叠或打开

  1. [root@cacti src]# tar xzvf haproxy-1.4.24.tar.gz
  2. [root@cacti haproxy-1.4.24]# make TARGET=linux26 PREFIX=/usr/local/haproxy
  3. [root@cacti haproxy-1.4.24]# make install PREFIX=/usr/local/haproxy

三、创建配置文件
haproxy 默认是没有配置文件的,需要自己手机创建

vim /etc/haproxy.cfg

点击(此处)折叠或打开

  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. [root@cacti ~]# vim /etc/syslog.conf
  2. 添加:
  3. local3.* /var/log/haproxy.log
  4. local0.* /var/log/haproxy.log
  5. [root@cacti ~]# vim /etc/sysconfig/syslog
  6. 修改:
  7. SYSLOGD_OPTIONS="-r -m 0"
  8. service syslog restart

五、创建haproxy启动脚本

点击(此处)折叠或打开

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

六、启动

点击(此处)折叠或打开

  1. [root@cacti etc]# /etc/init.d/haproxy start
  2. Starting haproxy: [ OK ]
  3. [root@cacti etc]# ps -ef | grep haproxy
  4. 100 2305 1 0 17:55 ? 00:00:00 /usr/local/sbin/haproxy -D -f /etc/haproxy.cfg -p /var/run/haproxy.pid
  5. root 2308 774 0 17:55 pts/0 00:00:00 grep haproxy
  6. [root@cacti etc]#

报错:

点击(此处)折叠或打开

  1. [root@cacti ~]# /etc/init.d/haproxy start
  2. Starting haproxy: [ALERT] 177/105503 (18602) : Starting proxy cacti: cannot bind socket
  3. [FAILED]
  4. 产生这个错误可能有两个原因,
  5. 1)没有加入内核参数 (net.ipv4.ip_nonlocal_bind=1)
  6. 2)端口冲突

查看状态页面
http://192.168.0.14:8080/stats

测试:
打开http:192.168.0.14
可以轮询到两个web服务器上。

haproxy 配置文件说明 
来源
http://www.linuxidc.com/Linux/2012-07/65350.htm

点击(此处)折叠或打开

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

转载于:https://www.cnblogs.com/breg/p/4747941.html

haproxy 配置 说明相关推荐

  1. redis sentinel集群配置及haproxy配置

    ip分布情况: sentinel-1/redis 主 10.11.11.5 sentinel-2/redis 从 10.11.11.7 sentinel-3/redis 从 10.11.11.8 ha ...

  2. CENTOS7 高性能Linux集群 通过yum进行 haproxy配置 !安装 !使用!HAProxy配置文件详解

    Haproxy配置 永久修改名字:便于区分虚拟机! Haproxy:hostnamectl set-hostname haproxy Web1: hostnamectl set-hostname WE ...

  3. haproxy配置文件管理脚本

    #!/usr/bin/env python import os,sys,time,re,prettytable,json from collections import defaultdict,Ord ...

  4. haproxy配置负载均衡

    haproxy服务器(DR) IP:192.168.235.135 httpd服务器(RS1) IP:192.168.235.155 httpd服务器(RS2) IP:192.168.235.158 ...

  5. haproxy配置TCP代理

    haproxy配置TCP代理 安装: yum -y install haproxy 备份原配置文件: cp -rf /etc/haproxy/haproxy.cfg{,.bak} 删除配置文件中的注释 ...

  6. Haproxy 配置详解

    http://www.07net01.com/linux/Haproxypeizhixiangjie_645322_1380518936.html HAProxy提供高可用性.负载均衡以及基于TCP和 ...

  7. 利用keepalived和haproxy配置mysql的高可用负载均衡

    http://www.cnblogs.com/tae44/p/4717334.html 实验系统:CentOS 6.6_x86_64(2.6.32-504.30.3.el6.x86_64) 实验前提: ...

  8. 线上Haproxy配置

    安装平台:centos 6.3 cd /srv wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz #1.4.22版本比 ...

  9. haproxy配置参数详解

    可以先下载本文档附件,查看根据手册查看 一.CentOS 6.6 HAProxy安装配置指南 下面来进行配置文件的详细参数介绍,更多参数还请查看官方手册, 下面贴出我的配置文件,我的实验环境是一台服务 ...

  10. haproxy配置代理tomcat和nginx_你真的掌握LVS、Nginx及HAProxy工作原理吗?

    当前大多数的互联网系统都使用了服务器集群技术,集群是将相同服务部署在多台服务器上构成一个集群整体对外提供服务. 图片来自 Unsplash 这些集群可以是 Web 应用服务器集群,也可以是数据库服务器 ...

最新文章

  1. 不用比较运算符及循环控制语句,判断int型的a、b两数的大小
  2. 团队行为心理学读书笔记(2)招聘背后的心理学
  3. Safengine Android so加密
  4. java地图图表动态亮点,可视化图表行动指南:地表最强解读来了
  5. PTA-7-2 统计字符出现次数 (20分)
  6. c#自带压缩类实现数据库表导出到CSV压缩文件的方法
  7. 将asp.net webapi的运行时版本由4.0升级到4.5.1时遇到的问题及解决
  8. weblogic使用脚本部署
  9. faster rcnn第二阶段loss出现nan_深度学习训练Loss异常Debug思路
  10. 迅雷赚钱宝变砖TTL修复教程赚钱宝刷机
  11. Firefox选择哪个IE TAB
  12. 论文查重的标准是怎样的?
  13. java新闻网站项目描述_基于jsp的新闻网站-JavaEE实现新闻网站 - java项目源码
  14. css文本属性 首行缩进
  15. 手机信令数据怎么获得_运用手机信令数据研究大都市区空间结构 ——以南昌大都市区为例...
  16. java sencha_Sencha学习入门
  17. chmod 777的含义
  18. 创意下的个性化咖啡体验,Barsetto百胜图咖啡
  19. HashMap中的Hash码怎么计算,为什么要这样做?
  20. 【数学基础知识】证明三角形的三条垂线交于一点

热门文章

  1. 计算机网络环境中学科教学,浅谈基于计算机网络环境下的农村小学的科学学科教育...
  2. Spring-boot 项目中使用 jackson 遇到的一个问题
  3. RHEL 7.2 + Oracle 12cR2 + RAC
  4. RTP/RTCP协议与RTSP协议
  5. Qt学习之路(10): Meta-Object系统
  6. Docker 加速器升级版
  7. 代码协定(三)——假定和断言
  8. 计算Linux内存,CUP,硬盘使用率的shell脚本
  9. ?请问怎样将excel表格中的时间格式读取为字符串格式?
  10. Selenium自动化测试系列文章汇总