Sub topic 6
Enabling a Site: Validate with `nginx -t`, Then Reload
This chapter assumes that Nginx is installed and running under systemd. You should also be comfortable with sudo, file permissions, symbolic links, and basic HTTP configuration. The examples use the sites-available and sites-enabled layout commonly provided by Debian and Ubuntu packages.
Create the Site Configuration
A site configuration normally lives in /etc/nginx/sites-available/. Keeping the file there does not activate it. Activation happens when you create a symbolic link, or symlink, in /etc/nginx/sites-enabled/.
Create a server block for example.com:
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;
}
}Save this as /etc/nginx/sites-available/example.com. The root directory and its files must already exist and be readable by the Nginx worker process.
Enable the configuration by linking it into the enabled directory:
sudo ln -s /etc/nginx/sites-available/example.com \
/etc/nginx/sites-enabled/example.comNginx reads files from sites-enabled because the distribution's main configuration usually contains an include directive such as:
include /etc/nginx/sites-enabled/*;Check your /etc/nginx/nginx.conf if your installation uses a different layout.
Validate Before Reloading
Always test the complete Nginx configuration before asking the running service to reload it:
sudo nginx -tA successful test reports that the syntax is okay and that the configuration test is successful. The test checks more than the new file. It parses the complete configuration tree, including included files, referenced certificates, and many file paths.
Use && to reload only when validation succeeds:
sudo nginx -t && sudo systemctl reload nginxA reload tells the running master process to read the new configuration and start workers with it. Existing workers can finish current requests while new requests use the new workers. This normally avoids stopping the service or dropping active connections.
The Safe Enablement Workflow
After a successful reload, verify the service and inspect recent logs if needed:
sudo systemctl is-active nginx
sudo journalctl -u nginx --since "5 minutes ago"If the site is not ready for public DNS, test it locally with a Host header:
curl -H 'Host: example.com' http://127.0.0.1/This sends the request to the local Nginx listener while selecting the server block for example.com.
Common Enablement Errors
nginx -treports a missing file: check paths for certificates, included files, and document roots.lnreports that the file exists: inspect the existing link before changing it. It may already enable the intended site.- Nginx serves an old page: confirm that the symlink points to the expected file and reload succeeded.
- A reload fails: run
sudo nginx -tseparately and fix the first reported error.
Exercise
What commands enable /etc/nginx/sites-available/example.org and reload Nginx only if the complete configuration is valid?
Show answer
Run sudo ln -s /etc/nginx/sites-available/example.org /etc/nginx/sites-enabled/example.org, then run sudo nginx -t && sudo systemctl reload nginx.