一、haproxy安装

1、下载、解压haproxy-1.6.11.tar.gz
2、安装 rpm-build

[[email protected] ~]# yum install rpm-build -y

3、生成 rpmbuild 目录

[[email protected] ~]# rpmbuild -bb haproxy-1.6.11

haproxy配置
4、rpmbuild制作rpm安装包

[[email protected] ~]# mv haproxy-1.6.11.tar.gz rpmbuild/SOURCES/      ##源码打包
[[email protected] ~]# cd haproxy-1.6.11/examples/
[[email protected] examples]# rpmbuild -bb haproxy.spec                         ##rpm打包

注意:安装依赖性包

yum install -y pcre-devel
yum install -y gcc

5、安装haproxy

[[email protected] ~]# cd /root/rpmbuild/RPMS/x86_64/
[[email protected] x86_64]# ls
haproxy-1.6.11-1.x86_64.rpm
[[email protected] x86_64]# rpm -ivh haproxy-1.6.11-1.x86_64.rpm 

haproxy配置

二、haproxy负载均衡

1、修改配置文件

[[email protected] ~]# cd haproxy-1.6.11/examples/
[[email protected] examples]# cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg
[[email protected] examples]# cd /etc/haproxy/
[[email protected] haproxy]# vim haproxy.cfg 
 10 global
 11         maxconn         65535
 12         stats socket    /var/run/haproxy.stat mode 600 level admin
 13         log             127.0.0.1 local0
 14         uid             200
 15         gid             200
 16         chroot          /var/empty
 17         daemon
 18 
 19 defaults
 20         mode            http
 21         log             global
 22         option          httplog
 23         option          dontlognull
 24         monitor-uri     /monitoruri
 25         maxconn         8000
 26         timeout client  30s
 27         retries         2
 28         option redispatch
 29         timeout connect 5s
 30         timeout server  30s
 31         timeout queue   30s
 32         stats uri       /admin/stats
 33 
 34 # The public 'www' address in the DMZ
 35 frontend public
 36         bind             *:80
 37         #use_backend     static if { hdr_beg(host) -i img }
 38         #use_backend     static if { path_beg /img /css   }
 39         default_backend dynamic
 40 
 41 # the application servers go here
 42 backend dynamic
 43         balance         roundrobin
 44         server          web1 172.25.51.2:80 check inter 1000
 45         server          web2 172.25.51.3:80 check inter 1000

2、修改限制文件

[[email protected] haproxy]# vim /etc/security/limits.conf 
haproxy         -       nofile          65536

3、添加haproxy用户和组

[[email protected] haproxy]# groupadd -g 200 haproxy
[[email protected] haproxy]# useradd -u 200 -g 200 -M -s /sbin/nologin haproxy

haproxy配置
4、调度器开启haproxy服务

[[email protected] haproxy]# /etc/init.d/haproxy start            ##开启服务

5、后端开启httpd服务
haproxy配置
haproxy配置

测试:

haproxy配置haproxy配置haproxy配置

三、haproxy日志设置(更改日志路径)

[[email protected] ~]# vim /etc/rsyslog.conf 
[[email protected] ~]# /etc/init.d/rsyslog restart

haproxy配置haproxy配置haproxy配置
更改配置文件,重新开启haproxy服务

[[email protected] ~]# vim /etc/haproxy/haproxy.cfg 
[[email protected] ~]# /etc/init.d/haproxy restart

haproxy配置
当后端web服务器server1、server2崩溃时,跳转到backup
haproxy配置

查看haproxy日志

[[email protected] ~]# cat /var/log/haproxy.log 

haproxy配置

四、用户认证

[[email protected] ~]# vim /etc/haproxy/haproxy.cfg 
[[email protected] ~]# /etc/init.d/haproxy reload

haproxy配置
示图:用户认证,帐号admin,密码linux
haproxy配置
haproxy配置

五、ACL访问限制

1、黑名单
<1>黑名单设定

[[email protected] ~]# vim /etc/haproxy/haproxy.cfg 
frontend public
        bind             *:80
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        acl blacklist src 172.25.51.250       ##创建访问黑名单
        http-request deny if blacklist           ##对黑名单里面的用户进行拒绝
        default_backend dynamic
[[email protected] ~]# /etc/init.d/haproxy reload

测试:
haproxy配置
<2>黑名单访问重定向

[[email protected] ~]# vim /etc/haproxy/haproxy.cfg 
frontend public
        bind             *:80
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        acl blacklist src 172.25.51.250       ##创建访问黑名单
        http-request deny if blacklist           ##对黑名单里面的用户进行拒绝
        errorloc 403 http://172.25.51.4:80 ##如碰到403报错,将访问重定向
        default_backend dynamic
[[email protected] ~]# /etc/init.d/haproxy reload

haproxy配置
2、所有用户访问重定向

[[email protected] ~]# vim /etc/haproxy/haproxy.cfg 
frontend public
        bind             *:80
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        redirect location http://www.baidu.com   ##所有的用户访问dynamic都会被重新定向到www.baidu.com
        #errorloc 403 http://172.25.51.4:80
        default_backend dynamic
[[email protected] ~]# /etc/init.d/haproxy reload

haproxy配置
测试:浏览器访问172.25.51.1,自动跳转到 www.baidu.com
haproxy配置

六、haproxy实现动静分离

安装php

[[email protected] ~]# yum install -y php
[[email protected] ~]# vim /var/www/html/index.php
<?php
phpinfo()
?>

haproxy配置

[[email protected] ~]# vim /etc/haproxy/haproxy.cfg 
frontend public
        bind             *:80
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        use_backend     static if { path_end -i .php   }  ##访问路径中为.php结尾的页面时候去访问static
        default_backend dynamic  ##静态页面,采用默认访问路径

# the application servers go here
backend dynamic
        balance         roundrobin
        server          web2 172.25.51.3:80 check inter 1000
backend static
        balance         roundrobin
        server          web1 172.25.51.2:80 check inter 1000
[[email protected] ~]# /etc/init.d/haproxy reload

测试:
当访问172.25.12.1时候会去主动访问server3
haproxy配置
访问172.25.12.1/index.php的时候会去访问server2中的php文件haproxy配置

七、haproxy实现读写分离

将读的请求交给server2处理,将写的请求交给server3处理

[[email protected] ~]# vim /etc/haproxy/haproxy.cfg 
frontend public
        bind             *:80
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        acl write method POST
        acl write method PUT
        acl read method  GET
        acl read method  HEAD
        use_backend     dynamic if write
        use_backend     static if read
        default_backend dynamic

# the application servers go here
backend dynamic
        balance         roundrobin
        server          web2 172.25.51.3:80 check inter 1000
backend static
        balance         roundrobin
        server          web1 172.25.51.2:80 check inter 1000
[[email protected] ~]# /etc/init.d/haproxy reload

在server3和server2中的httpd默认发布目录,放进去index.php(选择图片的静态页面)和upload_file.php(上传图片的动态页面),在默认发布目录创建存放上传图片upload目录,修改权限为777。
haproxy配置
测试:
haproxy配置

haproxy配置

相关文章:

  • 2021-10-30
  • 2021-07-18
  • 2021-11-27
  • 2021-08-25
  • 2021-09-27
  • 2021-07-18
  • 2021-12-05
猜你喜欢
  • 2021-12-13
  • 2021-11-09
  • 2021-09-27
  • 2021-11-04
  • 2022-02-02
  • 2021-10-10
  • 2021-07-11
相关资源
相似解决方案