Skip to main content

Nginx Architecture & Installation

Before we start writing configuration files, it is important to understand why Nginx is so fast. Unlike traditional web servers that create a new "thread" or "process" for every user, Nginx uses an Asynchronous, Event-Driven Architecture.

What does "Event-Driven" mean?

In an event-driven model, Nginx can handle multiple requests at the same time without waiting for one to finish before starting the next. This allows it to serve thousands of users with minimal resources.

The Master-Worker Model

Nginx operates using a hierarchical process model. It divides the labor to ensure that if one part of the system is busy, the rest of the server stays responsive.

1. The Master Process

The "Manager" of the server. Its main jobs are:

  • Reading and validating the configuration files (nginx.conf).
  • Starting, stopping, and managing Worker Processes.
  • Performing privileged operations (like opening network ports).

2. The Worker Processes

The "Employees" who do the heavy lifting.

  • They handle the actual network connections.
  • They read and write content to the disk.
  • They communicate with backend servers (like your Node.js API).
  • Industrial Standard: Usually, you run one worker process per CPU core on your server.

The Event Loop (Why it Scales)

In a typical MERN app, a user might have a slow internet connection. A traditional server would "block" and wait for that user to finish. Nginx doesn't wait. It uses an Event Loop.

This allows a single Nginx worker to handle thousands of concurrent users with very low RAM usage.

Installation Guide

At CodeHarborHub, we recommend learning Nginx on a Linux environment (Ubuntu) or using Docker for consistent local development.

1. Update your package index:

sudo apt update

2. Install Nginx:

sudo apt install nginx -y

3. Verify Installation:

nginx -v
# Output: nginx version: nginx/1.18.0 (Ubuntu)

4. Check Service Status:

sudo systemctl status nginx

Managing the Nginx Service

Once installed, you will use these commands constantly. Bookmark these!

CommandActionWhen to use?
sudo systemctl start nginxStart NginxAfter a reboot or manual stop.
sudo systemctl stop nginxStop NginxTo take the server offline.
sudo systemctl restart nginxHard RestartTo apply major system changes.
sudo systemctl reload nginxGraceful ReloadBest Practice: Apply config changes without dropping user connections.

Verification: The "Welcome" Page

After installation, Nginx automatically starts a default website. If you navigate to your server's IP address (or localhost), you should see:

Firewall Check

On AWS or local Linux, if you can't see the page, ensure Port 80 is open in your Security Groups or ufw firewall: sudo ufw allow 'Nginx Full'