0

I use nginx to load balance my tornado app. There are 3 instances of the same application(listen on different port) on server_A. Below are my configuration.

http {
    upstream myapp1 {
        server server_A_IP:8888;
        server server_A_IP:8887;
        server server_A_IP:8886;
    }

    server {
        listen 80;

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

Now I have another server_B, and also I start 3 instances on this server, so I add 3 lines to my config's upstream.

   upstream myapp1 {
    server server_A_IP:8888;
    server server_A_IP:8887;
    server server_A_IP:8886;

    server server_B_IP:8888;
    server server_B_IP:8887;
    server server_B_IP:8886;
}

This would be ugly if I start 10 instances on each server or add a large amount server to upstream.

Is there any proper way to to this? What is the recommand way to load balance multi server? Thanks!

Song
  • 3
  • 1

1 Answers1

0

A proper way would be to use specialised tool for balancing.

Another way, quite simple, is to have domain name that points to multiple IPs and use that domain name in server directive.

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#server

A domain name that resolves to several IP addresses defines multiple servers at once.

So you would use

upstream myapp1 {
    server upstream.example.net:8886;
    server upstream.example.net:8887;
    server upstream.example.net:8888;
}

and add IPs to DNS when needed.

Alexey Ten
  • 7,922
  • 31
  • 35
  • Thanks, this looks much better. What happend If DNS cache point this host to server_IP_A? Would requests also load balance to server B and C? I am not familiar with DNS, please correct me if I am woring. Thanks – Song Nov 15 '16 at 08:53
  • @billy_lu what do you mean? – Alexey Ten Nov 15 '16 at 10:17
  • Sorry, I did not make it clear. I already know how it works after saw this answer http://stackoverflow.com/a/26957754/3680194. Thanks! – Song Nov 15 '16 at 11:05