0
0
Nginxdevops~5 mins

Round-robin (default) in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you have multiple servers to handle web traffic, you want to share the work evenly. Round-robin is a simple way to send each new visitor to the next server in line. This helps keep your website fast and reliable by not overloading one server.
When you want to balance web traffic evenly across several servers to avoid overload.
When you have multiple identical backend servers serving the same content.
When you want a simple, automatic way to distribute user requests without complex rules.
When you want to improve website availability by spreading requests in a cycle.
When you want to add or remove servers easily without changing the load balancing method.
Config File - nginx.conf
nginx.conf
http {
    upstream myapp {
        server backend1.example.com;
        server backend2.example.com;
        server backend3.example.com;
    }

    server {
        listen 80;

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

The upstream block defines a group of backend servers named myapp. Each server line adds one backend server to the group.

The server block listens on port 80 and forwards all requests to the myapp group using proxy_pass.

By default, Nginx uses round-robin to send each new request to the next server in the list.

Commands
Check the Nginx configuration file for syntax errors before applying changes.
Terminal
nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload Nginx to apply the new configuration without stopping the service.
Terminal
systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Send a request to Nginx to verify it forwards requests to backend servers using round-robin.
Terminal
curl -I http://localhost
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 05 Jun 2024 12:00:00 GMT Content-Type: text/html Content-Length: 612 Connection: keep-alive
Key Concept

If you remember nothing else from this pattern, remember: round-robin sends each new request to the next server in order, balancing load evenly by default.

Common Mistakes
Forgetting to reload Nginx after changing the configuration.
Nginx keeps using the old settings until reloaded, so changes have no effect.
Always run 'systemctl reload nginx' or 'nginx -s reload' after editing the config.
Not checking configuration syntax before reloading.
Syntax errors can cause Nginx to fail to reload, leading to downtime.
Run 'nginx -t' to verify the config is valid before reloading.
Listing backend servers with wrong hostnames or IPs.
Nginx cannot forward requests if backend servers are unreachable.
Use correct and reachable server addresses in the upstream block.
Summary
Define backend servers in an upstream block to group them for load balancing.
Use proxy_pass in a server block to forward requests to the upstream group.
Reload Nginx after configuration changes to apply the new load balancing setup.