Nginx Web Server: A Beginner’s Guide

Aditya NVAditya NV
5 min read

Nginx is a powerful, high-performance web server widely used for serving static content, acting as a reverse proxy, load balancing, and more. Here’s a broad overview tailored for beginners, covering its history, open-source nature, architecture, configuration, caching, proxies, static site deployment, load balancing, and the algorithms it uses.


A Brief History and Open Source Capabilities

  • Nginx was created by Igor Sysoev and released as open source in 2004 to address the "C10K problem"—the challenge of handling 10,000 concurrent client connections efficiently.

  • From its inception, Nginx focused on maximum performance and minimal resource usage. Over time, it evolved from a fast web server to a versatile application server and API gateway, supporting modern web and cloud architectures.

  • Nginx remains open source, with a commercial version (Nginx Plus) offering additional features. Its open-source model allows extensive customization and community-driven improvements.


Architecture

Nginx uses a modular, event-driven, asynchronous, and non-blocking architecture, which sets it apart from traditional process- or thread-based web servers.

  • Master-Worker Model:

    • The master process reads configuration files and manages worker processes.

    • Worker processes handle actual client requests, each capable of managing thousands of concurrent connections using a highly efficient event loop.

  • Modules:

    • Nginx’s core is lightweight, with most features implemented as modules (core, event, protocol, filter, upstream, and load balancer modules).

    • Modules are compiled with the core at build time, allowing for extensibility without modifying the core code.

  • Efficiency:

    • Utilizes multiplexing and event notifications for high concurrency and low memory usage.

Important Components of an nginx.conf File

The nginx.conf file is the main configuration file for Nginx and is structured using directives and blocks (contexts) to control the server’s behavior. Here are the most important components:

1. Directives

  • Directives are instructions that configure various aspects of Nginx.

  • They can be simple (single-line, ending with a semicolon) or block directives (enclosing other directives within curly braces {}).

2. Contexts (Blocks)

Nginx organizes configuration into hierarchical contexts, each serving a specific purpose:

ContextPurpose
mainTop-level, global settings (e.g., user, worker_processes, error_log)
eventsConfigures connection processing (e.g., worker_connections, event handling methods)
httpContains settings for handling HTTP traffic and includes server and location blocks
serverDefines configuration for a specific virtual host (domain or IP)
locationSpecifies how to process requests for particular URIs within a server block

3. Commonly Used Directives and Blocks

  • user: Sets the system user that Nginx will run as (main context).

  • worker_processes: Number of worker processes (main context).

  • error_log: Path and level for error logging (main context).

  • events: Handles connection settings like worker_connections (events context)56.

  • http: Encloses configuration for web traffic, such as MIME types, logging, and includes for other config files (http context)56.

  • server: Defines virtual hosts, including listen, server_name, root, and SSL settings (http context)56.

  • location: Used inside server blocks to match specific request URIs and apply custom settings (http context)6.

  • include: Allows splitting configuration into multiple files for modularity and maintainability (can be used in various contexts)38.

4. Modular Structure and Inheritance

  • The configuration is tree-like, with contexts nested inside one another.

  • Settings in broader contexts are inherited by nested contexts unless overridden.

  • Array-type directives, if overridden, replace the previous values rather than adding to them.

  • File Organization

    • Main config: /etc/nginx/nginx.conf

    • Additional configs: often included from /etc/nginx/conf.d/*.conf or similar directories for site-specific or feature-specific settings

5. Example Structure

textuser nginx;
worker_processes 1;

events {
    worker_connections 1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    server {
        listen       80;
        server_name  example.com;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
}

Caching

Nginx supports several caching strategies to boost performance:

  • Reverse Proxy Caching:

    • Nginx caches responses from upstream servers, reducing backend load and improving response times.

    • Configured using proxy_cache_path, proxy_cache, and proxy_cache_valid directives.

  • FastCGI Caching:

    • Used when serving dynamic content via FastCGI (e.g., PHP-FPM). Cached responses reduce repeated processing by application servers.

    • Configured using fastcgi_cache_path, fastcgi_cache, and fastcgi_cache_valid directives.

Example: Proxy Cache Configuration

texthttp {
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m;
    server {
        location / {
            proxy_cache my_cache;
            proxy_cache_valid 200 302 60m;
            proxy_pass http://upstream_server;
        }
    }
}

Proxies

Nginx can function as a:

  • Reverse Proxy:

    • Forwards client requests to backend servers, often used to improve security, performance, and scalability.

    • Configured with the proxy_pass directive.

  • Forward Proxy:

    • Less common, but possible with additional configuration.
  • Proxy Protocol Support:

    • Preserves client IP information when passing requests through multiple proxies or load balancers.

Static Page Deployment

  • Nginx excels at serving static files (HTML, CSS, JS, images) directly from the filesystem, making it ideal for static site hosting.

  • Set the root directive to the directory containing your static files, and Nginx will serve them efficiently.

Example: Static Site Configuration

server {
    listen 80;
    server_name mysite.com;
    root /var/www/mysite;
    index index.html;
}

Load Balancing

Nginx offers robust load balancing features, distributing incoming traffic across multiple backend servers to ensure reliability and scalability.

Supported Algorithms:

AlgorithmDescription
Round RobinDefault; requests are distributed evenly in order
Least ConnectionsNew requests go to the server with the fewest active connections
IP HashRequests from the same client IP always go to the same backend
  • Load balancing is configured using the upstream block and the proxy_pass directive.

Example: Load Balancing Configuration

textupstream backend {
    server backend1.example.com;
    server backend2.example.com;
}

server {
    listen 80;
    location / {
        proxy_pass http://backend;
    }
}

Summary Table

FeatureDescription
Open SourceYes, since 2004; extensible via modules
ArchitectureEvent-driven, asynchronous, master-worker model
ConfigurationHierarchical (main, http, server, location); flexible and modular
CachingReverse proxy, FastCGI, and more
Proxy SupportReverse proxy, forward proxy, proxy protocol support
Static Site HostingEfficient, easy to configure
Load BalancingRound robin, least connections, IP hash

Nginx’s performance, flexibility, and open-source nature make it a top choice for web serving, reverse proxying, caching, load balancing, and static site hosting. Its modular architecture and simple configuration syntax allow beginners to get started quickly while offering advanced capabilities for complex deployments.

0
Subscribe to my newsletter

Read articles from Aditya NV directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya NV
Aditya NV