Sub topic 1
Where Nginx configuration lives
Prerequisites
This chapter assumes you can:
- Use the Linux command line, a text editor, and
sudo. - Read basic file permissions and ownership.
- Run Nginx from the distribution packages under systemd.
- Describe how DNS maps names to IP addresses, and what the HTTP
Hostheader is.
Paths below follow Debian and Ubuntu packages. Where RHEL-family systems (Fedora, Rocky, AlmaLinux) differ, the text says so.
Why layout matters before server blocks
Hosting several sites on one Nginx means editing many small files. If you do not know which file Nginx reads first, which files it includes, and which directives are allowed where, you will edit a file Nginx ignores or put a directive in the wrong place. This chapter maps the layout. Later chapters fill in the server blocks.
The main file: nginx.conf
Nginx reads one entry-point file at startup and at every reload. For package installs this is /etc/nginx/nginx.conf. You can confirm which file your binary uses with nginx -V, which prints the compiled-in --conf-path.
nginx -V 2>&1 | tr ' ' '\n' | grep -E '^--(conf-path|error-log-path|http-log-path|pid-path)'The nginx.conf file is made of directives. A directive is either a simple directive (a name, parameters, and a semicolon) or a block directive (a name, parameters, and braces containing more directives). Blocks nest, and a block that contains other directives is called a context.
Contexts
Each directive is valid only in certain contexts. The ones you need for multi-site hosting are these:
| Context | Where it sits | What it holds |
|---|---|---|
| main | Top level of `nginx.conf` | Process-wide settings such as `user`, `worker_processes`, `pid`, and the global `error_log` |
| events | Top level | Connection handling settings such as `worker_connections` |
| http | Top level | Everything about HTTP: logging formats, gzip, TLS defaults, and all server blocks |
| server | Inside `http` | One virtual host: its `listen`, `server_name`, and site settings |
| location | Inside `server` (or another `location`) | Settings for requests whose URI matches a pattern |
Settings in an outer context are inherited by inner contexts unless the inner one sets its own value. This is why a directive such as access_log placed in http applies to every site, and why a site can override it by setting access_log inside its own server block. A few directives, such as add_header and proxy_set_header, are inherited only if the inner block defines none of its own. Defining one at an inner level replaces the whole inherited set. Keep this in mind when you share settings between sites.
A typical Debian or Ubuntu nginx.conf
The packaged file is short. This trimmed copy shows the parts that matter here:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}Three details in this file affect every site you add:
user www-data;is the account the worker processes run as. This account must be able to read your document roots. Permission problems are covered in the next chapter and in troubleshooting.includedirectives pull other files in at that exact position, as if their text were pasted there. The two includes insidehttpare where your site files come from.- The order of includes sets the order in which Nginx reads server blocks. This matters in one case: when several blocks tie for a match, or when nothing sets
default_server, the first block read for an address and port becomes the default.
The include directories
Debian and Ubuntu packages use two include directories side by side. RHEL-family packages use one.
| Path | Used by | Purpose |
|---|---|---|
| `/etc/nginx/conf.d/*.conf` | All distributions | Drop-in files. Only names ending in `.conf` are read. |
| `/etc/nginx/sites-available/` | Debian, Ubuntu | Every site file you have written, enabled or not. Nginx does not read this directory. |
| `/etc/nginx/sites-enabled/` | Debian, Ubuntu | Symlinks to files in `sites-available`. Nginx reads everything here. |
| `/etc/nginx/snippets/` | Debian, Ubuntu | Reusable fragments that you include by name. |
| `/etc/nginx/modules-enabled/` | Debian, Ubuntu | Loading of dynamic modules. |
The include pattern matters. conf.d/*.conf reads only files ending in .conf. sites-enabled/* reads every entry, whatever its name, so a backup file such as example.com~ or example.com.bak in that directory is loaded too. That usually causes duplicate server block warnings or conflicting settings.
On RHEL-family systems there is no sites-available or sites-enabled. Put one .conf file per site in /etc/nginx/conf.d/. To disable a site, rename it so it no longer ends in .conf, or move it elsewhere.
Why sites-available and sites-enabled exist
The two-directory scheme is a convention of the Debian packaging, not a feature of Nginx. Nginx only sees the result of the include. The scheme gives you a cheap on/off switch:
- Write the file once in
sites-available. - Enable it by creating a symlink in
sites-enabled. - Disable it by removing the symlink. The real file stays.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
sudo rm /etc/nginx/sites-enabled/example.comUse absolute paths when creating the link. A relative target is resolved relative to the link's own directory, which is a common source of broken links. The next chapters walk through the full enable-and-reload workflow.
How include resolves paths and globs
Relative include paths are resolved from the directory of the main configuration file, which is /etc/nginx/ for package installs. Wildcards are expanded by Nginx and the matches are read in alphabetical order. This means file names control ordering. If you want one file to be read before the others, give it a name that sorts first, such as 00-defaults.conf.
An include of a pattern that matches no files is not an error, but an include of a single named file that does not exist is. The same applies to a symlink whose target is gone. The error appears when you test the configuration.
Other places Nginx files live
Configuration is only one part of what you will touch. These are the paths to know on a package install:
| Item | Typical path |
|---|---|
| Main log directory | `/var/log/nginx/` |
| Default access log | `/var/log/nginx/access.log` |
| Default error log | `/var/log/nginx/error.log` |
| Default document root (Debian/Ubuntu) | `/var/www/html` |
| Default document root (RHEL family) | `/usr/share/nginx/html` |
| PID file | `/run/nginx.pid` |
| systemd unit | `/lib/systemd/system/nginx.service` (or `/usr/lib/systemd/system/`) |
Do not edit the unit file. If you need to change how systemd runs Nginx, use sudo systemctl edit nginx, which writes an override that survives package upgrades.
The default site
On Debian and Ubuntu, installing the package creates /etc/nginx/sites-available/default and links it into sites-enabled. It listens on port 80 with default_server and serves /var/www/html. That is the welcome page you see when you first browse to the server's IP address.
You can leave it in place, but it will answer for any host name that no other block claims. Later chapters show how to replace it with a deliberate catch-all. For now, note that it exists and that disabling it with sudo rm /etc/nginx/sites-enabled/default changes which block answers unknown names.
Inspecting the configuration Nginx actually uses
The files on disk are what you wrote. The running configuration is what Nginx assembled from all includes. The -T option tests the configuration and then prints the full result, with each file's path as a comment before its content.
sudo nginx -T 2>&1 | grep -E '^# configuration file'This lists every file that was read. If your new site file is not in the list, Nginx is not including it. The usual causes are a missing symlink, a file name that does not match the include pattern, or an include line that was removed from nginx.conf.
To find every server_name currently loaded:
sudo nginx -T 2>/dev/null | grep -E '^\s*server_name'Run the plain syntax check often:
sudo nginx -tA successful check prints syntax is ok and test is successful. A failure names the file and line number. A passing test does not prove the site behaves correctly, only that the syntax and referenced files are valid.
Flow of a configuration load
The diagram shows what Nginx reads and how files from the two directories end up inside the http context.
Common mistakes with layout
- Editing a file in
sites-availableand expecting a change. The file is only read if a symlink insites-enabledpoints to it. Check withls -l /etc/nginx/sites-enabled/. - Reloading without testing. Always run
nginx -tfirst. A reload with a broken configuration is refused and the old configuration keeps running, but you will not notice unless you read the error. - Leaving editor backups in
sites-enabled. Files such assite~orsite.swpget included because the pattern is*. - Placing a
serverblock outsidehttp. Nginx reports that theserverdirective is not allowed here. Check that the file is included from insidehttp, not from the main context. - Putting the same
listenandserver_namepair in two files. Nginx warns about a conflicting server name and ignores the second one. The warning appears innginx -toutput. - Editing the package's
nginx.confheavily. Package upgrades may ask to replace it. Keep site-specific settings in separate files so the main file stays close to the default.
Summary of the layout
nginx.confis the single entry point. Everything else is reached throughinclude.- Server blocks live inside
http. Sites come fromsites-enabled/on Debian and Ubuntu, or fromconf.d/everywhere. - Enabling a site is creating a symlink. Disabling it is removing the symlink.
nginx -tchecks syntax.nginx -Talso shows the merged result and every file that was read.
Exercise
You created /etc/nginx/sites-available/example.net and reloaded Nginx, but requests for example.net still reach the default site. sudo nginx -t reports no errors. Which command shows whether Nginx read your file, and what is the most likely cause?
Show answer
Run sudo nginx -T 2>&1 | grep -E '^# configuration file' and look for /etc/nginx/sites-available/example.net or its symlink path under sites-enabled. If it is not listed, the most likely cause is a missing symlink in /etc/nginx/sites-enabled/. Nginx only reads that directory, so create it with sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/example.net, then run sudo nginx -t && sudo systemctl reload nginx.
Exercise
On a Rocky Linux server you put a site file at /etc/nginx/conf.d/blog.conf.bak to keep a backup. Will Nginx read it, and why?
Show answer
No. The include pattern on these systems is conf.d/.conf, which matches only names ending in .conf. A file ending in .bak is skipped. On a Debian or Ubuntu system the sites-enabled/ pattern would read it, which is why backups do not belong there.