龙空技术网

Nginx集群搭建

土豆丝炒海带G 124

前言:

当前兄弟们对“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 
配置用于测试的两台Web服务器
# cd /etc/nginx/conf.d/# mv default.conf{,.bak}# cat vhost.confserver {    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 ;    location / {        root /usr/share/nginx/html/www;        index index.html index.htm;    }    access_log /usr/share/nginx/html/www/logs/access_ main;}
配置Web服务的内容
mkdir -p /usr/share/nginx/html/{www,bbs}/logsecho "`hostname -I `www" > /usr/share/nginx/html/www/index.htmlecho "`hostname -I `bbs" > /usr/share/nginx/html/bbs/index.html

用Client服务器测试结果

[root@localhost ~]# curl -H host:bbs.yunjisuan.com 192.168.133.130192.168.133.130 bbs[root@localhost ~]# curl -H host:bbs.yunjisuan.com 192.168.133.137192.168.133.137 bbs[root@localhost ~]# curl -H host: 192.168.133.137192.168.133.137 www[root@localhost ~]# curl -H host: 192.168.133.130192.168.133.130 www
配置lb的Nginx负载均衡
# 定义一个服务器池upstream ServerPools {        server 192.168.133.130:80 weight=1;  # 第一个服务器        server 192.168.133.137:80 weight=1;  # 第二个服务器}# 配置针对  的请求server {        listen 80;  # 监听 80 端口        server_name ;  # 配置 server_name 为         location / {  # 配置 location 匹配根路径                proxy_pass ;  # 将请求转发到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 ;  # 将请求转发到upstream服务器池                proxy_set_header Host $host;  # 设置 Host 头部,针对多台虚拟主机的情况                proxy_set_header X-Forwarded-For $remote_addr;  # 设置 X-Forwarded-For 头部,在日志中显示客户端ip        }}
upstream模块:定义一个上游服务器池,用于存储多个上游服务器的信息,这样可以实现负载均衡。server模块:定义一个虚拟主机,用于监听指定的端口和主机名,可以配置多个server模块实现一个nginx服务器对多个网站的服务。server_name指令:配置虚拟主机的域名。location指令:用于匹配请求的URI,指定在请求URI匹配时所应该执行的操作。proxy_pass指令:将请求转发到上游服务器池。proxy_set_header指令:设置HTTP请求头部的值,这里用于设置Host和X-Forwarded-For头部。Host头部指定目标服务器的主机名,X-Forwarded-For头部指定客户端的IP地址。测试

修改测试服务器的hosts文件

[root@localhost ~]# vim /etc/hosts192.168.133.142  bbs.yunjisuan.com

测试

[root@localhost ~]# for i in {1..10}; do curl  ; done192.168.133.130 www192.168.133.137 www192.168.133.130 www192.168.133.137 www192.168.133.130 www[root@localhost ~]# for i in {1..10}; do curl bbs.yunjisuan.com ; done192.168.133.130 bbs192.168.133.137 bbs192.168.133.130 bbs192.168.133.137 bbs192.168.133.130 bbs

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

标签: #nginx服务搭建