Skip to main content

Configuration

NGINX uses a centralized, hierarchical configuration system. Instead of per-directory files (like Apache’s .htaccess), all behavior is controlled from configuration files.

Main Configuration Components

  1. nginx.conf → Global & core settings
  2. sites-available/ → All website configurations (inactive & active)
  3. sites-enabled/ → Active websites (symbolic links)

nginx.conf (Main Configuration File)

Location

/etc/nginx/nginx.conf

Purpose

  • Main entry point for NGINX
  • Defines global settings
  • Controls:
    • User & worker processes
    • Event handling
    • HTTP behavior
    • Inclusion of other config files

Basic nginx.conf Structure

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
worker_connections 1024;
}

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

sendfile on;
keepalive_timeout 65;

include /etc/nginx/sites-enabled/*;
}
  • user www-data;
    • User that NGINX worker processes run as
    • Improves security by avoiding root privileges
  • worker_processes auto;
    • Automatically uses the number of CPU cores
    • Improves performance
  • events { } Controls how NGINX handles connections.
events {
worker_connections 1024;
}
  • One worker can handle 1024 connections

  • Total connections = workers × connections

  • http { } Main HTTP configuration block.

    Inside it:

    • Logging
    • Compression
    • Virtual hosts
    • MIME types
    • Caching
  • include /etc/nginx/sites-enabled/\*;

    • Loads all active websites
    • Critical line for enabling virtual hosts

sites-available Directory

Location

/etc/nginx/sites-available/

Purpose

  • Stores all website configuration files
  • Can contain:
    • Active sites
    • Inactive sites
    • Backup or test configs

Files here are NOT active by default.

Example: Website Configuration in sites-available

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

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

location / {
try_files $uri $uri/ =404;
}

}
  • server → Virtual host
  • listen 80 → HTTP port
  • server_name → Domain names
  • root → Website directory
  • location / → Handles all requests
  • try_files → Prevents invalid URLs

Why sites-available Is Important

  • Clean organization
  • Easy enable/disable websites
  • Safe testing without affecting production

sites-enabled Directory

Location

/etc/nginx/sites-enabled/

Purpose

  • Contains symbolic links to files in sites-available
  • Only these configs are loaded by NGINX

Example: Enable a Site

sudo ln -s /etc/nginx/sites-available/example \
/etc/nginx/sites-enabled/

This creates:

sites-enabled/example → sites-available/example
  • Symbolic link avoids duplication
  • Deleting the link disables the site
  • Original config remains safe

Disable a Site

sudo rm /etc/nginx/sites-enabled/example
sudo systemctl reload nginx

How These Files Work Together

Request Flow

Client Request

nginx.conf

http block

sites-enabled/*

server block match

location block match