nginx 负载均衡的三种参数设置

时间:2022-09-08 09:46:04

1、

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
user nobody;
worker_processes 4;//一般是CPU的核心数
events{
  worker_connections 1024;
}
 
http{
  upstream mypro {
    server 182.13.32.12;
    server 213.11.23.24;
  }
 
  server {
    listen 8080;
    location / {
      proxy_pass http://mypro;
    }
  }
}

2、加入ip_hash,让同一个用户落到同一台机器上

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use nobody;
worker_processes 4;//一般是CPU的核心数
events{
  worker_connections 1024;
}
 
http{
  upstream mypro {
    ip_hash;
    server 182.13.32.12;
    server 213.11.23.24;
  }
 
  server {
    listen 8080;
    location / {
      proxy_pass http://mypro;
    }
  }
}

3、加入权重

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use nobody;
worker_processes 4;//一般是CPU的核心数
events{
  worker_connections 1024;
}
 
http{
  upstream mypro {
    server 182.13.32.12 weight=2;//被访问到的概率
    server 213.11.23.24;
  }
 
  server {
    listen 8080;
    location / {
      proxy_pass http://mypro;
    }
  }
}

负载均衡的进程可以单独启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/fzjh.conf