天天看点

haproxy

四层转发tcp(lvs)七层代理http(haproxy)

稳定性的适合用lvs  网站负载适合用haproxy nginx

haproxy(单核):

HAProxy 提供高可用性、负载均衡以及基于 TCP 和 HTTP 应用的代理,支持虚拟主机,

它是免费、快速并且可靠的一种解决方案。HAProxy 特别适用于那些负载特大的 web 站点, 这些站点通常又需要会话保持或七层处理。HAProxy 运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的 web 服务器不被暴露到网络上。

三台虚拟机:

虚拟机3调度器:

yum install haproxy -y (建议用rpm包装)

haproxy

cd /etc/haproxy/

vim haproxy.cfg

监控页面添加认证:

listen admin *:8080

       stats enable

       stats uri /status  # 监控页面地址

       stats authadmin:westos  # 管理帐号和密码

       stats refresh 5s  #刷新频率

listen  westos *:80      #监听的实例名称,地址和端口

        balance roundrobin   #负载均衡算法

        server web1172.25.42.13:80 check

        server web2 172.25.42.14:80 check

haproxy
haproxy
haproxy
haproxy

两台real server

/etc/init.d/httpd start

日志监控:

$ModLoad imudp     #接受 haproxy 日志

$UDPServerRun 514

vim /etc/rsyslog.conf

*.info;mail.none;authpriv.none;cron.none;local2.none     /var/log/messages

local2.*                 /var/log/haproxy.log   #日志文件位置

haproxy

自带健康检查:(测试)

haproxy
haproxy

动态静态访问分离:(可以自己加虚拟主机)

frontend  westos *:80

    acl url_static path_beg       -i /p_w_picpaths

    acl url_static path_end   -i .jpg .gif .png

    use_backend static          if url_static

    default_backend  app (默认访问app)

backend static (静态)

        balance roundrobin

backend app

        server web1 172.25.42.13:80 check

haproxy
haproxy
haproxy
haproxy

错误重定向:

 acl url_static    path_beg       -i /p_w_picpaths  (以什么开头 默认根目录)

 acl url_static path_end      -i .jpg .gif .png (以什么结尾)

    acl badhost  src 172.25.42.250  (设定谁不能访问我)

    block if badhost

    errorloc 403 http://172.25.42.12:8000(注意端口不要冲突)(403:服务器拒绝你的访问 服务器设定你是坏的 )(如果是403错误就重定向到 172.25.254.12:8000)

    redirect location http://172.25.42.12:8000 ifbadhost (如果出现错误访问 就重定向 不管是什么错误)

haproxy

     use_backend static          if url_static

     default_backend  app

backend static

haproxy

        server local 172.25.42.12:8000backup

(如果后端realserver down掉 则调度报错页面 但是正常情况下不会访问 因为 是“backup”)

haproxy
haproxy
haproxy

301永久重定向:(一般推荐用301  302临时重定向 有恶意刷点击的嫌疑)

    aclwestos.org hdr_beg(host) -i westos.org 

    acl 172.25.42.12 hdr_beg(host) -i172.25.42.12

#    block if badhost

#    errorloc 403http://172.25.42.12:8000

#    redirect locationhttp://172.25.42.12:8000 if badhost

     redirect code 301 locationhttp://www.westos.org if westos.org

     (以westos.org访问自动重定向 www.westos.org)

     redirect code 301 locationhttp://www.westos.org if 172.25.42.12

     (以172.25.42.12访问自动重定向 www.westos.org)

haproxy

读写分离:

real server(两台): yum install php -y

php页面:

chmod 777 upload (默认上传目录一定要给权限)

haproxy

调度机器:

    acl read method GET     

    acl read method HEAD     //两个read write 只用一个就行

    acl write method PUT

    acl write method POST

     use_backend app         if write 

     default_backend  static (默认静态页面 为了测试 刚开始 www.westos.org 时是172.25.42.14 而写(上传时 却是在172.25.254.13 上进行的)做到了读写分离)

        server web1 172.25.42.13:80 check(默认会上传到这个real server)

        server local 172.25.42.12:8000backup

注意: /etc/init.d/haporxy reload   real server:/etc/init.d/httpd restart

haproxy
haproxy
haproxy

继续阅读