Nginx Cheatsheet

Basics for Projects

Use this Nginx reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Install

# Debian/Ubuntu
sudo apt update && sudo apt install nginx

# RHEL/CentOS/Fedora
sudo dnf install nginx

# macOS (Homebrew)
brew install nginx

# Check installed version
nginx -v
nginx -V   # includes compiled-in modules and configure flags

Service Control

CommandWhat it does
sudo systemctl start nginxStart the service
sudo systemctl stop nginxStop the service
sudo systemctl restart nginxFull stop + start (drops connections)
sudo systemctl reload nginxGraceful reload — zero downtime config swap
sudo systemctl enable nginxEnable auto-start on boot
sudo systemctl status nginxShow running state and recent logs
sudo nginx -s reloadReload without systemctl (sends HUP signal)
sudo nginx -s stopFast shutdown
sudo nginx -s quitGraceful shutdown (finish in-flight requests)

Config Testing

sudo nginx -t           # Test config, print errors
sudo nginx -T           # Test + dump full merged config to stdout
sudo nginx -t -c /path/to/nginx.conf   # Test a non-default config file

Always run nginx -t before reloading. A bad config aborts the reload — the running process keeps serving.

Key File Paths

PathPurpose
/etc/nginx/nginx.confMain config (Debian/Ubuntu/RHEL)
/usr/local/etc/nginx/nginx.confMain config (Homebrew macOS)
/etc/nginx/sites-available/Vhost definitions (Debian convention)
/etc/nginx/sites-enabled/Symlinks to active vhosts
/etc/nginx/conf.d/Drop-in config snippets (RHEL convention)
/var/log/nginx/access.logDefault access log
/var/log/nginx/error.logDefault error log
/var/run/nginx.pidPID file for the master process
/usr/share/nginx/html/Default document root

Process Model

Master process   — reads config, binds privileged ports, spawns workers
  └── Worker 1   — handles connections (non-privileged)
  └── Worker 2
  └── ...
  • worker_processes auto; — one worker per CPU core (recommended)
  • Each worker can handle thousands of concurrent connections via async I/O
  • Master never handles client traffic — only workers do

Enable a Site (Debian/Ubuntu)

# Create symlink from sites-available to sites-enabled
sudo ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/

# Disable a site
sudo rm /etc/nginx/sites-enabled/mysite.conf

sudo nginx -t && sudo systemctl reload nginx

Useful One-liners

# Watch error log live
sudo tail -f /var/log/nginx/error.log

# Watch access log live
sudo tail -f /var/log/nginx/access.log

# Count active connections by state
ss -s

# Show which process owns port 80
sudo ss -tlnp | grep ':80'

# Send USR1 to rotate logs without downtime
sudo kill -USR1 $(cat /var/run/nginx.pid)