Home Nginx Controlling Controlling NginxDefinition Controlling Nginx means starting it, stopping it, and applying new config without dropping every connection. The master process reads signals; worker processes handle requests.
Day-to-day commands: start the service, run nginx -t to test config, then reload. Prefer graceful stop (quit) over a hard kill.
Config Start with this shape. Adjust paths for your server.
sudo nginx -t
sudo nginx -s reload
# or
sudo systemctl reload nginx Always test with nginx -t first. If the test fails, Nginx keeps the old working config.
Key Directives Name Plain meaning nginx -tTest configuration syntax and report errors. nginx -s reloadAsk the master to apply a new config (HUP). nginx -s quitGraceful shutdown — finish current requests, then exit. nginx -s stopFast shutdown. nginx -s reopenRe-open log files after rotation (USR1).
Examples 1. Test Config, Then Reload 2. Start and Stop with systemd 3. Signals the Master Understands 4. What Happens If Config Is Invalid? Example 1 — Test Config, Then Reload Change a config file, validate it, then reload so workers pick up the new settings.
sudo nginx -t
sudo systemctl reload nginx
# equivalent signal form:
# sudo nginx -s reload How It Works nginx -t must say the syntax is ok.reload starts new workers with the new config.Old workers finish in-flight requests, then exit.Example 2 — Start and Stop with systemd On Ubuntu-style servers, systemd is the usual way to manage the service.
sudo systemctl start nginx
sudo systemctl status nginx
sudo systemctl stop nginx
# graceful stop via nginx:
sudo nginx -s quit How It Works start launches the master and workers.status shows whether the unit is active.quit is gentler than killing the process.Example 3 — Signals the Master Understands You can send Unix signals to the master PID (often in /run/nginx.pid).
# graceful reload
sudo kill -HUP $(cat /run/nginx.pid)
# reopen logs after rotate
sudo kill -USR1 $(cat /run/nginx.pid)
# graceful shutdown
sudo kill -QUIT $(cat /run/nginx.pid) How It Works HUP equals reload configuration.USR1 reopens log files after you rename them.QUIT is graceful shutdown; TERM/INT are fast stop.Example 4 — What Happens If Config Is Invalid? Reload refuses a broken file and keeps serving with the previous good config.
# after a typo in nginx.conf:
sudo nginx -t
# nginx: configuration file ... test failed
sudo nginx -s reload
# master rolls back; old workers keep running How It Works Test first so you see the line number of the error.Failed reload does not take the site down.Fix the typo , test again, then reload.Frequently Asked Questions What is the difference between reload and restart? Reload applies config with less downtime. Restart stops and starts the whole service.
Why does nginx -t matter? It catches missing semicolons and bad directives before you ask the master to reload.
Where is the PID file? Often /run/nginx.pid or /var/run/nginx.pid. The pid directive in nginx.conf can change this.
How do I rotate logs safely? Rename the log files, then send reopen (USR1) or nginx -s reopen so Nginx opens fresh files.
Can I upgrade the binary without downtime? Yes, advanced setups use USR2 for on-the-fly executable upgrade. Beginners can restart during a maintenance window.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion