Blog
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)

HiuZing

🍑
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)
  • Nginx

    • Nginx介绍
    • Nginx安装
    • Nginx配置文件
    • Nginx反向代理
    • Nginx拉取git项目
    • Nginx添加虚拟主机配置文件
    • Nginx项目自动更新
    • Vue-histroy模式跳转路由404问题
    • GoAccess
    • Nginx负载均衡
      • 基本语法
        • 默认状态是按照轮询的方式去做负载的
        • 权重weight
        • fail_timeout backup
  • Docker

  • Server

  • Linux

  • Windows

  • 运维
  • Nginx
HiuZing
2023-03-29
目录

Nginx负载均衡

# 基本语法

upstream的基本语法如下,一个upstream需要设置一个名称,这个名称可以在server里面当作proxy主机使用。

upstream  node {
    server 127.0.0.1:9001;
    server 127.0.0.1:9002;
    server 127.0.0.1:9003;
}

location / {
    proxy_pass http://node;
}
1
2
3
4
5
6
7
8
9

# 默认状态是按照轮询的方式去做负载的

使用express 启动三个服务 分别是9001 9002 9003

点击查看
const express = require('express')
var num = 1
const app = express()
 
app.get('/list',(req,res)=>{
    res.json({
        code:200,
        message:"Nginx 负载均衡9001"
    })
    console.log("Nginx 负载均衡9001",num)
   num++
})
//------------------------------9001
app.listen(9001,()=>{
    console.log('9001 success')
})
 
//-----------------------------------
const express = require('express')
var num = 1
const app = express()
 
app.get('/list',(req,res)=>{
    res.json({
        code:200,
        message:"Nginx 负载均衡9002"
    })
    console.log("Nginx 负载均衡9002",num)
    num++
})
//------------------------------9002
app.listen(9002,()=>{
    console.log('9002 success')
})
 
//--------------------------------
 
const express = require('express')
var num = 1
const app = express()
 
app.get('/list',(req,res)=>{
    
    res.json({
        code:200,
        message:"Nginx 负载均衡9003"
    })
    console.log("Nginx 负载均衡9003",num)
    num++
})
//------------------------------9003
app.listen(9003,()=>{
    console.log('9003 success')
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

经过33次压测之后 平均每个负载为11次证明轮询

image-20230329121910476

# 权重weight

upstream  node {
    server 127.0.0.1:9001 weight=3;
    server 127.0.0.1:9002 weight=2;
    server 127.0.0.1:9003 weight=1;
}
1
2
3
4
5

权重越大服务器承载的并发就越高

# fail_timeout backup

fail_timeout是故障等待超时时间

backup是备用服务器参数,可以为一个upstream设置一个backup的server,在生产server全部都出问题之后,可以自动切换到备用server上,为回复服务争取时间

upstream  node {
    server 127.0.0.1:9001 fail_timeout=60;
    server 127.0.0.1:9002 fail_timeout=20;
    server 127.0.0.1:9003 backup;
}
1
2
3
4
5
上次更新: 2024/08/14, 04:14:33
GoAccess
Docker基础命令

← GoAccess Docker基础命令→

最近更新
01
React Native 使用SVG
08-13
02
Docker基础命令
08-04
03
算数逻辑单元
07-30
更多文章>
Theme by Vdoing | Copyright © 2021-2024 WeiXiaojing | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式