nginx 负载均衡

准备

  • 虚拟机one:192.168.30.135:80
  • 虚拟机two:192.168.30.128:80
  • 两个虚拟机装的nginx都是:nginx version: nginx/1.18.0
  • 官网参考地址: http://nginx.org/

 

配置代理

  • 在各虚拟机上的conf.d文件夹下建立nginx配置文件,名字分别为:“xuniji_one.conf”、“xuniji_two.conf”
  • 反向代理我写在了xuniji_one.conf中,如下图:
    • 图中参数介绍:

      • upstream后面的名称与proxy_pass后面的地址对应。(名称可以随意写)

      • upstream中的两个server地址就是两个服务器的地址。
      • proxy_pass:设置后端代理服务器的地址。这个地址(address)可以是一个域名或ip地址和端口,或者一个 unix-domain socket路径。
      • proxy_set_header:就是可设置请求头-并将头信息传递到服务器端。

      

测试

打开浏览器,访问设置代理的服务器(www.xuniji.one.com)。每次访问最终都是请求不同的服务器。

 

 

 

知识点补充

nginx的负载均衡策略有六种:

1、轮询(默认策略,nginx自带策略):我上面的例子就是轮询的方式,它是upstream模块默认的负载均衡默认策略。会将每个请求按时间顺序分配到不同的后端服务器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
http {
    upstream xuniji_fuzai {
        server 192.168.30.128:80;
        server 192.168.30.135:80;
    }
  
    server {
        listen 81;
        server_name www.xuniji.one.com;
  
        location / {
            proxy_pass http://xuniji_fuzai;
            proxy_set_header Host $proxy_host;
        }
    }
}

  

2、weight(权重,nginx自带策略):指定轮询的访问几率,用于后端服务器性能不均时调整访问比例。权重越高,被分配的次数越多。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
http {
    upstream xuniji_fuzai {
        server 192.168.30.128:80 weight=7;
        server 192.168.30.135:80 weight=2;
    }
  
    server {
        listen 81;
        server_name www.xuniji.one.com;
  
        location / {
            proxy_pass http://xuniji_fuzai;
            proxy_set_header Host $proxy_host;
        }
    }
}

  

3、ip_hash(依据ip分配,nginx自带策略):指定负载均衡器按照基于客户端IP的分配方式,这个方法确保了相同的客户端的请求一直发送到相同的服务器,可以解决session不能跨服务器的问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
http {
    upstream xuniji_fuzai {
        ip_hash;
        server 192.168.30.128:80;
        server 192.168.30.135:80;
    }
  
    server {
        listen 81;
        server_name www.xuniji.one.com;
  
        location / {
            proxy_pass http://xuniji_fuzai;
            proxy_set_header Host $proxy_host;
        }
    }
}

  

4、least_conn(最少连接,nginx自带策略):把请求转发给连接数较少的后端服务器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
http {
    upstream xuniji_fuzai {
        #把请求转发给连接数比较少的服务器
        least_conn;
        server 192.168.30.128:80;
        server 192.168.30.135:80;
    }
  
    server {
        listen 81;
        server_name www.xuniji.one.com;
  
        location / {
            proxy_pass http://xuniji_fuzai;
            proxy_set_header Host $proxy_host;
        }
    }
}   

  

5、fair(第三方):按照服务器端的响应时间来分配请求,响应时间短的优先分配。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
http {
    upstream xuniji_fuzai {
        fair;
        server 192.168.30.128:80;
        server 192.168.30.135:80;
    }
  
    server {
        listen 81;
        server_name www.xuniji.one.com;
  
        location / {
            proxy_pass http://xuniji_fuzai;
            proxy_set_header Host $proxy_host;
        }
    }
}  

  

6、url_hash(第三方):该策略按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,需要配合缓存用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
http {
    upstream xuniji_fuzai {
        hash $request_uri;
        server 192.168.30.128:80;
        server 192.168.30.135:80;
    }
  
    server {
        listen 81;
        server_name www.xuniji.one.com;
  
        location / {
            proxy_pass http://xuniji_fuzai;
            proxy_set_header Host $proxy_host;
        }
    }

posted on   itjeff  阅读(51)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示