Nginx负载均衡
zhaolengquan Lv3

什么是反向代理服务器

反向代理应该是Nginx使用最多的功能了,反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

简单来说就是真实的服务器不能直接被外部网络访问,所以需要一台代理服务器,而代理服务器能被外部网络访问的同时又跟真实服务器在同一个网络环境,当然也可能是同一台服务器,端口不同而已。
反向代理通过proxy_pass指令来实现。

Nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#HTTP里的配置
upstream mydata {
#mydata是自己起的名字,可以随便起,下面 location的proxy_pass 有用到
# weight是权重
# 权重的数值越大,被分配到的几率也更大
server localhost:8181 weight=2;
server localhost:8182 weight=1;
}

server {
listen 80; #服务器监听80端口
server_name localhost;

location / {
root html;
index index.html index.htm;

# 监听地址, mydata 即用户访问的地址
proxy_pass http://mydata ;
}

引入Thymeleaf视图解析器必要的start依赖

1
2
3
4
5
6
7
8
9
10

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Java代码配置

localhost:8181

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class NginxController {

@RequestMapping("/")
public String test() {
return "Nginx";
}
}

Nginx.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>
localhost8181: hello Nginx
</h1>

</body>
</html>

application.yml

1
2
3
4
5
6
7
8
server:
port: 8181

mvc:
static-path-pattern: /**
web:
resources:
static-locations: classpath:/templates/,classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

localhost:8182

controller不需要改变,application.yml里port改成8182

html稍做变化

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>
localhost8182: hello Nginx
</h1>

</body>
</html>

用Maven把项目打成Jar包后 修改html和端口再次打包

image-20220316084127306

java -jar SpringBoottest-0.0.1-SNAPSHOT.jar

java -jar SpringBoottest-0.0.1-SNAPSHOT2.jar

访问Nginx localhost:80

刷新能得到不同的效果

image-20220316085139800 image-20220316085158548

Nginx负载均衡的几种方式

普通轮询算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    upstream mydata {
server localhost:8181
server localhost:8182
}

server {
listen 80; #服务器监听80端口
server_name localhost;

location / {
root html;
index index.html index.htm;

# 监听地址, mydata 即用户访问的地址
proxy_pass http://mydata ;
}

基于比例加权平均

1
2
3
4
upstream mydata {
server localhost:8181 weight=5;
server localhost:8182 weight=2;
}

基于IP路由负载

上面两种方式都有一个问题 如果我们的请求不是无状态的时候(采用Session保存数据),比如我们把登录信息保存到了Session中,那么跳转到另外一台服务器的时候,就需要重新登陆了,此时可以在upstream 配置中加一行 ip_hash, ip_hash首先把每一个请求 先获得请求的ip地址映射成一个hash值,这样的话每个ip都可以固定访问一台后端服务器,解决了Session跨域的问题

1
2
3
4
5
upstream mydata {
server localhost:8181 weight=5;
server localhost:8182 weight=2;
ip_hash;
}

基于服务器响应时间负载分配

根据服务器处理请求的时间来进行负载,处理请求越快,也就是响应时间越短的优先分配。

1
2
3
4
5
upstream mydata {
server localhost:8181 weight=5;
server localhost:8182 weight=2;
fair;
}

对不同域名实现负载均衡

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
    upstream AdminConfig {
server localhost:8181
server localhost:8182
}

upstream StudentConfig {
server localhost:8181
server localhost:8182
}

server {
listen 80; #服务器监听80端口
server_name localhost;

location /Admin/ {
root html;
index index.html index.htm;
# 监听地址, mydata 即用户访问的地址
proxy_pass http://AdminConfig ;
}

location /Student/ {
root html;
index index.html index.htm;
# 监听地址, mydata 即用户访问的地址
proxy_pass http://StudentConfig ;
}
 Comments