Useful Nginx Commands
A concise guide to the most useful Nginx commands you should know.
1. Testing Configuration
Before making any changes live, always test your Nginx configuration:
sudo nginx -t
- Checks for syntax errors.
- Ensures configuration files are valid.
- Example output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
2. Starting, Stopping, Restarting Nginx
Use systemctl:
# Start Nginx
sudo systemctl start nginx
# Stop Nginx
sudo systemctl stop nginx
# Restart Nginx (stops then starts)
sudo systemctl restart nginx
# Reload Nginx (applies config changes without dropping connections)
sudo systemctl reload nginx
Tip: Use reload instead of restart when you’ve just changed the configuration—it’s safer for live servers.
3. Checking Nginx Status
sudo systemctl status nginx
- Shows if Nginx is active, loaded, or failed.
- Useful for debugging if the server won’t start.
4. Enabling/Disabling Nginx on Boot
# Enable Nginx to start automatically on boot
sudo systemctl enable nginx
# Disable automatic start on boot
sudo systemctl disable nginx
5. Viewing Active Nginx Processes
ps aux | grep nginx
- Lists all running Nginx processes.
- Shows the master process and multiple worker processes.
6. Managing Nginx Logs
Default log locations:
- Access log:
/var/log/nginx/access.log - Error log:
/var/log/nginx/error.log
Useful commands:
# Tail error log live
sudo tail -f /var/log/nginx/error.log
# Tail access log live
sudo tail -f /var/log/nginx/access.log
7. Reloading Configuration Without Restart
sudo nginx -s reload
- Reloads Nginx gracefully.
- Handy if you don’t want to use
systemctl.
8. Checking Nginx Version
nginx -v
- Shows installed Nginx version.
nginx -V
- Shows version plus compilation options (useful for debugging modules).
9. Testing Server Response
curl -I http://localhost
-Ifetches only HTTP headers.- Checks if Nginx is serving requests correctly.