- 🌐 Web Server & Proxy – Nginx serves websites and acts as a reverse proxy, forwarding client requests to backend servers.
- 🚀 High Performance – It is lightweight, efficient, and optimized for handling high traffic with low resource usage.
- 🔄 Load Balancing – Distributes traffic across multiple servers to improve speed and reliability.
- 🔒 Security Features – Supports SSL/TLS encryption, access control, and DDoS protection.
- ⚡ Scalability – Used by large-scale applications like Netflix and GitHub to manage millions of requests.
This project sets up an Nginx reverse proxy with load balancing for a Node.js web application. It includes Dockerization and securing the connection with a self-signed TLS certificate.
sudo apt update && sudo apt install nginx -y
git clone https://github.com/Devisrisamidurai/Nginx-node.js-app.git
cd Nginx-node.js-app
Build and run the Node.js app inside Docker containers:
docker build -t node-app .
docker run -d -p 3001:3000 --name app1 node-app
docker run -d -p 3002:3000 --name app2 node-app
docker run -d -p 3003:3000 --name app3 node-app
Edit the Nginx configuration file:
sudo vi /etc/nginx/nginx.conf
Paste the following configuration:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
upstream nodejs_cluster {
server 127.0.0.1:3001;
server 127.0.0.1:3002;
server 127.0.0.1:3003;
}
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /etc/nginx/ssl/nginx-selfsigned.crt;
ssl_certificate_key /etc/nginx/ssl/nginx-selfsigned.key;
location / {
proxy_pass http://nodejs_cluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
listen 80;
server_name localhost;
location / {
return 301 https://$host$request_uri;
}
}
}
Restart Nginx:
sudo systemctl restart nginx
Create a directory for SSL certificates:
sudo mkdir /etc/nginx/ssl
cd /etc/nginx/ssl
Generate a self-signed certificate:
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout nginx-selfsigned.key -out nginx-selfsigned.crt
Restart Nginx to apply changes:
sudo systemctl restart nginx
📋 Copy
Check if Nginx is running:
sudo systemctl status nginx
📋 Copy
Test HTTPS access in the browser:
https://localhost
If using a self-signed certificate, accept the warning.
You have successfully set up an Nginx reverse proxy with load balancing for a Dockerized Node.js application and secured it with HTTPS.