基于CentOS 7 配置nginx负载均衡

搭建负载均衡服务的需求如下:

1 ) 把单台计算机无法承受的大规模并发访问或数据流量分担到多台节点设备上,分别进行处理, 减少用户等待响应的时间, 提升用户体验。
2 ) 单个重负载的运算分担到多台节点设备上做并行处理, 每个节点设备处理结束后, 将结果汇总,返回给用户,系统处理能力得到大幅度提高。
3 ) 7 x 24 小时的服务保证, 任意一个或多个有限后面节点设备宕机, 不能影响业务。 在负载均衡集群中, 同组集群的所有计算机节点都应该提供相同的服务。 集群负载均衡器会截获所有对该服务的入站请求。 然后将这些请求尽可能地平均地分配在所有集群节点上。

实验环境
HOSTNAME IP 说明
master 192.168.19.132 Nginx主负载均衡器
node1 192.168.19.133 Web1服务器
node3 192.168.19.135 Web2服务器

系统准备:CentOS Linux 7   x86_64
软件准备:nginx-1.22.0-1.el7.ngx.x86_64.rpm

                ​​​​​​​        ​​​​​​​   1、安装Nginx

2、添加 https 代理模块

3、定义Web服务器池

4、配置用于测试的Web服务

5、配置用于测试的Web服务

6、配置hosts解析

7、重新启动服务测试

1、安装Nginx

以下操作在3台服务器上执行

yum install nginx-1.22.0-1.el7.ngx.x86_64.rpm

2、添加 https 代理模块

这里需要重新编译 nginx,需要查看当前 nginx 的版本和编译选项,然后去官网下载同版本的 nginx 源 码进行重新编译

/usr/sbin/nginx -V

基于CentOS 7 配置nginx负载均衡_第1张图片

tar -xf c.tar.gz -C /usr/local/

下载模块 ngx_http_proxy_connect_module

git clone https://github.com/chobits/ngx_http_proxy_connect_module

打补丁,对 nginx 源码修改,这一步很重要,不然后面的 make 过不去

cd /usr/local/nginx-1.22.0/

patch -p 1 < /root/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch

在原有配置后追加模块,make 后注意不要 install

cd /usr/local/nginx-1.22.0/

        *安装依赖  yum install -y gcc gcc-c++ zlib_devel openssl-devel pcre-devel

./configure --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module --add-module=/root/ngx_http_proxy_connect_module/ make

出现如下结果,安装成功

基于CentOS 7 配置nginx负载均衡_第2张图片

3、定义Web服务器池

以下操作在lb01

upstream www_server_pools{
        server 192.168.19.133:80 weight=1;
        server 192.168.19.135:80 weight=1;
        }
server {
    listen       80;
    server_name  www.xixi.com;
    location / {
        proxy_pass http://www_server_pools;
    }
}

4、配置用于测试的Web服务

以下操作在两台web服务器

cd /etc/nginx/conf.d/

mv default.conf{,.bak}

cat vhost.conf
server {
    listen       80;
    server_name  bbs.yunjisuan.com;

    location / {
        root   /usr/share/nginx/html/bbs;
        index  index.html index.htm;
    }

        access_log /usr/share/nginx/html/bbs/logs/access_bbs.log main;
    }
server {
    listen       80;
    server_name  www.yunjisuan.com;

    location / {
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
    }

        access_log /usr/share/nginx/html/www/logs/access_www.log main;
    }

5、配置用于测试的Web服务

以下操作在两台web服务器

mkdir -p /usr/share/nginx/html/{www,bbs}/logs

echo "`hostname -I `www" > /usr/share/nginx/html/www/index.html

echo "`hostname -I `bbs" > /usr/share/nginx/html/bbs/index.html

6、配置hosts解析

[root@master ~]# tail -1 /etc/hosts

192.168.19.132 www.xixi.com

7、重新启动服务测试

[root@master ~]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@master ~]# systemctl restart nginx

[root@master ~]# for ((i=1;i<=4;i++)); do curl http://www.yunjisuan.com; done   

#正确显示结果

192.168.19.133 bbs

192.168.19.135 bbs

192.168.19.133 bbs

192.168.19.135 bbs

但是我显示如下,192.168.19.135的服务显示不出来??!!! 

基于CentOS 7 配置nginx负载均衡_第3张图片

从上面的测试结果可以看出来。两个Web节点按照1:1的比例被访问。 下面宕掉任意一个Web节点,看看测试结果如何,测试如下:

[root@node2 ~]# systemctl stop nginx

[root@master~]# for ((i=1;i<=4;i++)); do curl http://www.yunjisuan.com; done #正确显示结果 192.168.19.133 bbs

192.168.19.135 bbs

192.168.19.133 bbs

192.168.19.135 bbs

#192.168.19.135服务器显示依旧有问题

节点恢复正常后,再次测试:

[root@node2 ~]# systemctl start nginx

[root@master ~]# for ((i=1;i<=4;i++)); do curl http://www.yunjisuan.com; done #正确显示结果

192.168.19.133 bbs

192.168.19.135 bbs

192.168.19.133 bbs

192.168.19.135 bbs

#192.168.19.135服务器显示依旧有问题

实现 Nginx 负载均衡的组件说明

ngx_http_proxy_module proxy 代理模块,用于把请求后拋给服务器节点或 upstream 服务器池
ngx_http_upstream_module 负载均衡模块,可以实现网站的负载均衡功能及节点的健康检査

你可能感兴趣的:(运维,nginx,负载均衡)