前言
用Nginx配置一个简单的负载均衡,基础到不能再基础的入门篇
启动两个springboot web应用
写了两个springboot web应用,用于测试,启动了都可以单独访问,就可以了。
负载均衡配置
这里配置了负载均衡的权重,就是访问一次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