What is Nginx ?
Vincent Tommi

Vincent Tommi @vincenttommi

About: python Developer

Location:
Nairobi-Kenya
Joined:
Apr 2, 2023

What is Nginx ?

Publish Date: May 16
0 0

How NGINX Works

  1. Web Server
    NGINX is asynchronous and event-driven, meaning it can handle many requests using minimal resources. When a static file is requested (like an HTML, CSS, or image file), NGINX reads it from the disk and returns it directly to the client.

  2. Acting as a Reverse Proxy: The Middleman with Superpowers
    One of the most common and powerful uses of NGINX is as a reverse proxy. In this role, NGINX doesn’t serve the content itself. Instead, it acts as an intermediary between the client and one or more backend servers — such as those running Node.js, Python, Ruby, PHP, or any other application framework.

Here's how it works:

A client sends an HTTP request to your web application.

NGINX receives the request first, acting as the front-facing gateway.

Based on its configuration, NGINX forwards the request to the appropriate backend server.

The backend server processes the request (e.g., querying a database or rendering a web page) and sends the response back to NGINX.

NGINX then returns the response to the original client.

Key Advantages of Using NGINX as a Reverse Proxy:

Load Balancing: Distributes traffic across multiple backend servers using strategies such as round-robin, least connections, or IP hash.

Caching: Caches responses from backend servers to improve speed and reduce load.

SSL Termination: Handles HTTPS encryption/decryption to offload that work from backend servers.

Security: Hides backend server IPs, reducing the attack surface.

  1. Load Balancer NGINX can balance client requests across multiple backend servers, enhancing performance and fault tolerance. It supports several algorithms:

Round Robin: Requests are distributed sequentially.

Least Connections: New requests are sent to the server with the fewest active connections.

IP Hash: Requests from the same client IP are always sent to the same server.

  1. Content Caching
    NGINX can cache frequently requested content (both static and dynamic) to reduce the load on backend servers and improve response times for users.

  2. Handling Concurrency
    Traditional servers like Apache use a thread- or process-per-connection model, which becomes inefficient under heavy load.

NGINX, on the other hand, uses a non-blocking, event-driven architecture:

A small number of worker processes handle many thousands of concurrent connections.

Each worker uses an event loop and non-blocking I/O.

This design significantly reduces memory and CPU usage and allows NGINX to scale easily, even on modest hardware.

Setting up NGINX as a Web Server
In this section, we'll walk through setting up NGINX on a Linux machine (Ubuntu/Debian or CentOS/RHEL). We'll also provide steps for local testing.

  1. Install NGINX On Ubuntu/Debian:
sudo apt update
sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

On CentOS/RHEL:

sudo yum install epel-release
sudo yum install nginx
Enter fullscreen mode Exit fullscreen mode

Start and Enable the Service:

sudo systemctl start nginx
sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode
  1. Verify the Installation Open your browser and visit:
http://<your-server-ip>
Enter fullscreen mode Exit fullscreen mode

You should see the default NGINX Welcome Page, confirming it's running correctly.

  1. Serve Static Website Files Default Root Directories: Ubuntu: /var/www/html CentOS: /usr/share/nginx/html Replace the default index.html with a custom message:
echo "<h1>Hello from NGINX</h1>" | sudo tee /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

Refresh your browser — you should see your custom message.

  1. Configure a New Server Block (Virtual Host) To host a custom domain (e.g., example.com):

Ubuntu:

sudo nano /etc/nginx/sites-available/example.com
Enter fullscreen mode Exit fullscreen mode

CentOS:

sudo nano /etc/nginx/conf.d/example.com.conf
Enter fullscreen mode Exit fullscreen mode

Add the following configuration:

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/example.com;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Create the Web Root and Add Content
sudo mkdir -p /var/www/example.com
echo "<h1>Welcome to example.com</h1>" | sudo tee /var/www/example.com/index.html
Enter fullscreen mode Exit fullscreen mode
  1. Enable the Server Block (Ubuntu Only)
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode
  1. Test and Reload NGINX
sudo nginx -t    # Check for syntax errors
sudo systemctl reload nginx

Enter fullscreen mode Exit fullscreen mode
  1. Update DNS or /etc/hosts for Local Testing If DNS isn’t set up yet, edit your local/etc/hosts:
sudo nano /etc/hosts
Enter fullscreen mode Exit fullscreen mode

Add this line:

127.0.0.1 example.com
Enter fullscreen mode Exit fullscreen mode

✅ Done!

Now, visit http://example.com in your browser — you should see your custom web page served by NGINX

Comments 0 total

    Add comment