1

Load balancers allow you to route client requests across multiple servers in order to maximize speed and capacity utilization and ensure that no one server is overworked. NGINX is one such load balancer that also acts as a web server and reverse proxy.

If you want to load balance between three servers using NGINX, you might have something like this in your nginx.conf configuration file.

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

    server {
        listen 80;

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

Here, you hardcode the three servers you want to load balance between in the configuration file. If you want to load balance between more servers, you have to add a line for each additional server you want to add, meaning you have to hardcode all the servers into your configuration file.

Is there a way to dynamically and easily scale up the number of servers you want to load balance between? How does load balancing scale as the load on your servers increase?

grenmester
  • 95
  • 10

1 Answers1

0

From the NGINX upstream documentation;

server address [parameters];

Defines the address and other parameters of a server. The address can be specified as a domain name or IP address, with an optional port, or as a UNIX-domain socket path specified after the “unix:” prefix. If a port is not specified, the port 80 is used. A domain name that resolves to several IP addresses defines multiple servers at once.

Create a DNS record, that points to multiple IP addresses;

lb.example.com
    --> 10.0.1.20
    --> 10.0.1.21
    --> 10.0.1.22
    --> 10.0.1.23
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • I think it's worth mentioning that NGINX will not check for changes in the record unless you have NGINX Plus and added a 'resolve' option. – anemyte Mar 27 '21 at 06:15
  • 2
    As mentioned by @anemyte the OSS Version has limited features when it comes to dynamic upstream configuration. I have build a solution based on a go-script creating a `upstream.conf` file and reload the nginx worker after doing so. Reloading nginx is most cases not a problem unless you have hundrets of certs or huge configuration files. Let me know if this would be a use case for happy. Happy to share the code. – Timo Stark Mar 27 '21 at 08:05
  • 1
    I was wondering if there is a solution that is within NGINX. The first solution moves the load balancing to the DNS level outside of NGINX and the second solution uses a script that lies outside of NGINX. Seems like doing dynamic load balacning within NGINX is not possible. – grenmester Mar 29 '21 at 11:33
  • 1
    It is absolutley possible but not with the Free (OpenSource) Version of NGINX. NGINX Plus (The paid version of NGINX) will be able to do it. https://docs.nginx.com/nginx/admin-guide/load-balancer/dynamic-configuration-api/ – Timo Stark Mar 30 '21 at 10:24
  • 1
    As @Timo mentioned, this is only available out-of-the-box on NGINX Plus. They even provide API's to control those sort of things. An other, as you already mentioned, could be some sort of bash script to alter the NGINX config file. – 0stone0 Apr 02 '21 at 12:57