Nginx Cheatsheet - Nginx Configuration & Command Reference

Essential Nginx commands for config testing, location matching, reverse proxy, and log troubleshooting. Reference directly when editing configs or debugging 502 errors.

Web Services·19 commands·Last updated 2026-07-21
Back to Web Services

Commands & Testing 5

nginx -t
Test config syntax, always run before reload
nginx -s reload
Graceful reload without dropping connections
nginx -T
Print full effective config, debug include conflicts
systemctl restart nginx
Full restart (drops connections), prefer reload
nginx -V
Show compile flags and enabled modules

Location Matching Priority 5

location = /path
Exact match, highest priority
location ^~ /static/
Prefix match, skip regex check
location ~ \.php$
Case-sensitive regex match
location ~* \.(jpg|png)$
Case-insensitive regex match
location /path
Regular prefix match, lowest priority

Reverse Proxy Snippets 5

proxy_pass http://127.0.0.1:8080;
Forward to backend, trailing slash affects path
proxy_set_header Host $host;
Pass original Host header
proxy_set_header X-Real-IP $remote_addr;
Pass real client IP
proxy_read_timeout 60s;
Backend response timeout, increase for 502 on long requests
client_max_body_size 50m;
Max upload size, increase for 413 errors

Logs & Troubleshooting 4

tail -f /var/log/nginx/error.log
Follow error log, check here first for 502/504
tail -f /var/log/nginx/access.log
Follow access log
grep " 502 " /var/log/nginx/access.log
Filter 502 requests
curl -I http://localhost
View response headers only, quick service check

💡 Tips

  • Trailing slash in proxy_pass changes semantics: with / replaces location prefix, without it appends.
  • Always run nginx -t before reload — a syntax error won't break the running process but reload won't apply.
  • For 502: check error.log — "connection refused" means backend is down, "timeout" means backend is too slow.