3513. Using Nginx as Load Balancer
Nginx, Proxy, and Load Balancing

Introduce how to setup Load Balancer with Nginx for Node Server.

1. Introduction

Load balancing across multiple application instances is a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault-tolerant configurations.

It is possible to use nginx as a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications with nginx.

2. Load Balancing Methods

The following load balancing mechanisms (or methods) are supported in nginx:

  • round-robin: requests to the application servers are distributed in a round-robin fashion,
  • least-connected: next request is assigned to the server with the least number of active connections,
  • ip-hash: a hash-function is used to determine what server should be selected for the next request (based on the client’s IP address). Can be used for Session persistence.

3. Setting up Load Balancer

3.1 Node Servers

First, follow the steps mentioned in Creating Http Server with Node.js to setup three node servers. They are served at different ports, 8086, 8087 and 8088.
Server1. image Server2. image Server3. image

3.2 Nginx Server

Second, follow the steps mentioned in Installing Nginx on macOS to setup a nginx server. It is served at post 9096. image

3.3 Configuring Nginx Server as Load Balancer

Edit Nginx’s configuration file /usr/local/etc/nginx/nginx.conf. Create server list in upstream, and link them to proxy_pass directive in location. Here, we use the default load balancing method: round-robin.

http {
    upstream nodeapp {

    server {
        listen       9096;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://nodeapp/;

Stop and restart nginx, then refresh the web browser. Nginx is now serving as load balancer for the node servers. For the first time, data comes from server1 . image Refresh the web browser, this time, the date comes from server 2. image Refresh again, the date comes from server 3. If you continue refresh, you will see three node servers return data in turn. image

4. Source Files

5. References