Beginner Fundamentals

Configuration Structure

Nginx configuration lives in text files, with nginx.conf as the entry point. The file is organized into nested blocks called contexts.

The Main File

The primary file is usually at /etc/nginx/nginx.conf. It often includes other files from conf.d/ or sites-enabled/ to keep things tidy.

Contexts

A context is a section that groups related settings. They nest inside each other:

# main context (the top level)
worker_processes auto;

events {
    # events context
    worker_connections 1024;
}

http {
    # http context
    include mime.types;

    server {
        # server context
        listen 80;
        server_name example.com;

        location / {
            # location context
            root /var/www/html;
        }
    }
}

What Each Context Does

  • main: top-level settings like worker_processes and the user.
  • events: connection handling, such as worker_connections.
  • http: everything about HTTP traffic; holds one or more server blocks.
  • server: a single virtual host with its own ports and names.
  • location: rules for matching specific request paths.

Settings flow inward: a directive in http applies to every server unless overridden. Keep this nesting in mind as you build configs.