Sub topic 1
Where Nginx Configuration Lives
Prerequisites
This chapter assumes that Nginx is installed from your Linux distribution's packages and is running under systemd. You should be comfortable using sudo, navigating the command line, and editing text files. You should also know that /etc contains system-wide configuration files.
The main configuration file
Nginx starts with /etc/nginx/nginx.conf. This is the main configuration file. It usually contains global settings, event settings, HTTP settings, and include directives that load additional files.
A typical structure looks like this:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}An include directive inserts the contents of matching files into the configuration. This keeps the main file small and lets you manage separate sites independently.
Debian and Ubuntu layout
Debian and Ubuntu packages commonly use these directories:
| Path | Purpose |
|---|---|
| `/etc/nginx/nginx.conf` | Main configuration file |
| `/etc/nginx/sites-available/` | Site configurations available to enable |
| `/etc/nginx/sites-enabled/` | Site configurations currently loaded |
| `/etc/nginx/snippets/` | Reusable configuration fragments |
| `/etc/nginx/conf.d/` | Additional files ending in `.conf` |
A site file normally lives in sites-available. A symbolic link, or symlink, in sites-enabled makes that site active. A symlink is a filesystem entry that points to another path.
flowchart TD
A["/etc/nginx/nginx.conf"] --> B["include sites-enabled/*"]
B --> C["sites-enabled/example.com"]
C -. "symlink" .-> D["sites-available/example.com"]
A --> E["include conf.d/*.conf"]
A --> F["include snippets when referenced"]This layout separates storing a site from activating it. Removing a symlink disables a site without deleting its configuration file.
Other distribution layouts
Red Hat Enterprise Linux, Rocky Linux, AlmaLinux, and Fedora commonly use /etc/nginx/conf.d/ for virtual host files. A file such as /etc/nginx/conf.d/example.com.conf is loaded when the main configuration includes conf.d/*.conf.
The same Nginx directive can work in either layout. The difference is how the distribution organizes and includes files. Do not add a second include for the same directory unless you have checked that it is not already present. Loading the same definitions twice can produce conflicts.
Inspecting the active configuration
Use nginx -T to print the complete configuration after Nginx expands all include directives:
sudo nginx -TThis is useful when you need to find which file defines a setting or confirm that a site file is actually loaded. To check syntax without changing the running service, use:
sudo nginx -tA successful test reports that the syntax is okay and the test is successful. A failed test includes the file and line number that needs attention.
Exercise
Exercise
On a Debian or Ubuntu server, where should you store a site configuration that is not yet active, and how can you inspect whether Nginx loads it?
Show answer
Store it in /etc/nginx/sites-available/. Create a symlink in /etc/nginx/sites-enabled/ to activate it, then run sudo nginx -T and search the output for the site's directives.