How do I test and reload Nginx configuration safely?
Nginx can validate configuration before a reload. A successful syntax check is necessary, but it does not prove that every application route or upstream works. After reloading, verify the service and the exact website affected by the change.
Before you start
- An approved, narrowly scoped configuration change.
- The correct binary and configuration for the running Nginx instance.
- A known URL for the changed site.
Instructions
1. Test the selected instance
For the standard systemd installation, run the test with sufficient privileges to read referenced configuration and certificate files. Custom instances may require their existing -c and -p arguments; do not test a different default instance.
sudo nginx -tChecks syntax and whether referenced files can be opened. It does not reload workers.
2. Reload only after the test succeeds
The conditional operator prevents the reload command from running when the configuration test fails. A reload asks Nginx to apply configuration and transition workers gracefully.
sudo nginx -t && sudo systemctl reload nginxTests immediately before requesting the reload of the standard nginx unit.
3. Verify service and application
Check recent service messages and the actual hostname. For custom units, replace nginx with the verified unit name.
systemctl is-active nginx
sudo journalctl -u nginx --since "5 minutes ago" --no-pager -n 40
curl --head --connect-timeout 5 --max-time 15 https://app.example.com/Checks the unit, recent messages and a bounded HTTPS response.
Check the result
- The configuration test succeeds, the reload completes and the intended site still returns its expected response.
- Related sites sharing the same instance remain reachable.
Troubleshooting
- The test fails
- Correct the exact reported configuration error and repeat the test. Keep the existing running service in place; restarting it with invalid configuration can turn a contained change into an outage.
Official references
Use the documentation for your installed version when a flag, service name or platform feature differs.
