keepcalculator

06 Numbers and Powers

Useful nginx command examples

Card No. 06·87674 · Updated · Standard formula, shown below

Useful nginx commands cover starting, stopping, reloading, testing configuration, and viewing logs. This article lists the most common commands with examples, explains what each does, and shows how to read their output.

Basic service commands

On most Linux distributions, nginx runs as a service. The exact command depends on your init system. For systemd (used by Ubuntu 16.04+, Debian 8+, CentOS 7+, and most modern distros):

  • sudo systemctl start nginx – start nginx
  • sudo systemctl stop nginx – stop nginx
  • sudo systemctl restart nginx – stop then start nginx
  • sudo systemctl reload nginx – reload configuration without dropping connections
  • sudo systemctl status nginx – show whether nginx is running and recent log lines
  • sudo systemctl enable nginx – make nginx start at boot

On older systems using SysV init, use sudo service nginx start, stop, restart, reload, or status.

Direct nginx binary commands

You can also call the nginx binary directly. The binary is usually at /usr/sbin/nginx or /usr/local/nginx/sbin/nginx. Run nginx -h to see all options.

  • sudo nginx – start nginx (uses the default configuration file)
  • sudo nginx -s stop – stop nginx immediately
  • sudo nginx -s quit – stop nginx gracefully (finishes current requests)
  • sudo nginx -s reload – reload configuration (same as systemctl reload)
  • sudo nginx -s reopen – reopen log files (useful after log rotation)

Testing configuration

Before reloading or restarting, always test the configuration. A syntax error will prevent nginx from starting, and a bad reload can take down your site.

  • sudo nginx -t – test configuration and print the result
  • sudo nginx -T – test configuration and dump the full expanded configuration to stdout
  • sudo nginx -t -c /path/to/nginx.conf – test a specific configuration file

If the configuration is valid, you’ll see:

nginx: configuration file /etc/nginx/nginx.conf test is successful

If there are errors, nginx prints the file name, line number, and a message like unknown directive "server_name" or host not found in upstream.

Viewing and following logs

nginx writes two main logs: access log and error log. Their locations depend on your configuration, but defaults are often /var/log/nginx/access.log and /var/log/nginx/error.log.

  • tail -f /var/log/nginx/access.log – watch incoming requests in real time
  • tail -f /var/log/nginx/error.log – watch errors in real time (great for debugging)
  • grep " 500 " /var/log/nginx/access.log – find responses with status 500
  • sudo journalctl -u nginx – view nginx service logs (systemd)

Checking which nginx is running and its version

If you have multiple nginx installations, it helps to know which binary is running and what version it is.

  • nginx -v – print version
  • nginx -V – print version, compiler flags, and configure arguments (shows which modules are compiled in)
  • ps aux | grep nginx – show running nginx processes and their command lines
  • sudo ss -tlnp | grep nginx – show which ports nginx is listening on

Reload vs restart: what’s the difference?

Reload is almost always better than restart. When you reload, nginx re-reads the configuration and applies changes without dropping existing connections. The master process sends a signal to worker processes, which finish current requests and then pick up the new configuration. Restart stops all processes and starts fresh, which means a brief downtime. Use reload for config changes, and restart only when you need to apply a new binary or fix a stuck process.

Common problems and fixes

  • Permission denied when binding to port 80 – you must run nginx as root (or use sudo) to bind to ports below 1024.
  • Port already in use – run sudo ss -tlnp | grep :80 to see which process is using port 80, then stop it or change nginx’s listen directive.
  • Configuration test fails – read the error message carefully; it usually points to the exact line. Common causes: missing semicolon, missing closing brace, or a directive that requires a module not compiled in.
  • Worker process fails to start – check the error log. Often it’s a file permission issue on the log files or the user directive points to a nonexistent user.
  • Reload doesn’t pick up changes – make sure you edited the correct file. Use nginx -T to see the full expanded configuration and verify your change is there.

Frequently Asked Questions

How do I check if nginx is running?

Use sudo systemctl status nginx (systemd) or ps aux | grep nginx. If nginx is running, you’ll see a master process and several worker processes. You can also try curl -I http://localhost – if you get a response, nginx is serving.

What’s the difference between nginx -s stop and nginx -s quit?

stop sends a SIGTERM and terminates nginx immediately, potentially dropping in-flight requests. quit sends a SIGQUIT, which lets nginx finish processing current requests before shutting down. Prefer quit for a graceful shutdown.

Why do I need to test configuration before reloading?

A syntax error in the configuration can prevent nginx from starting. If you reload with a broken config, nginx will keep running with the old configuration, but the error may not be obvious. Testing with nginx -t catches errors before you apply them. It’s a safety step that takes one second.

How do I reload nginx without restarting?

Use sudo systemctl reload nginx or sudo nginx -s reload. This is the recommended way to apply configuration changes because it doesn’t drop connections.

Where are nginx logs located?

By default, logs are in /var/log/nginx/ (access.log and error.log). You can change the location with the access_log and error_log directives in your nginx.conf. To find the actual path, run nginx -T | grep log.

How do I see which modules are compiled into nginx?

Run nginx -V. The output includes --with-http_ssl_module, --with-stream, and other configure arguments. This tells you which features are available. If a directive like stream is not recognized, the module is likely not compiled in.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →