Installation

sudo apt update

sudo apt install nginx -y

After that enable it.

sudo systemctl enable nginx

Then Start it.

sudo systemctl start nginx

Check the status(Running)

sudo systemctl status nginx

It will show this kinds of output.

Image description

To verify the installation, just paste the address in the web browser.

http://localhost

Then we will see the webpage like that. If we paste our IP on the address bar, we will see the same thing.

Image description

Creating Backend servers

For Backend-1, the port is 8083

mkdir -p ~/backend1 && cd ~/backend1
echo 'Backend Server 1' > index.html
python3 -m http.server 8083

Image description

For Backend-2, the port is 8084

mkdir -p ~/backend2 && cd ~/backend2
echo 'Backend Server 2' > index.html
python3 -m http.server 8084

Image description

Configure for Load Balancing

sudo nano /etc/nginx/nginx.conf

Then Configure like that..

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    upstream backend_servers {
        server 127.0.0.1:8083;
        server 127.0.0.1:8084;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://backend_servers;
        }
    }

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

Save the file and exit (Ctrl + X, then Y, then Enter).

Now, Test the config.
sudo nginx -t

it will give a successful text if everything is okay.

Image description

If successful, restart Nginx:
sudo systemctl restart nginx

For the health check, I modify the config file in upstream part and add checking parameters.

upstream backend_servers {
    server 127.0.0.1:8083 max_fails=3 fail_timeout=10s;
    server 127.0.0.1:8084 max_fails=3 fail_timeout=10s;
}

Testing config

Check if Nginx is forwarding requests:
curl -I http://localhost

Image description

Stop any of backend server

Check the port-8083 is running or not.
sudo lsof -i :8083

This will show the PID (Process ID) of the application using that port.

*Same for another port(8084)

Image description

Now, to kill the process. paste the command.

sudo kill -9

For port-8083, PID was 22278.

sudo kill -9 22278

Verify that from the browser. It will show like that.

Image description

Notice that, the server-2 is running perfectly because it is not killed yet.

Image description

If we want to restart server-1.

cd ~/backend1

(# Change this path if your script is elsewhere)
Then run. python3 -m http.server 8083

Monitor Load Balance

verify if Nginx is listening on port 80:sudo ss -tulnp | grep nginx

Log for monitoring: sudo tail -f /var/log/nginx/access.log

Uninstall Nginx
sudo systemctl stop nginx

After stop, remove all file.
sudo apt-get purge nginx nginx-common -y
sudo apt-get autoremove -y

Check if any Nginx files remain: dpkg -l | grep nginx

delete the Nginx configuration directory: sudo rm -rf /etc/nginx