Nginx集群搭建

Nginx集群搭建,Nginx负载均衡

Nginx集群搭建

Nginx负载均衡

环境:四台虚拟机、系统Centos7.9、Nginx

Hostname

Ip

说明

lb

192.168.133.142

Nginx主负载均衡器

rs1

192.168.133.130

Web服务器1

rs2

192.168.133.137

Web服务器2

Client

192.168.133.139

客户端—测试用

安装Nginx

这里我们选择yum源安装,因为centos7yum源里没有nginx,所以我们要先安装拓展源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

配置用于测试的两台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;
}

配置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

用Client服务器测试结果

[root@localhost ~]# curl -H host:bbs.yunjisuan.com 192.168.133.130
192.168.133.130 bbs
[root@localhost ~]# curl -H host:bbs.yunjisuan.com 192.168.133.137
192.168.133.137 bbs
[root@localhost ~]# curl -H host:www.yunjisuan.com 192.168.133.137
192.168.133.137 www
[root@localhost ~]# curl -H host:www.yunjisuan.com 192.168.133.130
192.168.133.130 www

配置lb的Nginx负载均衡

# 定义一个服务器池
upstream ServerPools {
        server 192.168.133.130:80 weight=1;  # 第一个服务器
        server 192.168.133.137:80 weight=1;  # 第二个服务器
}
# 配置针对 www.yunjisuan.com 的请求
server {
        listen 80;  # 监听 80 端口
        server_name www.yunjisuan.com;  # 配置 server_name 为 www.yunjisuan.com
        location / {  # 配置 location 匹配根路径
                proxy_pass http://ServerPools;  # 将请求转发到upstream服务器池
                proxy_set_header Host $host;  # 设置 Host 头部,针对多台虚拟主机的情况
                proxy_set_header X-Forwarded-For $remote_addr;  # 设置 X-Forwarded-For 头部,在日志中显示客户端ip
        }
}
# 配置针对 bbs.yunjisuan.com 的请求
server {
        listen 80;  # 监听 80 端口
        server_name bbs.yunjisuan.com;  # 配置 server_name 为 bbs.yunjisuan.com
        location / {  # 配置 location 匹配根路径
                proxy_pass http://ServerPools;  # 将请求转发到upstream服务器池
                proxy_set_header Host $host;  # 设置 Host 头部,针对多台虚拟主机的情况
                proxy_set_header X-Forwarded-For $remote_addr;  # 设置 X-Forwarded-For 头部,在日志中显示客户端ip
        }
}

1.upstream模块:定义一个上游服务器池,用于存储多个上游服务器的信息,这样可以实现负载均衡。

2.server模块:定义一个虚拟主机,用于监听指定的端口和主机名,可以配置多个server模块实现一个nginx服务器对多个网站的服务。

3.server_name指令:配置虚拟主机的域名。

4.location指令:用于匹配请求的URI,指定在请求URI匹配时所应该执行的操作。

5.proxy_pass指令:将请求转发到上游服务器池。

6.proxy_set_header指令:设置HTTP请求头部的值,这里用于设置Host和X-Forwarded-For头部。Host头部指定目标服务器的主机名,X-Forwarded-For头部指定客户端的IP地址。

Nginx集群搭建

测试

修改测试服务器的hosts文件

[root@localhost ~]# vim /etc/hosts
192.168.133.142 www.yunjisuan.com bbs.yunjisuan.com

测试

[root@localhost ~]# for i in {1..10}; do curl www.yunjisuan.com ; done
192.168.133.130 www
192.168.133.137 www
192.168.133.130 www
192.168.133.137 www
192.168.133.130 www
[root@localhost ~]# for i in {1..10}; do curl bbs.yunjisuan.com ; done
192.168.133.130 bbs
192.168.133.137 bbs
192.168.133.130 bbs
192.168.133.137 bbs
192.168.133.130 bbs

这基本已经基本实现了wrr的负载均衡,测试结果也是成功的。

Intoep小程序

微信扫一扫,打开小程序浏览更便捷

原创文章,作者:howkunet,如若转载,请注明出处:https://www.intoep.com/system/62441.html

发表回复

登录后才能评论