nginx commands: test config, reload, restart, stop
Test your nginx config with nginx -t, then reload safely, and know the difference between reload, restart, stop and quit.
Always test before you reload. nginx -t checks the configuration file, and nginx -s reload applies it without dropping connections. If the new config is wrong, nginx keeps running with the old one.
Change the config safely
- Edit your nginx config (for example with our reverse proxy generator).
- Run sudo nginx -t to test it. Fix anything it reports.
- Run sudo nginx -s reload, or sudo systemctl reload nginx on systemd systems.
- Open the site and check it. Look at the error log if it fails.
Commands you can copy
sudo nginx -tsudo nginx -Tsudo nginx -s reloadsudo systemctl reload nginxsudo nginx -s stopsudo nginx -s quitsudo nginx -s reopensudo nginx -c /path/to/nginx.conf -tnginx -Vps -ax | grep nginxWhat each one does
- nginx -t
- Checks the syntax, then tries to open the files the configuration refers to. -T does the same and also prints the configuration. -q hides non-error messages while testing. A line such as test is successful means it is safe to reload.
- reload
- The master process checks the new configuration. If it is fine, it starts new workers and tells old workers to finish their current requests and exit, so visitors are not cut off. If the check fails, nginx rolls back and keeps working with the old configuration.
- stop vs quit
- stop is a fast shutdown. quit is graceful: workers finish current requests first. nginx’s own guide says to run these as the same user that started nginx.
- restart
- There is no restart signal in nginx’s own commands. systemctl restart nginx stops and starts the service, which drops open connections. Prefer reload for config changes.
- Config test passes but the site is down
- Look at the error log. A 502 Bad Gateway means the app behind nginx is not reachable, and a 403 usually means file permissions.
- Address already in use
- Another program or an old nginx holds the port. Find it with the port finder.
- Log rotation
- Rename the log file, then run nginx -s reopen so nginx starts writing to a new one.
Questions about nginx commands
- Should I use reload or restart?
- Reload for configuration changes. It is graceful and falls back to the old config if the new one is invalid. Restart only when reload is not enough.
- Does nginx -t change anything?
- No. It only checks the configuration.
- Where is the nginx config file?
- Run nginx -t. It prints the path of the main file it tested, usually /etc/nginx/nginx.conf on Linux.
- Why does nginx -s reload say it cannot find the pid file?
- nginx is not running, or you are not the user that started it. Start it first, or use sudo.
Config generators for this
- nginx reverse proxy configSend visitors from port 80 to an app running on port 3000.
- nginx PHP-FPM configRun a PHP site (WordPress, Laravel or plain PHP) on nginx with PHP-FPM.
- nginx HTTPS server blockServe your site over HTTPS with nginx: certificate paths, HTTP to HTTPS redirect, HSTS and a proxy or static site.
- nginx redirect www to non-wwwSend www.example.com to example.com (or the reverse) with one permanent redirect.
- nginx load balancer configSplit traffic across two or three copies of your app with an nginx upstream block.