nginx配置-之负载均衡

round-robin           轮询方式,即轮流调度,默认方式
least-connected       最小连接
ip-hash                  IP hash,基于客户端的IP来选择服务器,一般用在保持会话的场景

例1:基本配置

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

例2:使用最小连接数
upstream myapp1 {
        least_conn;
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

例3:使用 ip hash
upstream myapp1 {
    ip_hash;
    server srv1.example.com;
    server srv2.example.com;
    server srv3.example.com;
}

例4:权重的使用
 upstream myapp1 {
        server srv1.example.com weight=3;
        server srv2.example.com;
        server srv3.example.com;
    }

注意:使用权重,可以更加灵活的定制负载均衡算法,只有在最新的nginx版本中,最小连接和ip hash才支持 weight(权重)























猜你喜欢

转载自www.cnblogs.com/aishangwei/p/9153100.html