Sub topic 6
Enabling a site: validate with nginx -t, then reload
This chapter assumes you can use the Linux command line and sudo, and that Nginx is installed from distribution packages and runs under systemd. It also assumes you know what a server block is and what listen and server_name do. The commands use the Debian and Ubuntu layout, where site files live in sites-available and sites-enabled.
By the end you will be able to create a site, enable it, check the configuration, and reload Nginx without dropping connections. You will also know what to do when the check fails.
The workflow in one picture
Enabling a site is a short, repeatable loop. You write the file, link it, test it, and reload only if the test passes.
Step 1: Create the document root and a test page
Each site needs a directory that holds its files. A visible test page for each site makes it easy to tell later which site answered a request.
sudo mkdir -p /var/www/example.com/html /var/www/example.org/html
echo '<h1>example.com</h1>' | sudo tee /var/www/example.com/html/index.html
echo '<h1>example.org</h1>' | sudo tee /var/www/example.org/html/index.htmltee runs under sudo here because a plain > redirect is performed by your own shell, which has no permission to write in /var/www. Nginx worker processes must be able to read these files. The default www-data user on Debian and Ubuntu can read them, because the files are world-readable and the directories are traversable. The permissions section below covers what to do if they are not.
Step 2: Write the site file
Create /etc/nginx/sites-available/example.com with this content:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}Create /etc/nginx/sites-available/example.org the same way. Change the server_name to example.org www.example.org and the root to /var/www/example.org/html.
The file name has no technical meaning to Nginx. Naming each file after its primary domain keeps the directory easy to scan. Files in sites-available are inactive. Nginx reads only what is linked into sites-enabled.
Step 3: Enable the site with a symlink
A symbolic link (symlink) is a small file that points to another file. Linking a file into sites-enabled makes the main configuration pick it up through an include line.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/example.org /etc/nginx/sites-enabled/Use absolute paths for the link target. A relative target is resolved from the directory that contains the link, not from your current directory, so it often produces a dangling link that points nowhere.
The reason this works is a line in /etc/nginx/nginx.conf, inside the http block:
include /etc/nginx/sites-enabled/*;Check that this line exists on your system. If you installed Nginx from the upstream nginx.org packages, or on a RHEL-family distribution, the layout is different. Those systems use conf.d/*.conf and have no sites-enabled. On them, put the file directly in /etc/nginx/conf.d/ and give it a .conf suffix.
To disable a site, remove the link and not the original file:
sudo rm /etc/nginx/sites-enabled/example.orgThe file stays in sites-available, so you can enable it again later.
Step 4: Validate with nginx -t
Run the configuration test before every reload.
sudo nginx -tA successful run prints two lines:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulThe test does more than check syntax. Nginx parses every included file, resolves paths, checks that referenced certificate files can be opened, and tries to open log files. It does not start serving, so it cannot tell you whether the site behaves correctly. A passing test means the configuration is loadable, not that it is right.
Use sudo nginx -T (capital T) to run the test and also print the full merged configuration, with each file's path shown above its contents. This is the quickest way to see what Nginx actually loaded when you suspect an include is missing.
Reading a failure
Errors name the file and line number. Two common ones:
nginx: [emerg] unknown directive "server_nam" in /etc/nginx/sites-enabled/example.com:4
nginx: configuration file /etc/nginx/nginx.conf test failednginx: [emerg] directive "root" is not terminated by ";" in /etc/nginx/sites-enabled/example.com:7Open the file at the reported line. If the actual mistake is on the line above, such as a missing semicolon, the error is often reported on the next line, where the parser noticed the problem.
Some messages are warnings, not errors, and the test still succeeds. A typical one is conflicting server name "example.com" on 0.0.0.0:80, ignored. It means two server blocks declare the same name on the same address and port, and Nginx ignores the second. The test passes, but the site you expected may not be the one that answers. Treat these warnings as errors.
Step 5: Reload, and combine it with the test
Chain the two commands with && so the reload runs only when the test succeeds:
sudo nginx -t && sudo systemctl reload nginxThe shell stops at the first command that returns a non-zero exit status. A failed test therefore never reaches the reload.
Reload versus restart
| Action | Command | What happens to connections |
|---|---|---|
| Reload | `systemctl reload nginx` | The master process re-reads the configuration and starts new workers. Old workers finish their current requests, then exit. No downtime. |
| Restart | `systemctl restart nginx` | All processes stop and start again. Connections in progress are dropped, and the port is briefly closed. |
Reload is the normal choice for configuration changes. If the new configuration is invalid, the master process keeps running the old workers and logs the error, so a bad reload does not take the site down. The nginx -t step still matters. Without it, a failed reload gives you no feedback at the terminal, and you only discover the problem when the change does not appear.
Some changes need a full restart. Examples are changes to the user directive or loading a new dynamic module. Changes to listen for new addresses usually work with a reload, but if a reload does not take effect, confirm it with ss -ltnp.
Step 6: Confirm that the site works
Check each site and verify the response:
curl -i --resolve example.com:80:127.0.0.1 http://example.com/
curl -i --resolve example.org:80:127.0.0.1 http://example.org/The --resolve option makes curl map the name to 127.0.0.1 for this one request, so no DNS change is needed. Each response should show HTTP/1.1 200 OK and the matching test heading. If both commands return the same page, the second site is not being selected. A later chapter covers how Nginx chooses a block. For now, check that both files are linked and that nginx -T lists both server_name lines.
File permissions and ownership
A passing nginx -t says nothing about whether workers can read the content. The usual cause of 403 Forbidden on a new site is permissions. The worker user needs two things:
- Execute (
x) permission on every directory in the path, from/down to the document root. - Read (
r) permission on the files.
Check which user the workers run as, then test the path as that user:
ps -o user= -C nginx | sort -u
sudo -u www-data stat /var/www/example.com/html/index.htmlOn systems where the workers run as nginx, replace www-data with nginx. If the stat call fails with "Permission denied", walk up the path with namei -l /var/www/example.com/html/index.html. It prints the mode and owner of each component. A home directory such as /home/alice/site is a frequent culprit, because home directories are often mode 750 or 700. Place site content under /var/www instead.
On systems with SELinux enforcing, such as RHEL and Fedora, correct Unix permissions are not enough. Content outside the default web locations needs the httpd_sys_content_t label.
Common mistakes
- Editing a file in
sites-availableand forgetting the symlink. The change has no effect because Nginx never reads that file. - Linking with a relative path, which creates a dangling link.
ls -l /etc/nginx/sites-enabled/shows a broken link in red, andnginx -treports that no such file exists. - Reloading without testing, and assuming it worked because the command printed nothing.
- Leaving the packaged
defaultsite enabled. It usesdefault_serveron port 80, so unmatched requests land on the Nginx welcome page. Remove the link withsudo rm /etc/nginx/sites-enabled/defaultonce you have your own catch-all. - Putting a site file in
sites-enabledas a copy instead of a link. It works, but then you have two files that can drift apart.
If a reload does not seem to apply
- Run
sudo systemctl status nginxand read the last log lines. - Run
sudo journalctl -u nginx -n 20for the error that a failed reload logged. - Run
sudo tail -n 20 /var/log/nginx/error.logfor configuration errors that occurred at reload time. - Confirm that the master process started after your edit with
ps -o lstart= -C nginx. Workers that predate your change mean the reload did not apply.
Exercise
You add a new site file to sites-available, create the symlink, and run sudo systemctl reload nginx without testing. The command prints nothing, but the new site is not served. What probably happened, and what should you have run first?
Show answer
The new configuration probably contains an error, so Nginx rejected it and kept running the old workers with the old configuration. Reload does not print the error to the terminal. Running sudo nginx -t first, or chaining it as sudo nginx -t && sudo systemctl reload nginx, would have shown the file name and line number of the problem. The error is also in journalctl -u nginx and in the error log.
Exercise
nginx -t reports success, but curl http://example.com/ returns 403 Forbidden for a site whose root is /home/alice/site. Name the likely cause and two commands that help find it.
Show answer
Nginx workers cannot traverse or read the path, most likely because /home/alice is not executable for the worker user. nginx -t does not check content permissions. Use namei -l /home/alice/site/index.html to see the mode of each directory, and sudo -u www-data stat /home/alice/site/index.html to reproduce the failure as the worker user. The fix is to move the content to /var/www or to correct the permissions.