Sub topic 1
How Nginx configuration is organized, tested and reloaded
Nginx reads a configuration file, builds an internal configuration, and then uses that configuration to accept connections and route requests. This chapter assumes you already know basic Linux commands, HTTP status codes and headers, TCP ports, and how to edit files with sudo.
Prerequisites
You need:
- Nginx installed from your distribution's package manager.
sudoaccess.- A service listening on a local port, such as
127.0.0.1:3000. - Basic knowledge of files, directories, processes, and systemd.
Configuration structure
The main configuration file is usually /etc/nginx/nginx.conf. The exact path can differ by distribution. Run nginx -V or nginx -t to see the compiled configuration path.
Nginx configuration is organized into contexts. A context is a section where certain directives are valid.
- The main context contains global settings.
- The
eventscontext controls connection processing. - The
httpcontext contains web-server settings. - A
serverblock describes one virtual host, usually selected by hostname and port. - A
locationblock matches part of a request URI.
The main file commonly includes smaller files from directories such as /etc/nginx/conf.d/ or /etc/nginx/sites-enabled/. Splitting sites into separate files makes changes easier to review and roll back.
events {}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}A directive ends with a semicolon. Braces define contexts. Many directives inherit values from an outer context, but some do not. Read the directive documentation when inheritance affects security or request routing.
Include files and site selection
An included file is inserted as if its contents appeared at the include directive. The filename alone does not determine which requests it handles. Nginx selects a server block using the listening address, port, and server_name. It then chooses the best matching location.
Keep one application or site in a clearly named file, such as /etc/nginx/conf.d/example.com.conf. Avoid editing generated files or changing the main file for every application.
Validate before reloading
Use the configuration test before applying a change:
sudo nginx -tA successful test reports that the syntax is okay and the test is successful. The test also checks whether referenced files, certificates, and some paths can be opened. It does not prove that an application is healthy or that every request reaches the intended backend.
To inspect the complete configuration after all includes are expanded, use:
sudo nginx -TThis output is useful when a setting appears to be ignored. Look for duplicate server_name values, an unexpected default server, or a location that matches before the one you expected.
Reload safely
A reload tells the running master process to read the new configuration and start new worker processes. Existing workers finish their current requests before exiting.
sudo nginx -t && sudo systemctl reload nginxThe && operator runs the reload only when the test succeeds. A reload is usually preferable to a restart because it avoids unnecessarily interrupting active connections.
If a reload fails, inspect the service status and journal:
sudo systemctl status nginx --no-pager
sudo journalctl -u nginx -n 50 --no-pagerCheck the error log as well, commonly /var/log/nginx/error.log. After correcting the problem, test again and reload.
| Action | Purpose |
|---|---|
| `nginx -t` | Validate syntax and referenced resources |
| `nginx -T` | Print the expanded configuration |
| `systemctl reload nginx` | Apply a valid configuration with minimal interruption |
| `journalctl -u nginx` | Read service-level errors |
Exercise
Why is sudo nginx -t && sudo systemctl reload nginx safer than running the reload command alone?
Show answer
The reload runs only if Nginx accepts the complete configuration, so a syntax error does not replace the working configuration with an invalid one.