Sub topic 12
Troubleshooting Common Problems
Assume you can use the Linux command line, edit files with sudo, read basic HTTP responses, and understand DNS names and TCP ports. Troubleshooting Nginx is usually a process of finding which layer failed: the service, configuration, network, request matching, upstream application, or file permissions.
Start with service and configuration checks
First check whether Nginx is running:
systemctl status nginxIf it is stopped or failed, read the recent service messages:
sudo journalctl -u nginx --since "30 minutes ago"A configuration error often prevents Nginx from starting or reloading. Test the complete configuration before changing the service:
sudo nginx -tThe command reports the file and line containing a syntax error. Common causes include a missing semicolon, an unmatched brace, or a directive placed in the wrong context. A context is the section where a directive is allowed, such as http, server, or location.
After correcting the problem, reload Nginx:
sudo systemctl reload nginxA reload keeps existing connections alive while workers begin using the new configuration. If the test fails, do not reload.
Separate network problems from Nginx problems
Test the server locally first:
curl -I http://127.0.0.1
sudo ss -ltnp | grep nginxIf curl cannot connect and Nginx is running, check that it listens on the expected address and port. A firewall may also block port 80 or 443:
sudo ufw statusIf local requests work but requests from another machine fail, investigate the firewall, cloud security group, load balancer, or DNS record. Check DNS with:
dig +short example.comThe result should contain the server's public address.
Read logs and identify the failing layer
The access log records requests that reached Nginx. The error log records startup failures, permission errors, upstream failures, and other warnings.
sudo tail -f /var/log/nginx/access.log /var/log/nginx/error.logUse the HTTP status code as an initial clue:
| Symptom | Likely cause |
|---|---|
| 404 Not Found | Wrong `root`, URI, or `try_files` rule |
| 403 Forbidden | File permissions, directory permissions, or access rules |
| 502 Bad Gateway | Upstream application is stopped or unreachable |
| 504 Gateway Timeout | Upstream is too slow or network access is blocked |
| Redirect loop | Conflicting HTTP, HTTPS, or proxy scheme settings |
For a 502, verify the application directly:
curl -i http://127.0.0.1:3000
sudo ss -ltnp | grep ':3000'If the application listens on another address or port, update proxy_pass. If it works directly but fails through Nginx, inspect the matching location block and its proxy headers.
Check location and file permissions
Nginx chooses a server block using the listening address, port, and Host header. It then selects the best matching location. A request may therefore reach a different block than expected.
For static files, confirm that the file exists and that the Nginx worker can traverse every parent directory:
namei -l /var/www/example.com/html/index.htmlAvoid solving permission errors by making the entire site world-writable. Give the service only the read access it needs.
Exercise
Nginx returns 502 for /api/, but curl http://127.0.0.1:3000 fails too. What should you investigate first?
Show answer
Investigate the application service, its listening port, and its logs before changing Nginx. The upstream is unavailable independently of the proxy.