Configuring Round Robin Load Balancing by Using NGINX

The default load balancing method that's used by NGINX is the round-robin method. This method proxies traffic sequentially to each server in a defined group. This task describes how to configure round-robin load balancing for an example set of servers.

  1. Create a configuration file for the load-balancer at /etc/nginx/conf.d/example.com.conf.

    Create a configuration file for the load-balancer at /etc/nginx/conf.d/example.com.conf, where example.com is the name of the external domain where inbound traffic is directed. The file would contain the following content:

    http
    {
      upstream backend {
        server server1.example.com;
        server server2.example.com;
        server server3.example.com;
      }
    
      server {
        listen 80;
        server_name example.com
                      www.example.com;
        location / {
          proxy_pass  http://backend;
        }
      }
    }
    1. In the upstream backend configuration block, list the backend servers within the environment.

      For example, substitute server1.example.com with the fully qualified domain name or the hostname of a web server instance.

      Set the server_name directive with the domain name or names that you intend to use publicly for the load balanced service. For example, substitute example.com and www.example.com to match the company domain.

    2. Optionally, append more failover options.
      For example, add max_fails and fail_timeout, to the end of each entry to add resilience in case any of the servers goes offline.
  2. Reload the systemd configuration.

    After ensuring that the configuration is valid, enable it by reloading NGINX on the public-facing and backend servers:

    sudo systemctl reload nginx