阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

HAproxy安装配置重定向及读写分离

149次阅读
没有评论

共计 4774 个字符,预计需要花费 12 分钟才能阅读完成。

HAproxy 安装配置重定向及读写分离

  • server1:172.25.88.1
  • server3:172.25.88.3
  • server4:172.25.88.4

简单配置
server1:
yum install haproxy -y

vim /etc/haproxy/haproxy.cfg

60 #———————————————————————
 61 # main frontend which proxys to the backends
 62 #———————————————————————
 63 #frontend  main *:5000
 64 #    acl url_static      path_beg      -i /static /images /javascript /stylesheets
 65 #    acl url_static      path_end      -i .jpg .gif .png .css .js
 66 #
 67 #    use_backend static          if url_static
 68 #    default_backend            app
 69 #
 70 #———————————————————————
 71 # static backend for serving up images, stylesheets and such
 72 #———————————————————————
 73 #backend static
 74 #    balance    roundrobin
 75 #    server      static 127.0.0.1:4331 check
 76 #
 77 #———————————————————————
 78 # round robin balancing between the various backends
 79 #———————————————————————

listen westos *:80
  balance    roundrobin
  server      web1 172.25.88.3:80 check
  server      web2 172.25.88.4:80 check

 89 listen admin *:8080        haproxy 监控页面: 用浏览器访问 172.25.88.1:8080/status
 90        stats  enable
 91        stats uri /status  监控页面地址
 92        stats auth admin:westos    用户名:密码
 93        stats refresh 5s

server3:

yum install httpd -y

server4:
yum install httpd -y

检验:
浏览器访问 172.25.88.1    rr:3/4

浏览器访问 172.25.88.1:8080/status

HAproxy 安装配置重定向及读写分离

HAproxy 日志
日志默认存入 rsyslog,要查看的话, 需要打开 rsyslog

server1:

vim /etc/rsyslog.conf

13 $ModLoad imudp          接受 haproxy 日志
 14 $UDPServerRun 514
 42 *.info;mail.none;authpriv.none;cron.none;local2.none        /var/log/messages
 62 local2.*    /var/log/haproxy.log    单独存一份

/etc/init.d/rsyslog start

tail -f /var/log/haproxy.log

HAproxy 安装配置重定向及读写分离

server1:
vim /etc/haproxy

86 frontend westos *:80
 87        acl url_static      path_beg      -i  /images
 88        acl url_static      path_end      -i .jpg .gif .png
 89        use_backend static          if url_static
 90        default_backend app
 91
 92 backend static  后端静态
 93    balance    roundrobin
 94    server  web1 172.25.88.3:80 check
 95
 96 backend app
 97    balance    roundrobin
 98    server  web2 172.25.88.4:80 checkserver3:

/etc/init.d/httpd start

ll  /var/www/html/images/ OSI.gif  RedHat.jpg

HAproxy 安装配置重定向及读写分离

访问的是 server3, 因为只有 server3 才有 images 目录 server4:

/etc/init.d/httpd start

HAproxy 安装配置重定向及读写分离

重定向

server1:

先设置黑名单

vim /etc/haproxy

90        acl badhost src 172.25.88.250
 91        block if badhost 直接给用户返回 403, 不太友好, 所以重定向应运而生

HAproxy 安装配置重定向及读写分离

错误重定向(403)
vim /etc/httpd/conf/httpd.conf Listen 8000

vim /var/www/html/index.html 攻城师正在修复 ING… 其实我就是不想让你访问!!! 啦啦啦!

server1:
vim /etc/haproxy

90        acl badhost src 172.25.88.250
 91        block if badhost
 92        errorloc 403 http://172.25.88.1:8000badhost(172.25.88.250)在浏览器中访问 172.25.88.1 自动跳转到 http://172.25.88.1:8000

HAproxy 安装配置重定向及读写分离

黑名单重定向
server1:
        acl  badhost src 172.25.88.250
    redirect location http://172.25.88.1:8000 if  badhostbadhost(172.25.88.250) 在浏览器中访问 172.25.88.1 自动跳转到 http://172.25.4.11:8000

RS 全挂了以后(302 临时重定向)
server4:

/etc/init.d/httpd stop

server1:

vim /etc/haproxy

103 backend app
104    balance    roundrobin
105    server  web2 172.25.88.4:80 check
106    server  local 172.25.88.1:8000 backup 访问 172.25.88.1,跳转到 8000  显示工程师正在修复 ING(页面需要自己写)

网易:301    永久重定向      cdn

302    临时重定向,不推荐

HAproxy 安装配置重定向及读写分离

server1:

vim /etc/haproxy

94        redirect code 301 location http://172.25.88.1:8000 if badhost 如果不写 301, 只写 code 默认是 302, 临时重定向

HAproxy 安装配置重定向及读写分离

网页重定向
1. 访问 westos.org 自动跳转到 www.westos.org
真机:

vim /etc/hosts

172.25.88.1    server1.lalala.com www.westos.org westos.orgserver1:
vim /etc/haproxy

acl westos.org hdr_beg(host) -i westos.org
 redirect code 301 location http://www.westos.org if westos.org

HAproxy 安装配置重定向及读写分离

HAproxy 安装配置重定向及读写分离

2. 访问 IP 自动跳转到域名
server1:
vim /etc/haproxy

acl 172.25.88.1 hdr(host) -i 172.25.88.1
redirect code 301 location http://www.westos.org if 172.25.88.1

HAproxy 安装配置重定向及读写分离

HAproxy 安装配置重定向及读写分离

读写分离
server11:
vim /etc/haproxy

106        acl read method GET
107        acl read method HEAD
108        acl write method PUT
109        acl write method POST
119    use_backend app          if write
120    default_backend static

123 backend static
124        balance    roundrobin
125        server      web1 172.25.88.3:80 check    读
126
127 backend  app
128        balance    roundrobin
129        server      web2 172.25.88.4:80 check    写 server3,server4:
yum install php -y cd /var/www/html/upload/

mv * ..

chmod 777 upload

server3 和 server4 的 upload 都有读写权限哦~

HAproxy 安装配置重定向及读写分离

HAproxy 安装配置重定向及读写分离

记得将 upload 中的 size 改大一点.

&& ($_FILES[“file”][“size”] < 20000000))/etc/init.d/httpd restart  因为装了 php 模块, 还是重启一下吧~

检测
在浏览器中访问 www.westos.org/images

因为只有 server3 中有 images,server4 中没有,所以读端在 server3

HAproxy 安装配置重定向及读写分离

上传图片 (写) 在 server4 的 upload 中

尽管 server3,server4 的 upload 都是 777

HAproxy 安装配置重定向及读写分离

构建高可用集群 Keepalived+Haproxy 负载均衡  http://www.linuxidc.com/Linux/2016-12/138917.htm

HAproxy 的基本配置(负载均衡 + 日志独立 + 动静分离 + 读写分离)http://www.linuxidc.com/Linux/2017-03/141614.htm

CentOS 7 下 Keepalived + HAProxy 搭建配置详解  http://www.linuxidc.com/Linux/2017-03/141593.htm

HAproxy 实现反向代理和负载均衡  http://www.linuxidc.com/Linux/2016-12/138749.htm

HAProxy+Keepalived 实现高可用负载均衡 http://www.linuxidc.com/Linux/2016-06/132225.htm

使用 HAProxy 配置 HTTP 负载均衡器 http://www.linuxidc.com/Linux/2015-01/112487.htm

Ubuntu 16.04 下安装 HAProxy 1.5.11 做 tcp 负载均衡 http://www.linuxidc.com/Linux/2016-06/132689.htm

HAproxy 的详细介绍:请点这里
HAproxy 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-04/142455.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计4774字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中