nginx中的负载均衡 ( load balancing in nginx)

访问量: 1469

refer to:  http://nginx.com/resources/admin-guide/load-balancer/

很简单, 自己之前的这篇文章:http://siwei.me/blog/posts/nginx-and-thin-to-server-rails-app

     server {
         listen 80;
         charset utf-8;
         location / {
             proxy_pass          http://rails_servers;
        }
     }
     upstream rails_servers{
            server 127.0.0.1:6661;
            server 127.0.0.1:6662;
            server 127.0.0.1:6663;
            server 127.0.0.1:6664;
     }

可以看出,重点就在于如何配置 rails_servers, 以及 proxy_pass 这个指令(directive)

关于负载均衡的策略,有如下几种:

1. weight( 权重,这个是默认的。见下面的例子)

upstream backend {
   server backend1.example.com;
   server backend2.example.com;
}

2. least_conn: 下一个请求,会发送给所有负载中,负载最少的那个:

upstream backend {
    least_conn;

    server backend1.example.com;
    server backend2.example.com;
}

3. ip_hash: 如果多个request来自于同样的ip,那么这些request都会被转发到一个backend server上去。

upstream backend {
    ip_hash;

    server backend1.example.com;
    server backend2.example.com;
}

订阅/RSS Feed

Subscribe

分类/category