Logo

dev-resources.site

for different kinds of informations.

Nginx Simplified: Technical Insights with Real-World Analogies

Published at
1/13/2025
Categories
linux
nginx
devops
webserver
Author
shweta_thikekar
Categories
4 categories in total
linux
open
nginx
open
devops
open
webserver
open
Author
15 person written this
shweta_thikekar
open
Nginx Simplified: Technical Insights with Real-World Analogies

What is Nginx?

Nginx is an open-source, high-performance web server that also acts as a reverse proxy, load balancer, and HTTP cache. It’s designed to handle a high number of concurrent connections efficiently.

Analogy:

Imagine a busy restaurant:

  • Web Server Role: Nginx is the chef, serving meals (webpages) directly.
  • Reverse Proxy Role: It’s the receptionist, passing orders (user requests) to the right chef in the kitchen.
  • Load Balancer Role: It’s the manager, ensuring chefs share the workload evenly.
  • Cache Role: It’s the fridge, keeping popular dishes ready to serve quickly.

Installing Nginx

Nginx runs on Linux. Let’s install it.

Ubuntu/Debian:

sudo apt update
sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode

CentOS/RHEL:

sudo yum install nginx -y
Enter fullscreen mode Exit fullscreen mode

Analogy:
Installing Nginx is like building the restaurant’s infrastructure, setting up tables, and opening for business.

Basic Configuration

Technical:

The main configuration file is:

/etc/nginx/nginx.conf

Key parts:

http {}: Handles web traffic configuration.
server {}: Defines how to respond to requests for a domain.
location {}: Specifies rules for URLs.

Example:

server {
    listen 80;
    server_name example.com;

    location / {
        root /var/www/html;
        index index.html;
    }
}
Enter fullscreen mode Exit fullscreen mode

Analogy:

Think of nginx.conf as the restaurant’s recipe book:

  • http: General guidelines for all recipes.
  • server: Recipe for a specific dish (domain).
  • location: Special instructions for certain ingredients (URLs).

Hosting a Website

Technical:

Go to the web root directory:
cd /var/www/html

Create an index.html file:
echo "<h1>Welcome to Nginx!</h1>" | sudo tee index.html

Restart Nginx:
sudo systemctl restart nginx

Open your browser and go to http://localhost to see your page.

Analogy:

This is like creating the restaurant’s menu (website). Now anyone can come and order food (visit your site).

Reverse Proxy

Technical:

A reverse proxy forwards client requests to backend servers. It hides the servers from the users.

Example Configuration:

server {
    listen 80;
    server_name myproxy.com;

    location / {
        proxy_pass http://127.0.0.1:5000;
    }
}
Enter fullscreen mode Exit fullscreen mode

Analogy:

Imagine the receptionist (Nginx) doesn’t cook but takes orders and gives them to the kitchen (backend servers). The customer only interacts with the receptionist.

Load Balancing

Nginx distributes requests among multiple backend servers to balance the load.

Example Configuration:

http {
    upstream backend {
        server 192.168.1.10;
        server 192.168.1.11;
    }

    server {
        listen 80;
        server_name myloadbalancer.com;

        location / {
            proxy_pass http://backend;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Analogy:

This is like having multiple chefs in the kitchen. The manager (Nginx) assigns each chef an equal number of orders.

Caching

Technical:

Caching stores frequently requested content to serve it faster.

Enable Caching:

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

server {
    location / {
        proxy_cache my_cache;
        proxy_pass http://backend;
        add_header X-Cache-Status $upstream_cache_status;
    }
}
Enter fullscreen mode Exit fullscreen mode

Analogy:

Caching is like prepping popular dishes ahead of time so they’re ready to serve instantly.

HTTPS with SSL

Technical:

Enable secure communication with HTTPS using SSL certificates.

Generate a certificate:

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt
Enter fullscreen mode Exit fullscreen mode

Update Nginx:

server {
    listen 443 ssl;
    server_name mywebsite.com;

    ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;

    location / {
        root /var/www/html;
        index index.html;
    }
}
Enter fullscreen mode Exit fullscreen mode

Analogy:

HTTPS is like adding a security guard to the restaurant, ensuring all communication is safe and encrypted.

Debugging and Logs

Technical:

Check logs for debugging:

Access logs: /var/log/nginx/access.log
Error logs: /var/log/nginx/error.log
Enter fullscreen mode Exit fullscreen mode

Test configurations:

sudo nginx -t

Analogy:

Logs are like feedback cards from customers. They help you know if something is going wrong.

Load Balancing Algorithms in Nginx

Nginx supports multiple algorithms for distributing traffic among backend servers. The choice of algorithm depends on the scenario and configuration. Here's a list of commonly used load balancing algorithms:

Load Balancing Algorithms in Nginx
Nginx supports multiple algorithms for distributing traffic among backend servers. The choice of algorithm depends on the scenario and configuration. Here's a list of commonly used load balancing algorithms:

1. Round Robin (Default)

How it works: Requests are distributed sequentially to each backend server in turn.
Use case: Best for equally capable backend servers with no need for complex logic.

Example:

upstream backend {
    server 192.168.1.10;
    server 192.168.1.11;
}
Enter fullscreen mode Exit fullscreen mode

2. Least Connections
How it works: Sends requests to the server with the least number of active connections.

3. IP Hash
How it works: Distributes requests based on the client’s IP address. Ensures a client is always routed to the same server.

4. Generic Hash
How it works: Routes requests based on a specified key (e.g., a URL, cookie, or header).

Conclusion

Nginx is a powerful and versatile tool, capable of handling complex web server, proxy, load balancing, and caching needs with ease. Its event-driven architecture makes it an ideal choice for high-performance environments, and its flexibility allows it to cater to varied use cases, from hosting a simple static website to serving as a reverse proxy for microservices.

By mastering its features, such as load balancing algorithms, HTTPS setup, and caching mechanisms, you can optimize your application for scalability, security, and speed. Whether you're a beginner starting with installation or an experienced DevOps engineer diving into advanced configurations, Nginx offers endless possibilities for enhancing your infrastructure.

nginx Article's
30 articles in total
Favicon
nginx-mod-http-geoip
Favicon
How to run a Nginx-web server
Favicon
ngx whitelist/blacklist module
Favicon
Nginx Simplified: Technical Insights with Real-World Analogies
Favicon
Nginx Configuration Tips for Secure Communication: Enabling mTLS and checking client fingerprint
Favicon
Building a Scalable Reverse Proxy Server like Nginx with Node.js and TypeScript
Favicon
Deploy NestJS and NextJS application in same server using pm2 and Nginx
Favicon
Setting Up an NGINX Reverse Proxy with a Node.js Cluster Using Docker
Favicon
การทำ HTTPS ด้วย Certbot และ Nginx บน Ubuntu Server
Favicon
How to configure Free SSL Certificate on Nginx using Certbot
Favicon
Docker Hands-on: Learn Docker Volume and Bind Mounts with Sample Projects using NGINX
Favicon
自建的git远程仓库,在push时413 Request Entity Too Large
Favicon
Optimize SvelteKit performance with brotli compression
Favicon
I’m running a Spring Boot application inside a Docker container on my VM. The application works fine over HTTP, and I can access all endpoints via http://127.0.0.1:8080. I’ve set up NGINX as a reverse proxy to serve HTTPS requests. No errors for http reqs.
Favicon
Deploying a MERN App on Azure: The Smart Way
Favicon
My First Full-Stack Deployment with Docker and NGINX as Load Balancer
Favicon
Streamlined Release Process for a Web Application: Trunk-Based Development with Feature Flags
Favicon
How to Install NGINX on Ubuntu 22.04
Favicon
Secure Nginx with Let's Encrypt on Ubuntu
Favicon
Kubernetes Ingress Controllers and NGINX Ingress: A Complete Guide
Favicon
What is HTTP 499 Status Code and How to Fix it?
Favicon
Docker Compose Demo: Running Multiple Services with Two Domains on Localhost
Favicon
Building a Production Stack: Docker, Meilisearch, NGINX & NestJS
Favicon
Step-by-Step Guide: Assigning a Namecheap Domain to DigitalOcean Hosting with Nginx
Favicon
Streamlining React CI/CD Pipelines with GitHub Actions
Favicon
Connecting to an EC2 Instance with Ubuntu and Installing NGINX on AWS
Favicon
Installing Nginx Web Server on Linux: A Step-by-Step Guide
Favicon
Hosting multiple Websites on a single Nginx Server
Favicon
Unleashing the Power of NGINX as an API Gateway
Favicon
Installing Wordpress with Nginx in Ubuntu

Featured ones: