How to Configure Load Balancing with Nginx

How to Configure Load Balancing with Nginx

Load Balancing is a mechanism for sharing or distributing traffic to multiple servers. Nginx in addition to functioning as a web server can also function as a load balancer.

Load Balancing Method

  1. Round Robin: distributes traffic to each server in turn.
  2. Least Connections: distributes traffic to the server with the least active connections.
  3. IP Hash: distributes the same traffic source to the same server the first time you make a request.

Topologi Load Balancing
Load Balancing Topology

0.Equipment used

Equipment used in this tutorial:

  • OS Ubuntu 18.04 LTS
  • Nginx web server
  • PHP-FPM 7.2
  • Node1:
  • Node2:
  • LoadBalancer:
  • Domain:

1.Install Nginx and PHP-FPM on Node

Install Nginx on Node1 and Node2.

Create file index.php on Node1.

Create file index.php on Node2.

Create server block on Node1 and Node2.

Configuration file for Nginx server block.

Test and restart Nginx.

2.Install and Configure Nginx on Load Balancer

Install Nginx.

Creating a Nginx server block for the load balancing with domain

Nginx server block configuration code for load balancing.

In the upstream backend configuration the IP address of the backend server is written. By default the load balancing method used is Round Robin.

Load balancing configuration if using the Least Connections method.

Load balancing configuration if using the IP Hash method.

Test and restart Nginx.


Browse the domain repeatedly, will display pages from Node1 and Node2 alternately.

Halaman index.php dari Node1
index.php page from Node1
Halaman index.php dari Node2
index.php page from Node2


Leave a Reply

Your email address will not be published. Required fields are marked *