nginx官网关于负载均衡模块的文档帮助:链接
1.nginx配置文件
[root@lb01 ~]# cd /application/nginx-1.6.2/
[root@lb01 nginx-1.6.2]# egrep -v "^$|#" conf/nginx.conf.default > conf/nginx.conf
- 1
- 2
2.官方示例配置及解释
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
upstream:模块 不允许修改
backend:名称 可修改,我使用带有“_”的符号名字,会报400,原因不知。
server backend1.example.com: 写监听的域名或者ip
weight:权重,权重越大,分发到的任务越多
server unix:/tmp/backend3:可以使用socket
server backup1.example.com:8080 backup:热备,nginx自带的高可用,当上面监听的
两个服务器都挂掉了,就由热备的提供服务。
proxy_pass http://backend: 使用负载均衡
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
3.实际配置
[root@lb01 nginx-1.6.2]# cat -n conf/nginx.conf
1 worker_processes 1;
2 events {
3 worker_connections 1024;
4 }
5 http {
6 include mime.types;
7 default_type application/octet-stream;
8 sendfile on;
9 upstream lbproxy {
10 server 10.0.0.12:80 weight=5;
11 server 10.0.0.13:80 weight=5;
12 server 10.0.0.11:80 backup;
13 }
14 keepalive_timeout 65;
15 server {
16 listen 80;
17 server_name lb.liang.com;
18 location / {
19 root html;
20 index index.html index.htm;
21 proxy_pass http://lbproxy;
22 }
23 }
24 }
- 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
4.检查语法,重启nginx
[root@lb01 nginx-1.6.2]# nginx -t
nginx: the configuration file /usr/local/nginx-1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.6.2/conf/nginx.conf test is successful
[root@lb01 nginx-1.6.2]# nginx -s reload
- 1
- 2
- 3
- 4
5.测试
[root@lb01 nginx-1.6.2]# echo "10.0.0.10 lb.liang.com" >> /etc/hosts
[root@lb01 nginx-1.6.2]# for num in `seq 10`;do curl lb.liang.com;done
10.0.0.12
10.0.0.13
10.0.0.12
10.0.0.13
10.0.0.12
10.0.0.13
10.0.0.12
10.0.0.13
10.0.0.12
10.0.0.13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
6.修改权重测试
[root@lb01 nginx-1.6.2]# vim conf/nginx.conf
10 server 10.0.0.12:80 weight=1;
11 server 10.0.0.13:80 weight=5;
12 server 10.0.0.11:80 backup;
13 }
[root@lb01 nginx-1.6.2]# nginx -s reload
[root@lb01 nginx-1.6.2]# for num in `seq 10`;do curl lb.liang.com;done
10.0.0.13
10.0.0.13
10.0.0.12
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.12
10.0.0.13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
7.ip_hash(使用宇保持会话)
7.1使用ip_hash一个ip只会调用到一台服务器
10 ip_hash;
11 server 10.0.0.12:80 weight=1;
12 server 10.0.0.13:80 weight=5;
13 server 10.0.0.11:80 backup;
14 }
- 1
- 2
- 3
- 4
- 5
7.2这时候检查语法会有错
[root@lb01 nginx-1.6.2]# nginx -t
nginx: [emerg] invalid parameter "backup" in /usr/local/nginx-1.6.2/conf/nginx.conf:13
nginx: configuration file /usr/local/nginx-1.6.2/conf/nginx.conf test failed
- 1
- 2
- 3
7.3需要删掉热备,ip_hash不支持热备
10 ip_hash;
11 server 10.0.0.12:80 weight=1;
12 server 10.0.0.13:80 weight=5;
13 #server 10.0.0.11:80 backup;
14 }
- 1
- 2
- 3
- 4
- 5
7.4重启,测试
[root@lb01 nginx-1.6.2]# nginx -s reload
[root@lb01 nginx-1.6.2]# for num in `seq 10`;do curl lb.liang.com;done
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13
10.0.0.13