Sub topic 5
Testing and Reloading Nginx Configuration Safely
This chapter assumes basic Linux command-line use, sudo, HTTP requests, DNS names, and TCP ports. Nginx configuration changes should follow a repeatable cycle: edit, validate, reload, then verify the result.
The safe change cycle
Nginx reads its main configuration from /etc/nginx/nginx.conf. That file often includes smaller files from directories such as /etc/nginx/conf.d/ or /etc/nginx/sites-enabled/. An included file is still part of the complete configuration, so a typo in any included file can prevent a reload.
Use this sequence:
- Edit one focused change.
- Test the complete configuration.
- Reload Nginx if the test succeeds.
- Send a request to the affected URL.
- Check logs if the result is unexpected.
Validate before reloading
The nginx -t command checks configuration syntax and tries to open referenced files. Run it with the same privileges Nginx uses to read its configuration and certificates:
sudo nginx -tA successful test normally ends with messages similar to syntax is ok and test is successful. A failure includes a file name and line number. Fix that location, then run the test again.
Use nginx -T when you need to inspect the fully expanded configuration, including files loaded through include directives:
sudo nginx -T | less| Check | What it tells you |
|---|---|
| `nginx -t` | Whether Nginx can load the configuration |
| `nginx -T` | Which directives Nginx sees after includes are expanded |
| `curl -I http://localhost` | Whether a local HTTP request receives a response |
Reload instead of restarting
A reload asks the running master process to read the new configuration and start workers with it. Existing workers finish current requests before exiting. This usually avoids an interruption.
sudo nginx -t && sudo systemctl reload nginx
systemctl is-active nginx
curl -I http://localhostThe && operator runs the reload only when nginx -t succeeds. A restart stops and starts the service, which can interrupt connections and is usually unnecessary for configuration changes. Use systemctl restart nginx only when a reload cannot apply the required change or when troubleshooting a stuck process.
Diagnose and roll back
If a reload fails, the old worker processes normally continue serving traffic. Read the error details with:
sudo journalctl -u nginx --since "10 minutes ago"
sudo tail -n 50 /var/log/nginx/error.log
sudo tail -n 50 /var/log/nginx/access.logThe error log records configuration and runtime problems. The access log records requests, response status codes, and often response sizes. A 404 usually indicates a missing file or unmatched application route. A 502 commonly means that Nginx could not connect to an upstream application.
Before a risky edit, make a dated copy of the file you will change. If verification fails after a successful reload, restore that copy, run nginx -t, and reload again. Keep changes small so the faulty edit is easy to identify.
Exercise
What command prevents a reload when the Nginx configuration contains a syntax error?
Show answer
sudo nginx -t && sudo systemctl reload nginx tests first and reloads only if the test succeeds.