如何用Haproxy+nginx实现负载均衡

环境

物理机IP环境配置
master192.168.194.148Haproxy
node1192.168.194.144nginx
node2192.168.194.146nginx

node1和node2安装nginx

(我这里采用的是tar包的方式进行安装的)

Nginx是一个高性能的HTTP和反向代理服务器,同时还是IMAP/POP3/SMTP代理服务器,该程序由俄罗斯Rambler.ru 站点开发,Nginx因为性能稳定、低系统资源消耗而闻名,近几年Nginx在国内已经成炙热化状态,比如像腾讯、网易、51CTO、迅雷、当当网、51、人人网等诸多大型网站都已经使用Nginx来做Web服务器,所以我们要学会运用Nginx还是非常有必要的

1 安装编译工具
yum -y install gcc gcc-c++ make
2 安装pcre库

为了确保能在 Nginx 中使用正则表达式进行更灵活的配置,安装之前需要确定系统是否安装有 PCRE

tar zxvf pcre-8.33.tar.gz   解压安装包
cd pcre-...  进入安装包目录
./configure   运行
make &&make install   进行编译

3 安装zlib库

tar zxvf zlib-1.2.7.tar.gz
cd zlib-...
./configure
make &&make install

4 安装nginx

tar zxvf nginx-1.4.4.....tar.gz
cd nginx-...
./configure 
make &&make install

5 开启nginx服务
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reload 重新启动服务
6 关闭防火墙
systemctl stop firewalld
setenforce 0
7 修改nginx的默认界面内容(方便我们查看负载均衡效果)
nginx 的默认页面放在/usr/local/nginx/html

[root@node1 html]# vi index.html 

<h1>NO.2</h1>

7 访问192.168.194.144查看是否部署成功
在这里插入图片描述

同理部署node2

部署master

1 安装haproxy
yum install haproxy -y
2 关闭防火墙
iptables -F
setenforce 0
3 将配置文件进行备份(以防万一)
cp /etc/haproxy/haproxy.cfg{,.bak}
4 编辑配置文件(注意用自己搭建nginx服务器的IP替换下面的IP)

 vim /etc/haproxy/haproxy.cfg
 #---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  0.0.0.0:80
    bind   *:80
    default_backend      websrvs

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvs
    balance     roundrobin
    server      srv1 192.168.194.144:80 check maxconn 3
    server      srv2 192.168.194.146:80 check


5 启用Haproxy的日志功能
vim /etc/haproxy/haproxy.cfg


log  127.0.0.1 local2  \\日志的设备管道为local2,需在rsyslog配置文件中定义local2的日志设备

vim /etc/rsyslog.conf

将下面两句解除注释
$ModLoad imudp  \\启用UDP协议接收日志
$UDPServerRun 514 \\UDP端口为514
增加
 local2.*    /var/log/haproxy.log  \\定义local2日志设备的文件为/var/log/haproxy.log 

6 开启服务
systemctl start haproxy
systemctl enable haproxy
7 查看80 端口是否打开
在这里插入图片描述

8 测试

[root@bogon ~]# for i in {1..10};do curl http://192.168.194.148;done
<h1>NO.2</h1>
<h1>N0.1</h1>
<h1>NO.2</h1>
<h1>N0.1</h1>
<h1>NO.2</h1>
<h1>N0.1</h1>
<h1>NO.2</h1>
<h1>N0.1</h1>
<h1>NO.2</h1>
<h1>N0.1</h1>

9 页面测试
在这里插入图片描述

在这里插入图片描述

扩展

=1 启动压缩功能=
vim /etc/haproxy/haproxy.cfg

frontend 0.0.0.0:80
        bind *:80
        default_backend websrvs
        compression algo gzip \\启动压缩功能,压缩类型为gzip
        compression type text/html text/plainhtml,  application/xml\\压缩文件的类型为文本文件,plainhtml纯文本文件
backend websrvs
        balance roundrobin
        server srv1 192.168.194.144:80 check
        server srv2 192.168.194.146:80 check

2 定义check检查的时间间隔
vim /etc/haproxy/haproxy.cfg

frontend 0.0.0.0:80
        bind *:80
        default_backend websrvs
backend websrvs
        balance roundrobin
        # option httpchk \\启用七层代理向主页发送请求
        option httpchk GET /test1.html HTTP/1.0 \\启用七层代理,当使用GET命令,使用HTTP1.0协议向test1.txt页面发送请求时检查页面健康状态
        server srv1 192.168.194.146:80 check inter 3000ms rise 1 fall 2 \\inter定义为每3s检查一次,rise为检查成功一次即为成功,fall为检查失败两次即为故障
        server srv2 192.168.194.144:80 check backup \\backup为备用服务端,当其他主机故障时启用

后端主机的httpd访问日志中可以看到每隔2秒都有一次主页检查记录日志
[root@node1 html]# tail -f /var/log/httpd/access_log

== 3 实现网页重定向==
访问192.168.194.144后端主机srv1的网页将自动跳转到指定的网页,eg redir http://www.baidu.com 跳转到www.baidu.com

frontend myweb
        bind *:80
        default_backend websrvs
 backend websrvs
        balance roundrobin
        server srv1 192.168.194.144:80 check inter 3000ms rise 1 fall 2 redir http://www.baidu.com \\将访问172.16.253.105主页面重定向访问www.baidu.com 
        server srv2 192.168.194.146:80 check backup

4 权重设置

frontend myweb
        bind *:80
        default_backend websrvs
backend websrvs
        balance roundrobin
        server srv1 192.168.194.144:80 check weight 2 \\权重为2
        server srv2 192.168.194.146:80 check weight 1  \\权重为1

5 页面的用户访问控制

frontend myweb
        stats enable  \\启用stats
        stats uri /myproxy?admin \\自定义stats页面uri的路径为/myproxy?admin
        stats realm "HAProxy Stats Page" \\认证提示
        stats auth admin:admin \\stats页面用户访问控制,用户admin,密码admin
        bind *:80
        default_backend websrvs
 backend websrvs
        balance roundrobin
        server srv1 192.168.194.144:80 check weight 2
        server srv2 192.168.194.146:80check weight 1

启用status的管理功能

 frontend myweb *:80
        stats enable  \\启用stats
        stats uri /myproxy?admin \\自定义stats页面uri的路径为/myproxy?admin
        stats realm "HAProxy Stats Page" \\认证提示
        stats auth admin:admin \\stats页面用户访问控制,用户admin,密码admin
        stats admin if TRUE \\总是允许访问stats的用户管理stats页面
        default_backend websrvs
    backend websrvs
        balance roundrobin
        server srv1 172.16.253.105:80 check weight 2
        server srv2 172.16.252.1:80 check weight 1

单独定义stats的管理页面

 frontend myweb
        bind *:80
        default_backend websrvs
    
 backend websrvs
        balance roundrobin
        server srv1 172.16.253.105:80 check weight 2
        server srv2 172.16.252.1:80 check weight 1
  listen stats
        bind *:9000 \\定义stats页面的监听端口为9000
        stats enable \\开启stats状态界面
        stats uri /myproxy?admin \\自定义stats的uri路径
        stats realm "HAProxy Stats Page" \\stats页面的提示信息
        stats auth admin:admin \\ststs状态界面的admin用户认证
        stats admin if TRUE  \\允许所有登录stats的用户管理stats界面

定义haproxy的工作模式为tcp,实现layer4层代理

listen sshsrvs
        mode tcp
        bind *:2222
        balance leastconn
        server sshsrv1 172.16.253.105:22 check
        server sshsrv2 172.16.252.1:22 check

[root@client ~]# ssh root@172.16.253.108 -p 2222

设置cookie

frontend myweb *:80
        default_backend websrvs
backend websrvs
        cookie WEBSRV insert indirect nocache \\WEBSRV为自定义的cookie键名
        balance roundrobin
        server srv1 172.16.253.105:80 check weight 2 cookie srv1 \\srv1为自定义的srv1服务器的cookie信息
        server srv2 172.16.252.1:80 check weight 1 cookie srv2 \\srv2为自定义的srv2服务器的cookie信息
client

[root@client ~]# curl -I 192.168.194.148
HTTP/1.1 200 OK
Date: Fri, 26 May 2017 03:30:41 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Thu, 25 May 2017 11:26:46 GMT
ETag: "40801-1c-550577f03843e"
Accept-Ranges: bytes
Content-Length: 28
Content-Type: text/html; charset=UTF-8
Set-Cookie: WEBSRV=srv2; path=/  \\Cookie信息为WEBSRV=srv2
Cache-control: private

[root@client ~]# curl -I 192.168.194.148/test3.html
HTTP/1.1 200 OK
Date: Tue, 29 Aug 2017 04:41:00 GMT
Server: Apache/2.4.6 (CentOS) PHP/5.4.16
Last-Modified: Mon, 28 Aug 2017 14:02:09 GMT
ETag: "13-557d0bda20453"
Accept-Ranges: bytes
Content-Length: 19
Content-Type: text/html; charset=UTF-8
Set-Cookie: WEBSRV=srv1; path=/  \\Cookie信息为WEBSRV=srv1
Cache-control: private
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值