Haproxy的安装和配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Haproxy的安装和配置相关的知识,希望对你有一定的参考价值。

Haproxy的安装和配置

 

Desktop1:172.25.68.1

Desktop2:172.25.68.2

Desktop3:172.25.68.3

真机:172.25.68.250

在desktop1和desktop2上

 yum install haproxy -y

 cd /etc/haproxy

 

 /etc/init.d/nginx stop    

 /etc/init.d/haproxy start

 编辑vim haproxy.cfg

 

listen westos *:80

        balance roundrobin

        server web1 172.25.68.1:80 check

        server web2 172.25.68.2:80 check

 

 /etc/init.d/haproxy reload

 

desktop3:

  yum install httpd

 编辑 vim /var/www/html/index.html

 Desktop3.example.com

 

  /etc/init.d/httpd start

测试:输入172.25.68.250:80

 

 

 

 

在desktop1上修改

编辑 vim haproxy.cfg

listen admin 172.25.68.250:8080

        stats enable

        stats uri /status

        stats auth admin:westos

        stats refresh 5s

 

listen westos 172.25.68.250:80

        balance roundrobin

        server web1 172.25.68.1:80 check

        server web2 172.25.68.2:80 check

 

 /etc/init.d/haproxy reload

 

测试: 172.25.68.250:8080/status 任意关闭开启desktop1和desktop2的http服务 注意观察颜色变化

 

动静分离:

desktop1:

编辑vim /etc/haproxy/haproxy.cfg

listen admin 172.25.68.250:8080

        stats enable

        stats uri /status

        stats auth admin:westos

        stats refresh 5s

 

frontend  westos 172.25.68.250:80

         acl url_static       path_beg       -i  /images

         acl url_static       path_end       -i .jpg .gif .png

 

         use_backend static          if url_static

         default_backend             app

 

backend static ##静态

        balance roundrobin

        server web1 172.25.68.2:80 check

 

backend app ##动态

        balance roundrobin

        server web2 172.25.68.1:80 check

 

 /etc/init.d/haproxy reload

 

Desktop2

cd /var/www/html

mkdir images

cd images

拷贝OSI.gifredhat.jpg到该目录

 

 

测试:172.25.68.250/images/redhat.jpg 或者 172.25.68.250/images/OSI.gif

 

 

访问权限的设置

desktop1:

编辑vim /etc/haproxy/haproxy.cfg

listen admin 172.25.68.250:8080

        stats enable

        stats uri /status

        stats auth admin:westos

        stats refresh 5s

 

frontend  westos 172.25.68.250:80

         acl url_static       path_beg       -i  /images

         acl url_static       path_end       -i .jpg .gif .png

 

         acl badhost src 172.25.68.2

         block if badhost

 

   use_backend static          if url_static

         default_backend             app

 

 /etc/init.d/haproxy reload

 

测试: 172.25.68.250 报错403

 

 

修改默认端口:编辑vim /etc/httpd/conf/httpd.conf

 135 #Listen 12.34.56.78:80

 136 Listen 8000

 

编辑vim /var/www/html

维护中,表闹........

 

/etc/init.d/httpd start

 

Desktop1:

编辑vim /etc/haproxy/haproxy.cfg

listen admin 172.25.68.250:8080

        stats enable

        stats uri /status

        stats auth admin:westos

        stats refresh 5s

 

frontend  westos 172.25.68.250:80

         acl url_static       path_beg       -i  /images

         acl url_static       path_end       -i .jpg .gif .png

 

         acl badhost src 172.25.68.2

 

#        block if badhost

         errorloc 403 http://172.25.68.2:8000

 

 use_backend static          if url_static

         default_backend             app

 

backend static

        balance roundrobin

        server web1 172.25.68.2:80 check

 

backend app

        balance roundrobin

        server web2 172.25.68.1:80 check

        server local 172.25.68.250:8000 backup

 

 /etc/init.d/haproxy reload

测试:172.25.68.250


本文出自 “12288655” 博客,谢绝转载!

以上是关于Haproxy的安装和配置的主要内容,如果未能解决你的问题,请参考以下文章

HAproxy安装和配置

mysql 主从配置和集群负载均衡

Keepalive+Haproxy高可用集群安装与配置

端口映射工具——HaProxy安装和配置

haproxy安装配置及haproxy+keepalived配置

Haproxy安装配置及日志输出问题