[Nginx] 使用Proxy實作Load Balancer

Nginx設定:

直接給範例:

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

    server {
        listen 80;

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

簡而言之,在Http設定內註冊Upstream,在Server內使用Proxy Pass調用該Upstream Server Group。


分派演算法:

  • round-robin — requests to the application servers are distributed in a round-robin fashion,
  • least-connected — next request is assigned to the server with the least number of active connections,
  • ip-hash — a hash-function is used to determine what server should be selected for the next request (based on the client’s IP address).

在Upstream內指定:

upstream myapp1 {
        #least_conn;
        #ip_hash;
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

官方文件:http://nginx.org/en/docs/http/load_balancing.html

Leave a Reply

Your email address will not be published. Required fields are marked *