小小百科网

标题: CentOS6/Debian7下Haproxy实现负载均衡以及基于TCP和HTTP应用的代理 [打印本页]

作者: xiaoxiao    时间: 2015-4-14 14:41
标题: CentOS6/Debian7下Haproxy实现负载均衡以及基于TCP和HTTP应用的代理
本帖最后由 xiaoxiao 于 2016-2-9 13:23 编辑


一:安装Haproxy
For CentOS 6 Yum安装:
1:安装EPEL
标准的CentOS repository里面没有Haproxy,所以我们要先安装 Extra Packages for Enterprise Linux (EPEL) repository 到yum,EPEL包含有Haproxy。输入命令:
  1. rpm -ivh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
复制代码

或本站下载:
  1. rpm -ivh http://dl.aixiaoxiao.cn/vps/epel-release-6-8.noarch.rpm
复制代码

2:安装Haproxy
  1. yum -y install haproxy
复制代码


For Debian 7.0
1:添加源:
  1. vi /etc/apt/sources.list
  2. 添加一条源:
  3. deb http://ftp.us.debian.org/debian/ wheezy-backports main
复制代码

2:然后更新源:
  1. apt-get update
复制代码

3:安装:
  1. apt-get install haproxy
复制代码


二:配置Haproxy
先备份一份:
  1. cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
复制代码

编辑:
  1. vi /etc/haproxy/haproxy.cfg
复制代码


以下配置可适用于https的代理
  1. global
  2.         log             127.0.0.1 local2
  3.         chroot          /var/lib/haproxy
  4.         pidfile         /var/run/haproxy.pid
  5.         maxconn         4000
  6.         user            haproxy
  7.         group           haproxy
  8.         daemon

  9. defaults
  10.         mode            http
  11.         log             global
  12.         option          dontlognull
  13.         option          httpclose
  14.         #option         httplog
  15.         option          tcplog
  16.         #option         forwardfor
  17.         option          redispatch
  18.         timeout connect 10000
  19.         timeout client  300000
  20.         timeout server  300000
  21.         maxconn         60000
  22.         retries         3

  23. frontend https_frontend
  24.         bind *:443
  25.         mode tcp
  26.         default_backend web_server

  27. backend web_server
  28.         mode tcp
  29.         balance roundrobin
  30.         server s1 192.168.1.100:443
  31.         server s2 192.168.1.101:443
复制代码



以下配置可适用于中转SS流量,例如中转服务器的50000端口
  1. global
  2.         ulimit-n  51200

  3. defaults
  4.         log global
  5.         mode    tcp
  6.         option  dontlognull
  7.         contimeout 1000
  8.         clitimeout 150000
  9.         srvtimeout 150000

  10. frontend ss-in
  11.         bind *:50000(定义本地出口端口)
  12.         default_backend ss-out

  13. backend ss-out
  14.         server server1 目标服务器IP地址:50000 maxconn 20480
复制代码


设置开机自动启动
  1. chkconfig haproxy on
复制代码


启动Haproxy服务
  1. service haproxy start
复制代码











欢迎光临 小小百科网 (http://www.aixiaoxiao.cn/) Powered by Discuz! X3.3