Controlling Nginx

Beginner
5 min read
Updated: Nov 2026
4 examples
Signals

Definition

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.

Nginx
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

NamePlain 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

Example 1 — Test Config, Then Reload

Change a config file, validate it, then reload so workers pick up the new settings.

Nginx
sudo nginx -t
sudo systemctl reload nginx
# equivalent signal form:
# sudo nginx -s reload

How It Works

  1. nginx -t must say the syntax is ok.
  2. reload starts new workers with the new config.
  3. 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.

Nginx
sudo systemctl start nginx
sudo systemctl status nginx
sudo systemctl stop nginx
# graceful stop via nginx:
sudo nginx -s quit

How It Works

  1. start launches the master and workers.
  2. status shows whether the unit is active.
  3. 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).

Nginx
# 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

  1. HUP equals reload configuration.
  2. USR1 reopens log files after you rename them.
  3. 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.

Nginx
# 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

  1. Test first so you see the line number of the error.
  2. Failed reload does not take the site down.
  3. Fix the typo, test again, then reload.

Frequently Asked Questions

Reload applies config with less downtime. Restart stops and starts the whole service.
It catches missing semicolons and bad directives before you ask the master to reload.
Often /run/nginx.pid or /var/run/nginx.pid. The pid directive in nginx.conf can change this.
Rename the log files, then send reopen (USR1) or nginx -s reopen so Nginx opens fresh files.
Yes, advanced setups use USR2 for on-the-fly executable upgrade. Beginners can restart during a maintenance window.

Next: Configuration file

Keep going through the beginner Nginx path.

Configuration file →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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