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.

Load Balancing Topology

*0.Equipment used*

Equipment used in this tutorial:

· OS Ubuntu 18.04 LTS · Nginx web server · PHP-FPM 7.2 · Node1: 10.130.127.167 · Node2: 10.130.128.35 · LoadBalancer: 128.199.187.215 · Domain: defnex.com

*1.Install Nginx and PHP-FPM on Node*

Install Nginx on Node1 and Node2.

[INPUT]

1 2 3 sudo apt install nginx php php-fpm -y sudo systemctl status nginx sudo systemctl status php7.2-fpm

Create file index.php on Node1.

[INPUT]

1 2 sudp mkdir /var/www/defnex.com sudo echo “

node1

” > /var/www/defnex.com/index.php

Create file index.php on Node2.

[INPUT]

1 2 sudo mkdir /var/www/defnex.com sudo echo “

node2

” > /var/www/defnex.com/index.php

Create server block on Node1 and Node2.

[INPUT]

1 sudo nano /etc/nginx/conf.d/defnex.com.conf

Configuration file for Nginx server block.

[INPUT]

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 server {     listen 80;       server_name defnex.com www.defnex.com;     root /var/www/defnex.com/;       index index.php index.html index.htm;       location / {         try_files $uri $uri/ /index.php?$query_string;     }       location ~ \.php$ {         try_files $fastcgi_script_name =404;         include fastcgi_params;         fastcgi_pass                    unix:/var/run/php/php7.2-fpm.sock;         fastcgi_index                   index.php;         fastcgi_param DOCUMENT_ROOT     $realpath_root;         fastcgi_param SCRIPT_FILENAME   $realpath_root$fastcgi_script_name;     }       access_log /var/log/nginx/defnex.com.access.log;     error_log /var/log/nginx/defnex.com.error.log warn; }

Test and restart Nginx.

[INPUT]

1 2 3 sudo nginx -t sudo systemctl restart nginx sudo systemctl status nginx

*2.Install and Configure Nginx on Load Balancer*

Install Nginx.

[INPUT]

1 2 sudo apt install nginx -y sudo systemctl status nginx

Creating a Nginx server block for the load balancing with domain defnex.com.

[INPUT]

1 sudo nano /etc/nginx/conf.d/lb-defnex.com.conf

Nginx server block configuration code for load balancing.

[INPUT]

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 upstream backend {     server 10.130.127.167; #node1     server 10.130.128.35;  #node2 }   server {     listen 80;     server_name defnex.com www.defnex.com;       location / {         proxy_redirect      off;         proxy_set_header    X-Real-IP $remote_addr;         proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;         proxy_set_header    Host $http_host;         proxy_pass http://backend;     } }

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.

[INPUT]

1 2 3 4 5 upstream backend { least_conn;     server 10.130.127.167; #node1     server 10.130.128.35;  #node2 }

Load balancing configuration if using the *IP Hash* method.

[INPUT]

1 2 3 4 5 upstream backend { ip_hash;     server 10.130.127.167; #node1     server 10.130.128.35;  #node2 }

Test and restart Nginx.

[INPUT]

1 2 3 sudo nginx -t sudo systemctl restart nginx sudo systemctl status nginx

*3.Testing*

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

index.php page from Node1

index.php page from Node2

Good luck 🙂

Leave a Reply

Your email address will not be published.