Nginx部署反向代理集群,Nginx负载均衡,健康检查,加权轮询

部署环境

节点IP地址角色
nginx192.168.44.173反向代理服务器
web1192.168.44.174网站1(nginx网站)
web2192.168.44.175网站2(nginx网站)

部署介绍

nginx安装,web网页位置和内容参考上一篇
https://blog.csdn.net/qq_44659804/article/details/126685364?spm=1001.2014.3001.5502

nginx安装
同节点nginx,节点web1,节点web2。
web网页位置和内容
web1网页文件在/data/nginx/index.html,内容为 web1_test~~~
web2网页文件在/data/nginx/index.html,内容为 web2_test~~~

此处部署仅对配置文件做修改,操作均在nginx安装目录进行

部署web1节点

关闭服务

[root@web1 nginx]# sbin/nginx -s stop

还原配置文件

[root@web1 nginx]# cp conf/nginx.conf.default conf/nginx.conf
cp:是否覆盖"conf/nginx.conf"yes

修改配置文件

匹配路径为/webs
网页在/data/nginx

[root@web1 nginx]# vim conf/nginx.conf
 33     #gzip  on;
 34
 35     server {
 36         listen       80;
 37         server_name  localhost;
 38
 39         #charset koi8-r;
 40
 41         #access_log  logs/host.access.log  main;
 42
 43         location / {
 44             root   html;
 45             index  index.html index.htm;
 46         }
 47         location /webs {
 48             alias /data/nginx;
 49            # root   html;
 50            # index  index.html index.htm;
 51         }
 52
 53         #error_page  404              /404.html;

启动服务

[root@web1 nginx]# sbin/nginx  -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@web1 nginx]# sbin/nginx
[root@web1 nginx]# ss -utnlp | grep nginx
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=1356,fd=6),("nginx",pid=1355,fd=6))

本机访问测试

[root@web1 nginx]# curl 192.168.44.174/webs/
 web1_test~~~

部署web2节点

关闭服务

[root@web2 nginx]# sbin/nginx -s stop

还原配置文件

[root@web2 nginx]# cp conf/nginx.conf.default conf/nginx.conf
cp:是否覆盖"conf/nginx.conf"yes

修改配置文件

匹配路径为/webs
网页在/data/nginx

[root@web2 nginx]# vim conf/nginx.conf
 33     #gzip  on;
 34
 35     server {
 36         listen       80;
 37         server_name  localhost;
 38
 39         #charset koi8-r;
 40
 41         #access_log  logs/host.access.log  main;
 42
 43         location / {
 44             root   html;
 45             index  index.html index.htm;
 46         }
 47
 48         location /webs {
 49             alias /data/nginx;
 50            # root   html;
 51            # index  index.html index.htm;
 52         }
 53         #error_page  404              /404.html;

启动服务

[root@web2 nginx]# sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@web2 nginx]#  ss -utnlp | grep nginx
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=1320,fd=6),("nginx",pid=1319,fd=6))

本机访问测试

[root@web2 nginx]# curl 192.168.44.175/webs/
web2_test~~~

部署nginx节点

关闭服务

[root@nginx nginx]# sbin/nginx -s stop

还原配置文件

[root@nginx nginx]# cp conf/nginx.conf.default conf/nginx.conf
cp:是否覆盖"conf/nginx.conf"yes

修改配置文件

增加集群名字叫web_test
匹配到/webs,把请求方向代理到集群web_test

[root@nginx nginx]# vim conf/nginx.conf
 33     #gzip  on;
 34     upstream web_test {
 35         server 192.168.44.174:80  max_fails=2 fail_timeout=30 weight=2;	#配置健康检查,检测两次如果失败,则认为集群中的主机故障,之后等待30秒再次测试。权重设置为2。
 36         server 192.168.44.175:80  max_fails=2 fail_timeout=30;	#配置健康检查,检测两次如果失败,则认为集群中的主机故障,之后等待30秒再次测试。权重设置默认为1。
 37     }
 38     server {
 39         listen       80;
 40         server_name  localhost;
 41
 42         #charset koi8-r;
 43
 44         #access_log  logs/host.access.log  main;
 45
 46         location / {
 47             root   html;
 48             index  index.html index.htm;
 49         }
 50
 51        location /webs {
 52        proxy_pass http://web_test;
 53        }
 54
 55         #error_page  404              /404.html;

启动服务

[root@nginx nginx]# sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx nginx]# ss -utnlp | grep nginx
tcp    LISTEN     0      128       *:80                    *:*                                                                                               

访问测试

web服务全部开启测试

访问10次,访问结果web1为web2的两倍

[root@nginx nginx]# for i in {1..10}
> do
> echo "第$i次访问"
> curl 192.168.44.173/webs/
> done
第1次访问
 web1_test~~~
第2次访问
web2_test~~~
第3次访问
 web1_test~~~
第4次访问
 web1_test~~~
第5次访问
web2_test~~~
第6次访问
 web1_test~~~
第7次访问
 web1_test~~~
第8次访问
web2_test~~~
第9次访问
 web1_test~~~
第10次访问
 web1_test~~~
#访问结果大致为2:1
关闭web1服务测试

把web1服务关闭再次访问,此时只有web2能被访问到,服务未中断。

[root@web1 nginx]# sbin/nginx  -s stop
[root@web1 nginx]# ss -utnlp | grep nginx
#本机无法访问
[root@web1 nginx]# curl 192.168.44.174/webs/
curl: (7) Failed connect to 192.168.44.174:80; 拒绝连接

#通过nginx代理访问,web1服务已关闭,只有web2

[root@nginx nginx]# for i in {1..10}; do echo "第$i次访问"; curl 192.168.44.173/webs/; done
第1次访问
web2_test~~~
第2次访问
web2_test~~~
第3次访问
web2_test~~~
第4次访问
web2_test~~~
第5次访问
web2_test~~~
第6次访问
web2_test~~~
第7次访问
web2_test~~~
第8次访问
web2_test~~~
第9次访问
web2_test~~~
第10次访问
web2_test~~~

再次开启web1服务测试

开启web1服务,健康检查通过后,继续对外提供服务
把web2服务开启,本机立即可以访问,因为健康检查的间隔时间使30秒,30秒后可通过nginx代理访问

[root@web1 nginx]# sbin/nginx
[root@web1 nginx]# ss -utnlp | grep nginx
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=1809,fd=6),("nginx",pid=1808,fd=6))
[root@web1 nginx]# curl 192.168.44.174/webs/
 web1_test~~~

通过nginx代理访问

[root@nginx nginx]# for i in {1..10}; do echo "第$i次访问"; curl 192.168.44.173/webs/; done
第1次访问
web2_test~~~
第2次访问
 web1_test~~~
第3次访问
web2_test~~~
第4次访问
 web1_test~~~
第5次访问
 web1_test~~~
第6次访问
web2_test~~~
第7次访问
 web1_test~~~
第8次访问
 web1_test~~~
第9次访问
web2_test~~~
第10次访问
 web1_test~~~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值