Nginx cheatsheet
Service control
| Command | Description |
|---|---|
systemctl status nginx | Service status |
systemctl reload nginx | Reload config (graceful) |
systemctl restart nginx | Full restart |
nginx -t | Test config syntax |
nginx -T | Test and dump full effective config |
Sites (Debian/Ubuntu)
| Command | Description |
|---|---|
ln -s .../sites-available/mysite /etc/nginx/sites-enabled/ | Enable a site |
rm /etc/nginx/sites-enabled/default | Disable default site |
ls /etc/nginx/sites-enabled/ | List enabled sites |
Logs and debugging
| Command | Description |
|---|---|
tail -f /var/log/nginx/error.log | Follow error log |
tail -f /var/log/nginx/access.log | Follow access log |
journalctl -u nginx -f | systemd journal |
ss -tlnp | grep nginx | Check listeners |
curl -vI -H "Host: www.example.com" http://127.0.0.1/ | Test vhost locally |
Static site server block
server {
listen 80;
server_name www.example.com;
root /var/www/example;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Reverse proxy server block
upstream app_backend {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://app_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Pro tips
- Always run
nginx -tbeforereload— syntax errors can prevent startup - Use
curl -H "Host: ..."to test server blocks without DNS - Trailing slash on
proxy_passchanges URI rewriting — check the docs nginx -Tshows the merged config after allincludedirectives
Practice scenarios
Hands-on Nginx scenarios on live Linux VMs: nginx