Master Nginx: Essential Commands for Starting, Stopping, Reloading and Debugging
This guide provides a comprehensive cheat‑sheet of Nginx command‑line operations—including how to start, stop, reload, check configuration, view logs, inspect processes, query version information and perform common debugging tasks—helping administrators manage Nginx efficiently in production and development environments.
Introduction
Nginx is a core component of many large‑scale architectures. The following reference lists the most frequently used Nginx commands for service control, configuration validation, log inspection, process monitoring, version checking, and troubleshooting.
1. Service Control (Start, Stop, Reload)
Start Nginx with the default configuration: nginx Start Nginx with a specific configuration file: nginx -c /etc/nginx/nginx.conf Immediately stop Nginx (not recommended for production): nginx -s stop Gracefully stop Nginx, waiting for active requests to finish (production‑recommended): nginx -s quit Reload configuration without downtime: nginx -s reload Reopen log files after rotation (useful with logrotate):
nginx -s reopen2. Configuration Validation
Test configuration syntax before applying changes: nginx -t Test a specific configuration file:
nginx -t -c /etc/nginx/nginx.conf3. Process and Status Management
Show all Nginx processes: ps -ef | grep nginx Show listening ports (choose one): netstat -lntp | grep nginx or ss -lntp | grep nginx Show master and worker processes: ps aux | grep 'nginx:' Display the PID stored in the default PID file:
cat /var/run/nginx.pid4. Log‑Related Commands (Core Troubleshooting)
Tail the access log in real time: tail -f /var/log/nginx/access.log Tail the error log in real time: tail -f /var/log/nginx/error.log Show the top IP addresses (quick traffic stats):
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | headCount HTTP status codes returned by the server:
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c5. Version and Compilation Parameters
Show the short version string: nginx -v Show the full version with compile options: nginx -V Check whether a specific module (e.g., SSL) was compiled in:
nginx -V 2>&1 | grep --with-http_ssl_module6. Configuration and Path Location
Find the path of the loaded nginx.conf file: nginx -V 2>&1 | grep conf Show default installation locations (binary, man pages, etc.): whereis nginx Show the absolute path of the Nginx binary:
which nginx7. Performance and Connection Inspection
Display a summary of socket statistics (including total connections): ss -s Count connections in TIME_WAIT state: ss -ant | grep TIME_WAIT | wc -l Count connections on a specific port (e.g., 80):
ss -ant | grep :80 | wc -l8. Debugging and Issue Localization
Run Nginx in the foreground (useful for debugging): nginx -g "daemon off;" Test configuration and reload in a single step (common in containers): nginx -t && nginx -s reload Force‑kill a stuck Nginx process as a last resort: kill -9 <pid> One‑click restart (use with caution):
nginx -s stop && nginxMike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
