Configuration File Structure

Beginner
6 min read
Updated: Nov 2026
3 examples
nginx.conf

Definition

Nginx reads a text config file (usually nginx.conf). Directives tell each module what to do. Semicolons end simple lines; braces { } open contexts.

Common nesting: main → events / http → server → location. Put each directive in the context the docs allow.

Config

Start with this shape. Adjust paths for your server.

Nginx
events { }

http {
    server {
        listen 80;
        server_name localhost;

        location / {
            root /var/www/html;
            index index.html;
        }
    }
}

Text after # is a comment. Include files with include /etc/nginx/conf.d/*.conf; to keep sites separate.

Key Directives

NamePlain meaning
events { }Connection handling settings (main context).
http { }HTTP settings shared by all virtual servers.
server { }One virtual host: listen port, name, locations.
location { }Rules for a URL path prefix or regex.
includePull in other config files.

Examples

Example 1 — See the Context Nesting

A directive outside any braces is in the main context. server lives inside http.

Nginx
worker_processes auto;   # main

events {                 # main → events
    worker_connections 1024;
}

http {                   # main → http
    include mime.types;

    server {             # http → server
        listen 80;
        location / {     # server → location
            root /data/www;
        }
    }
}

How It Works

  1. worker_processes belongs in main.
  2. listen belongs in server.
  3. root often sits in server or location.

Example 2 — Simple vs Block Directives

Simple directives end with ;. Block directives wrap more directives in braces.

Nginx
listen 80;                    # simple
root /var/www/html;            # simple

location /images/ {            # block (context)
    root /data;
}

How It Works

  1. Simple = name + parameters + semicolon.
  2. Block = name + optional params + { ... }.
  3. Contexts are blocks that may contain other directives.

Example 3 — Split Sites with include

Package installs often include conf.d or sites-enabled so each site is its own file.

Nginx
http {
    include /etc/nginx/mime.types;
    include /etc/nginx/conf.d/*.conf;
    # Ubuntu also uses:
    # include /etc/nginx/sites-enabled/*;
}

How It Works

  1. One file per site is easier to enable or disable.
  2. Test with nginx -t after every edit.
  3. Reload only when the test passes.

Frequently Asked Questions

Common paths: /etc/nginx/nginx.conf, /usr/local/nginx/conf/nginx.conf, or /usr/local/etc/nginx/nginx.conf.
A block that can hold other directives, such as http, server, or location.
No. listen belongs in the server context.
Everything after # on a line is ignored.
They keep the main file short and let you enable one site without editing everything else.

Next: Static content

Keep going through the beginner Nginx path.

Static content →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful