nginx集群搭建——四层

奋斗吧
奋斗吧
擅长邻域:未填写

标签: nginx集群搭建——四层

2023-05-11 18:23:27 182浏览

这里我们选择yum源安装,因为centos7yum源里没有nginx,所以我们要先安装拓展源。这边可以看见实现了最基本的wrr的负载均衡,测试结果也是成功的。环境:四台虚拟机、系统centos7.9、nginx。修改测试服务器的hosts文件。用client服务器测试结果。

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

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

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

3.配置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地址。

4.测试

修改测试服务器的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
192.168.133.137 www
192.168.133.130 www
192.168.133.137 www
192.168.133.130 www
192.168.133.137 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
192.168.133.137 bbs
192.168.133.130 bbs
192.168.133.137 bbs
192.168.133.130 bbs
192.168.133.137 bbs

这边可以看见实现了最基本的wrr的负载均衡,测试结果也是成功的

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695