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.
Back to Web ServicesCommands & Testing 5
nginx -tTest config syntax, always run before reload
nginx -s reloadGraceful reload without dropping connections
nginx -TPrint full effective config, debug include conflicts
systemctl restart nginxFull restart (drops connections), prefer reload
nginx -VShow compile flags and enabled modules
Location Matching Priority 5
location = /pathExact match, highest priority
location ^~ /static/Prefix match, skip regex check
location ~ \.php$Case-sensitive regex match
location ~* \.(jpg|png)$Case-insensitive regex match
location /pathRegular 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.logFollow error log, check here first for 502/504
tail -f /var/log/nginx/access.logFollow access log
grep " 502 " /var/log/nginx/access.logFilter 502 requests
curl -I http://localhostView 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.