Installing, testing and reloading

NGINX · Essentials · 8 min

Knowing the test-then-reload routine prevents taking a site down with a typo.

Install from your distribution (apt install nginx), from nginx.org's own package repositories for newer versions, or run the official nginx Docker image. Typical file locations on Linux:

  • /etc/nginx/nginx.conf: the main configuration file.
  • /etc/nginx/conf.d/*.conf (and on Debian/Ubuntu sites-enabled/): per-site files included from the main file.
  • /var/log/nginx/access.log and error.log: logs.
  • /usr/share/nginx/html or /var/www/html: default content.

The command-line tool controls everything:

  • nginx -t: test the configuration syntax and referenced files without touching the running server. Always run it before reloading.
  • nginx -T: test and print the full effective configuration, includes expanded. Great for debugging.
  • nginx -s reload (or systemctl reload nginx): graceful reload. The master process re-reads the configuration, starts new workers, and old workers finish their current requests before exiting. No connections are dropped.
  • nginx -s quit for graceful shutdown, nginx -V for the version and compiled-in modules.

If the new configuration is invalid, a reload keeps the old configuration running and logs the error, but a full restart would fail to start at all. That's one more reason to prefer reload and to test first.

Example

sudo apt install nginx
nginx -v                      # nginx version: nginx/1.30.6
sudo nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
sudo nginx -T | grep -n server_name      # see the effective config
sudo nginx -t && sudo systemctl reload nginx
curl -sI http://localhost/ | head -2
# HTTP/1.1 200 OK
# Server: nginx/1.30.6
tail -f /var/log/nginx/error.log

Remember

  • nginx -t tests, nginx -T tests and prints the merged config
  • reload is graceful: new workers start, old ones drain
  • Logs live in /var/log/nginx by default

Watch out for

  • Restarting instead of reloading: a broken config then leaves no server running.

Check yourself

  1. You reload NGINX with a configuration that has a syntax error. What happens?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: nginx.org/en/docs/beginners_guide.html#control