nginx负载均衡一配置

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

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: 使用负载均衡

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  }

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

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

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

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     }

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

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     }

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

猜你喜欢

转载自blog.csdn.net/liang_operations/article/details/81611101
今日推荐