nginx+haproxy负载均衡

一、 安装第一台节点服务器

1. 安装nginx依赖程序
[root@centos06 ~]# yum -y install pcre-devel zlib-devel

在这里插入图片描述

2. 释放nginx归档文件
[root@centos06 ~]# tar zxvf /mnt/nginx-1.6.0.tar.gz -C /usr/src/

在这里插入图片描述

3. 创建管理nginx用户
[root@centos06 ~]# useradd -M -s /sbin/nologin nginx

在这里插入图片描述

4. 配置nginx
[root@centos06 ~]# cd /usr/src/nginx-1.6.0/
[root@centos06 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx

在这里插入图片描述

5. 编译安装nginx
[root@centos06 nginx-1.6.0]# make && make install

在这里插入图片描述

6. 优化命令,设置网站根目录
[root@centos06 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@centos06 ~]# echo "www.test01.com" > /usr/local/nginx/html/index.html

在这里插入图片描述

7. 启动服务监听端口
[root@centos06 ~]# nginx
[root@centos06 ~]# netstat -anptu | grep nginx

在这里插入图片描述

二、 安装第二胎节点服务器

1. 安装依赖程序
[root@centos07 ~]# yum -y install pcre-devel zlib-devel

在这里插入图片描述

2. 释放nginx归档文件
[root@centos07 ~]# tar zxvf /mnt/nginx-1.6.0.tar.gz -C /usr/src/

在这里插入图片描述

3. 创建管理nginx账户
[root@centos07 ~]# useradd -M -s /sbin/nologin nginx

在这里插入图片描述

4. 配置nginx
[root@centos07 ~]# cd /usr/src/nginx-1.6.0/
[root@centos07 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx

在这里插入图片描述

5. 编译安装nginx
[root@centos07 nginx-1.6.0]# make && make install

在这里插入图片描述

6. 优化命令,创建网站根目录
[root@centos07 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@centos07 ~]# echo "www.test02.com" > /usr/local/nginx/html/index.html

在这里插入图片描述

7. 启动服务监听端口
[root@centos07 ~]# nginx
[root@centos07 ~]# netstat -anptu | grep nginx

在这里插入图片描述

三、 安装haproxy

1. 安装haproxy依赖程序
[root@centos08 ~]# yum -y install pcre-devel zlib-devel bzip2-devel

在这里插入图片描述

2. 释放haproxy归档文件
[root@centos08 ~]# tar zxvf /mnt/haproxy-1.4.24.tar.gz -C /usr/src/

在这里插入图片描述

3. 配置安装haproxy
[root@centos08 haproxy-1.4.24]# make TARGET=linux26 && make install

在这里插入图片描述

4. 生成主配置文件
[root@centos08 haproxy-1.4.24]# mkdir /etc/haproxy
[root@centos08 haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/

在这里插入图片描述

5. 优化命令
[root@centos08 haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/sbin/

在这里插入图片描述

6. 生成服务配置文件
[root@centos08 haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy
[root@centos08 haproxy-1.4.24]# chmod +x /etc/init.d/haproxy

在这里插入图片描述

7. 添加系统服务设置开机启用

在这里插入图片描述

8. 修改haproxy主配置文件
[root@centos08 ~]# vim /etc/haproxy/haproxy.cfg
global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        maxconn 4096
        chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        redispatch
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen  appli1-rewrite 192.168.100.80:80
        cookie  SERVERID rewrite
        balance roundrobin
        server  app1_1 192.168.100.60:80 check inter 2000 rise 2 fall 5
        server  app1_2 192.168.100.70:80 check inter 2000 rise 2 fall 5

        option  checkcache /index.html
9. 创建服务启动临时目录
[root@centos08 ~]# mkdir /usr/share/haproxy

在这里插入图片描述

10. 启动服务,监听端口
[root@centos08 ~]# systemctl start haproxy
[root@centos08 ~]# netstat -anptu | grep haproxy

在这里插入图片描述

四、 客户端进行测试

1. 访问192.168.100.80

在这里插入图片描述

2. 停止192.168.100.60,网站服务器
[root@centos06 ~]# killall nginx
[root@centos06 ~]# netstat -antpu | grep nginx

在这里插入图片描述

3. 再进行测试(访问出现test02,使用第二台节点服务器)

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值