Facebook Pixel

Nginx Load Balancing Updates Adding and Removing Backend Instances

Learn how to update Nginx to add or remove backend Node.js instances for load balancing, with zero-downtime scaling.

Nginx Load Balancing Updates

As your traffic grows, you need to add more Node.js instances. This guide covers updating Nginx for load balancing.

Initial Load Balancing Config

upstream nodejs_backend { server 127.0.0.1:3000; }

Adding More Instances

upstream nodejs_backend { server 127.0.0.1:3000; server 127.0.0.1:3001; server 127.0.0.1:3002; }
# Start new instances PORT=3001 pm2 start src/server.js --name api-2 PORT=3002 pm2 start src/server.js --name api-3 # Update Nginx sudo nginx -t && sudo systemctl reload nginx

Load Balancing Methods

# Round-robin (default) upstream backend { server 127.0.0.1:3000; server 127.0.0.1:3001; } # Least connections upstream backend { least_conn; server 127.0.0.1:3000; server 127.0.0.1:3001; } # IP hash (for WebSockets/sessions) upstream backend { ip_hash; server 127.0.0.1:3000; server 127.0.0.1:3001; } # Weighted (for different capacity servers) upstream backend { server 127.0.0.1:3000 weight=3; server 127.0.0.1:3001 weight=1; }

Health Checks

upstream backend { server 127.0.0.1:3000 max_fails=3 fail_timeout=30s; server 127.0.0.1:3001 max_fails=3 fail_timeout=30s; }

Removing an Instance (Zero Downtime)

# Mark as down (Nginx stops sending new requests) upstream backend { server 127.0.0.1:3000; server 127.0.0.1:3001 down; # Temporarily down }
# Reload Nginx sudo systemctl reload nginx # Stop the instance pm2 stop api-2 # Remove from config when done sudo nano /etc/nginx/sites-available/devtinder # Remove the 'down' line sudo systemctl reload nginx

The Takeaway

To update Nginx load balancing: add new servers to the upstream block, choose a method (round-robin, least_conn, ip_hash, weighted), set health checks (max_fails, fail_timeout), use 'down' to remove instances with zero downtime, and reload after each change. For WebSockets, always use ip_hash for sticky sessions.

Add a new line to the upstream block: server 127.0.0.1:3001;. Start the Node.js instance on that port (PORT=3001 pm2 start src/server.js). Test with nginx -t and reload. Nginx automatically distributes traffic to the new instance.

Mark the server as 'down' in the upstream block: server 127.0.0.1:3001 down;. Reload Nginx (stops sending new requests to it). Wait for existing requests to finish. Stop the instance (pm2 stop). Remove the line from config and reload again.

Round-robin (default, distributes in order) for most cases. least_conn for instances with different response times. ip_hash for WebSockets and sessions (same client always goes to same instance). weighted for instances with different capacities.

Add max_fails and fail_timeout to each server: server 127.0.0.1:3000 max_fails=3 fail_timeout=30s. Nginx removes the server after 3 failed attempts within 30 seconds and tries again after 30 seconds. No external health check endpoint needed.

Yes, ip_hash is critical for WebSockets. It ensures the same client always connects to the same Node.js instance. Without it, the initial HTTP upgrade and subsequent WebSocket frames might go to different instances, causing connection failures. Also use the Redis adapter for cross-instance messaging.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.