Skip to content

Devisrisamidurai/Nginx-node.js-app

Repository files navigation

Nginx for Node.js App

What is Nginx?

  • 🌐 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.

Installation Steps

1️⃣ Install Nginx on Linux

sudo apt update && sudo apt install nginx -y

2️⃣ Clone the Repository

git clone https://github.com/Devisrisamidurai/Nginx-node.js-app.git
cd Nginx-node.js-app

3️⃣ Dockerize the Node.js Application 🐳

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

4️⃣ Configure Nginx as Reverse Proxy with Load Balancing 🔄

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

5️⃣ Secure Connection with Self-Signed TLS Certificate 🔐

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

6️⃣ Verify Setup ✅

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.

🎯 Conclusion

You have successfully set up an Nginx reverse proxy with load balancing for a Dockerized Node.js application and secured it with HTTPS.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published