Understand and Write Your First Nginx Configuration

Learn directives, blocks, and includes, then write a separate Nginx configuration you can validate.

🎯 Introduction

Your page is ready, but Nginx does not yet know which port to listen on or where to find the files. This article provides a complete initial configuration that makes those choices explicit.


🧱 Understand the Configuration Language

Nginx configuration consists of directives. Each directive defines a setting, such as worker_processes 1;. Arguments follow the directive name, and simple directives end with a semicolon. Blocks enclose groups of directives in braces. Text after # is a comment for readers and is not executed.

Blocks cannot be nested arbitrarily like folders. Each directive is allowed only in certain locations, called contexts:

ContextPurpose
Top levelInstance-wide settings, such as processes and logs
eventsConnection handling settings
httpShared HTTP request settings
serverA set of addresses, ports, and site rules
locationRules for handling a category of paths within a site

A running instance of a program is a process. The Nginx master process reads configuration and manages worker processes; workers handle connections. One worker is enough to get started. You do not need to tune the worker count yet.


🛠 Write the Main Configuration

Create the main configuration with vim "$HOME/nginx-course/conf/nginx.conf" and save this complete content:

conf/nginx.conf
worker_processes 1;
pid logs/nginx.pid;
error_log logs/error.log notice;

events {
    worker_connections 256;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log logs/access.log;
    client_body_temp_path temp/client;
    proxy_temp_path temp/proxy;
    fastcgi_temp_path temp/fastcgi;
    uwsgi_temp_path temp/uwsgi;
    scgi_temp_path temp/scgi;

    include servers/*.conf;
}

The pid file stores the master process ID so later reload commands can find the right instance. worker_connections limits the number of connections a single worker can open. Proxying also uses connections to backends, so this number is not the number of users you can serve.

The temporary directories whose names contain fastcgi, uwsgi, and scgi belong to other application gateway modules. This course does not use them, but setting their paths in advance avoids attempts to create system temporary directories when you check configuration as a regular user.

The package's mime.types file maps file extensions to content types, so .css files are sent as stylesheets. default_type applies when no type matches; here it means generic binary data.

include reads other configuration files at this point. servers/*.conf is relative to the directory containing the main configuration, so it reads all files ending in .conf under conf/servers/. Relative paths for root, logs, and temporary directories instead use the instance prefix set with -p. These are two different reference directories; neither should simply be interpreted as the terminal's working directory.


🛠 Write the Site Configuration

Run vim "$HOME/nginx-course/conf/servers/site.conf" and save:

conf/servers/site.conf
server {
    listen 127.0.0.1:8080 default_server;
    server_name notes.test;
    charset utf-8;
    root html;
    index index.html;

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

listen sets the address and port that receive requests. 127.0.0.1 restricts access to the local machine. default_server selects this server when no site name matches on that address and port. server_name only matches the site name in requests; it does not register a domain or create DNS records.

charset utf-8 specifies the text encoding, preventing garbled Chinese text. root html points to html in the exercise directory, and index specifies the index file to look for when a directory is requested.

location / matches paths starting with /. The Nginx variable $uri is the current normalized path. try_files checks for files and directories from left to right and returns 404 if none exists. The static website chapter explains file lookup in more detail.


🧰 Check the Configuration

Run this in a terminal where you have defined ngx:

ngx -t

Continue only after seeing syntax is ok and test is successful. -t checks syntax and attempts to open files referenced by the configuration. It does not prove that page content or API logic is correct.

If you see directive is not allowed here, check the braces and the directive's context first. If a file cannot be found, read the full path in the error. Compare your configuration structure with the Nginx beginner's guide.


❓ Questions and Answers

Q1: Should site.conf have another http block around it?

No. The main configuration already includes it inside http. Adding another would create invalid nesting.

Q2: Why do the package's site configurations not affect this instance?

This instance reads only the main configuration we selected and the files it includes. It does not read /etc/nginx/sites-enabled.


🧪 Exercise

Temporarily remove the semicolon from charset utf-8;. Run ngx -t and note the error's line number, then restore the semicolon and check again.

Check your result: explain why a missing semicolon can make the error point to the next directive, and restore both success messages.


🎉 You can now read a configuration in terms of both its hierarchy and its paths!