close

Nginx除代理外也具有Load balance功能,透過upstream來設定,算是非常簡易的設定方式

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

    server {
        listen 80;

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

 

而進行Load balance的分派邏輯主要有三種:

1.Round-robin : 白話就是輪流,沒額外設定的話是預設選項。

2.Least-connected : 透過計數器找出當下連線數最少的那一台。

3.ip-hash : 根據來源位置用hash算出分配到的機器,假如app有保留session認證或token等各種類似的session persistence功能的話要選這個,不然每一次配到的目的地不會相同。

 

設定方式直接加在upstream裡面

   upstream myapp {
        least_conn;
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

 

除此之外,TCP的Socket Server一樣可以透過nginx來進行Load balance

stream {
   server {
      listen 9999;
      proxy_pass my_servers;
   }

   upstream my_servers {
      server 10.1.1.14:9000;
      server 15.99.99.199:16888;
   }
}
如此就可以將連入port 9999的對象,平均的分配到10.1.1.14:9000與15.99.99.199:16888

 

 

來源官方文件:

http://nginx.org/en/docs/http/load_balancing.html

arrow
arrow
    文章標籤
    nginx load balance upstream
    全站熱搜

    不來嗯 發表在 痞客邦 留言(0) 人氣()