Logo

dev-resources.site

for different kinds of informations.

Load Balancing Node.js Applications with Nginx Upstream Configuration

Published at
1/11/2025
Categories
programming
node
docker
Author
nhannguyendevjs
Categories
3 categories in total
programming
open
node
open
docker
open
Author
15 person written this
nhannguyendevjs
open
Load Balancing Node.js Applications with Nginx Upstream Configuration

As web applications grow in traffic and complexity, ensuring their reliability, scalability, and performance becomes critical. Node.js, a popular server-side platform, often runs on lightweight, single-threaded processes. While efficient, this can create bottlenecks when handling a high volume of requests. Load balancing offers a solution, and Nginx, a high-performance web server and reverse proxy, provides robust capabilities for distributing traffic to Node.js applications.

This blog post explores how to configure Nginx to load balance a Node.js application using the upstream directive.

Why Load Balancing?

Load balancing distributes incoming traffic across multiple servers to:

1. Enhance Performance: By spreading requests, you avoid overwhelming a single Node.js instance.

2. Increase Availability: If one server goes down, traffic can be routed to others.

3. Enable Scalability: Adding more servers to your backend can easily handle growing traffic demands.

Nginx acts as an intermediary between clients and your backend servers, ensuring seamless traffic distribution and improving the overall user experience.

Setting Up Nginx for Load Balancing

Prerequisites

Node.js Application: Have a Node.js app running on multiple instances, each on a different port (e.g., localhost:3000, localhost:3001, localhost:3002).

Nginx Installed: Ensure Nginx is installed on your server. Use sudo apt install nginx for Ubuntu-based systems.

Step-by-Step Configuration

1. Define Upstream Servers

Edit the Nginx configuration file (e.g., /etc/nginx/nginx.conf or a site-specific file in /etc/nginx/sites-available). Add the upstream block to define your Node.js instances:

http {
    upstream node_app {
        server localhost:3000;
        server localhost:3001;
        server localhost:3002;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://node_app;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Configure Load Balancing Methods

Nginx supports multiple load balancing strategies. The default is Round Robin, which distributes requests sequentially. To customize:

▪️Least Connections: Directs traffic to the server with the fewest active connections.

upstream node_app {
    least_conn;
    server localhost:3000;
    server localhost:3001;
    server localhost:3002;
}
Enter fullscreen mode Exit fullscreen mode

▪️IP Hash: Ensures requests from the same client are routed to the same server.

upstream node_app {
    ip_hash;
    server localhost:3000;
    server localhost:3001;
    server localhost:3002;
}
Enter fullscreen mode Exit fullscreen mode

3. Test and Reload Nginx

After editing the configuration, test it for syntax errors:

sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

If the test passes, reload Nginx:

sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Advanced Configurations

1. Health Checks: Ensure only healthy servers receive traffic.

upstream node_app {
    server localhost:3000;
    server localhost:3001;
    server localhost:3002;
    health_check;
}
Enter fullscreen mode Exit fullscreen mode

2. SSL Termination: Terminate HTTPS at Nginx and forward HTTP to Node.js.

server {
    listen 443 ssl;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

    location / {
        proxy_pass http://node_app;
        proxy_set_header Host $host;
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Sticky Sessions: Use cookies to ensure a client sticks to the same server.

upstream node_app {
    sticky;
    server localhost:3000;
    server localhost:3001;
    server localhost:3002;
}
Enter fullscreen mode Exit fullscreen mode

Monitoring and Scaling

▪️Monitoring: Use tools like Nginx logs or third-party solutions (e.g., Datadog) to monitor traffic and server health.

▪️Scaling: To handle increased traffic, add more Node.js instances and update the upstream block.

Conclusion

Configuring Nginx as a load balancer for Node.js applications is a powerful way to enhance performance, reliability, and scalability. By defining upstream servers and customizing load balancing strategies, you can tailor your setup to meet the demands of your application.

With this configuration in place, your application is well-equipped to handle growth and deliver a seamless user experience. As traffic patterns evolve, continue to monitor and optimize your setup to ensure peak performance.


I hope you found it helpful. Thanks for reading. 🙏
Let's get connected! You can find me on:

node Article's
30 articles in total
Favicon
Breaking the Scale Barrier: 1 Million Messages with NodeJS and Kafka
Favicon
assert in Nodejs and its usage in Grida source code
Favicon
Understanding Node.js Cluster: The Core Concepts
Favicon
🌟 A New Adventure Begins! 🛵🍕
Favicon
How “Silicon Valley” Inspired Me to Create a Photo Compressor CLI for Web Developers
Favicon
How Does This Jewelry Customization Site Work? Need Insights!
Favicon
Building a Secure Authentication API with TypeScript, Node.js, and MongoDB
Favicon
Understanding OAuth 1.0a Signature Generation: Postman vs. Node.js Library and Custom Implementation
Favicon
How to Fix the “Record to Delete Does Not Exist” Error in Prisma
Favicon
[Boost]
Favicon
Run NextJS App in shared-hosting cPanel domain!
Favicon
Construindo uma API segura e eficiente com @fastify/jwt e @fastify/mongodb
Favicon
New ways to engage with the stdlib community!
Favicon
Sore Throat: Causes, Symptoms, and Treatment
Favicon
Back to MonDEV 2025
Favicon
🌟 How to Fix Node.js Path Issues in VS Code (Step-by-Step Guide)
Favicon
How to write unit tests and E2E tests for NestJS applications
Favicon
Cookies auto clearing after browser refresh issue , CORS related express cookies issue
Favicon
Exploring TypeScript Support in Node.js v23.6.0
Favicon
Mastering Backend Node.js Folder Structure, A Beginner’s Guide
Favicon
how to setup express api from scratch
Favicon
Load Balancing Node.js Applications with Nginx Upstream Configuration
Favicon
Using LRU Cache in Node.js and TypeScript
Favicon
Welcome to Siitecch! Your Go-To Platform for Beginner-Friendly Full-Stack JavaScript Learning.
Favicon
I Really like Middleware in NodeJs/Express.
Favicon
Your own Telegram bot on NodeJS with TypeScript, Telegraf and Fastify (Part 3)
Favicon
Understanding Node.js Cluster: The Core Concepts
Favicon
JWT Authentication With NodeJS
Favicon
Stream de Arquivo PDF ou Imagem S3 - AWS
Favicon
Understanding Node.js Alpine Versions: A Lightweight Choice for Your Projects

Featured ones: