nginx + node.js负载均衡&可视化管理

389 阅读1分钟

这是我参与「掘金日新计划 · 4 月更文挑战」的第 11 天,点击查看活动详情


启动node节点

01.js

const express = require('express');
const app = express();

app.get('/', (req, res) => res.send('<h1>The server at 01<h1>'));

// 监听端口
app.listen(8001, () => console.log('success: 01.js'));

02.js

const express = require('express');
const app = express();

app.get('/', (req, res) => res.send('<h1>The server at 02<h1>'));

// 监听端口
app.listen(8002, () => console.log('success: 02.js')););

03.js

const express = require('express');
const app = express();

app.get('/', (req, res) => res.send('<h1>The server at 03<h1>'));

// 监听端口
app.listen(8003, () => console.log('success: 03.js'));

nginx配置

启动nginx

start nginx

nginx配置文件

文件地址 conf\nginx.conf

	# 负载均衡节点
	upstream testServer {
	  server localhost:8001 weight=10;
	  server localhost:8002 weight=2;
	  server localhost:8003;
	}

    server {
        listen       8888;
        server_name  localhost;

        location / {
            root   main; # 文件根目录
            index  index.html index.htm;
			proxy_pass http://testServer; 
        }
    }
重启nginx
nginx -s reload

查看负载效果

重复刷新 http://localhost:8888/,可以看到每次返回的页面(区别在于最后的01,02,03)比例基本符合nginx配置文件中权重的设置 在这里插入图片描述

nginx-gui可视化管理

配置文件位置

conf/conf.properties

修改配置

nginx.path = D:/software/nginx-1.18.0 #nginx的安装路径
nginx.config = D:/software/nginx-1.18.0/conf/nginx.conf
account.admin = admin

启动

直接点击运行windows下的批处理命令 startup.bat

访问

http://localhost:8889/admin/

效果

系统管理

服务器状态

可以查看当前服务器的CPU负载和内存使用情况

在这里插入图片描述

高级管理

修改nginx配置文件,保存并热加载 在这里插入图片描述

负载管理

可以添加和编辑负载配置,调整节点地址、权重,以及添加新节点等功能

在这里插入图片描述

监听管理
监听列表

查看和编辑监听端口、日志保存路径,配置错误页面

在这里插入图片描述 在这里插入图片描述

规则列表

可以查看和配置监听域、代理地址、根路径 在这里插入图片描述 在这里插入图片描述