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
| Command | What it does |
|---|---|
sudo systemctl start nginx | Start the service |
sudo systemctl stop nginx | Stop the service |
sudo systemctl restart nginx | Full stop + start (drops connections) |
sudo systemctl reload nginx | Graceful reload — zero downtime config swap |
sudo systemctl enable nginx | Enable auto-start on boot |
sudo systemctl status nginx | Show running state and recent logs |
sudo nginx -s reload | Reload without systemctl (sends HUP signal) |
sudo nginx -s stop | Fast shutdown |
sudo nginx -s quit | Graceful 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 -tbefore reloading. A bad config aborts the reload — the running process keeps serving.
Key File Paths
| Path | Purpose |
|---|---|
/etc/nginx/nginx.conf | Main config (Debian/Ubuntu/RHEL) |
/usr/local/etc/nginx/nginx.conf | Main 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.log | Default access log |
/var/log/nginx/error.log | Default error log |
/var/run/nginx.pid | PID 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)