0
0
Nginxdevops~5 mins

Why load balancing distributes traffic in Nginx - Why It Works

Choose your learning style9 modes available
Introduction
When many users visit a website or app, one server can get overwhelmed and slow down. Load balancing helps by spreading the visitors evenly across multiple servers so no single server gets too busy.
When your website gets more visitors than one server can handle smoothly
When you want to keep your app running even if one server stops working
When you want to improve the speed of your website by sharing the work
When you run multiple copies of the same app on different servers
When you want to avoid downtime during server maintenance
Config File - nginx.conf
nginx.conf
http {
    upstream myapp {
        server 192.168.1.101;
        server 192.168.1.102;
        server 192.168.1.103;
    }

    server {
        listen 80;

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

This configuration creates a group of servers called myapp with three backend servers.

The proxy_pass directive sends incoming requests to this group.

Nginx will distribute the traffic among these servers to balance the load.

Commands
Check if the nginx configuration file syntax is correct before applying it.
Terminal
sudo 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 load balancing configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Send a request to the nginx server to verify it forwards traffic to one of the backend servers.
Terminal
curl -I http://localhost
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Thu, 01 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: load balancing spreads user requests across multiple servers to keep your app fast and reliable.

Common Mistakes
Not testing nginx configuration before reloading
A syntax error can stop nginx from restarting, causing downtime.
Always run 'sudo nginx -t' to check configuration syntax before reloading.
Using only one backend server in the upstream block
Load balancing won't work if there is only one server; traffic won't be distributed.
Add at least two backend servers in the upstream block to enable load balancing.
Reloading nginx without applying the new config file
Nginx will continue using the old configuration, so load balancing changes won't take effect.
Save the configuration file with correct settings before reloading nginx.
Summary
Configure nginx with an upstream block listing multiple backend servers.
Test the nginx configuration syntax with 'nginx -t' before applying changes.
Reload nginx to apply the load balancing setup without downtime.
Verify traffic is served correctly by sending requests to nginx.