Nginx: Core Concepts and Configuration

Basic Directives and Configuration Files

Nginx configuration is built on a structured format consisting of key directives that control how the server handles connections, processes requests, and manages content delivery. Understanding these directives is essential for configuring and optimizing Nginx effectively.

  • Key Directives:

    • worker_processes: Defines the number of worker processes handling client requests. More workers improve performance for high-traffic sites.
    • events {}: Manages connection handling, ensuring efficient request processing.
    • http {}: The main section where web server settings, including caching, logging, and compression, are defined.
    • server {}: Defines settings for individual websites or applications, such as domains, ports, and document roots.
    • location {}: Specifies how Nginx handles different URLs and routes requests.
  • Checking the Nginx Configuration: Before applying changes, validate the configuration syntax:

    nginx -t
  • Reloading Nginx After Changes: Apply configuration updates without downtime:

    sudo systemctl reload nginx

HTTP Configuration and Server Blocks

Server blocks (also known as virtual hosts) define the behavior of Nginx for different domains and applications. They allow multiple websites to run on a single server with separate configurations.

  • Defining Server Blocks:

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

    This example sets up a basic web server that listens on port 80 and serves content from /var/www/example.

  • Managing Multiple Server Blocks: Each domain or application should have its own server {} block to ensure isolation.

  • Enabling and Disabling Server Blocks: Use symbolic links to activate or deactivate configurations without modifying files:

    ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Reverse Proxy and Load Balancing

Nginx can act as a reverse proxy, forwarding client requests to backend servers, improving security and performance.

  • Setting Up a Reverse Proxy:

    server {
        listen 80;
        server_name example.com;
        location / {
            proxy_pass http://127.0.0.1:5000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

    This setup forwards all incoming requests to a backend application running on port 5000.

  • Implementing Basic Load Balancing: Distributing traffic across multiple servers improves availability and performance:

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

    This configuration routes incoming requests to multiple backend servers in a round-robin fashion.

Static and Dynamic Content Handling

Nginx is highly efficient at serving static files, such as HTML, CSS, JavaScript, and images, while also supporting dynamic applications through reverse proxying.

  • Serving Static Content:

    location /static/ {
        root /var/www/html;
    }

    This example configures Nginx to serve static files from /var/www/html when requests match /static/.

  • Proxying Requests to Dynamic Applications: Nginx can forward requests to backend services like PHP, Python, or Node.js applications:

    location / {
        proxy_pass http://localhost:3000;
    }

    This setup directs all requests to a locally running application on port 3000.

Hands-On Exercise

To reinforce these concepts, try the following hands-on tasks:

  • Create and configure multiple server blocks for different domains.
  • Set up Nginx as a reverse proxy for a backend application.
  • Configure static and dynamic content handling to serve both files and web applications.
  • Test and reload Nginx configuration to apply changes effectively.

By mastering these core concepts, you’ll gain the foundational knowledge needed to deploy and manage Nginx effectively for various use cases.