学习笔记:Nginx配置负载均衡

前言

用Nginx配置一个简单的负载均衡,基础到不能再基础的入门篇

启动两个springboot web应用

写了两个springboot web应用,用于测试,启动了都可以单独访问,就可以了。

nginx_test_1
nginx_test_2

负载均衡配置

这里配置了负载均衡的权重,就是访问一次8080端口,会对9191和9292进行轮询分发访问请求,weight就是分发的权重,应该是访问15次8080,10次会分发给9292,5次会分发给9191

http {

        upstream balance {
                server 127.0.0.1:9191 weight=5;
                server 127.0.0.1:9292 weight=10;
        }

        server {
                listen  8080;

                location / {
                        proxy_pass      http://balance;
                }

                location /nginx-status {
                        allow 192.168.0.102;
                        deny all;
                        stub_status on;
                        access_log off;
                }

        }

}

events {
        worker_connections  1024;
}

测试

启动nginx

# nginx -c /opt/nginx/nginx-1.20.1/conf/nginx_balance_test.conf

在浏览器里测试,访问http://192.168.0.105:8080/hello,出现了11次nginx2,4次nginx1,说明这个轮询权重还是挺准的

this is nginx test 2
this is nginx test 2
this is nginx test 2
this is nginx test 1
this is nginx test 2
this is nginx test 2
this is nginx test 1
this is nginx test 2
this is nginx test 2
this is nginx test 1
this is nginx test 2
this is nginx test 2
this is nginx test 1
this is nginx test 2
this is nginx test 2

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值