Troubleshooting NGINX: 502 Bad Gateway, 504, 404, 403 and Config Errors
Diagnose common NGINX errors fast: 502 Bad Gateway, 504 Gateway Timeout, 403 Forbidden, 404, 413 and nginx -t failures, using logs and a systematic checklist...
Published November 27, 2026
NGINX errors are usually not NGINX's fault. A 502 means the backend did not answer properly, a 403 is usually file permissions, and a 404 is often a root or location mistake. The key is to read the error log first, because NGINX almost always writes down exactly what went wrong. This guide gives you a checklist for each common error.
The concept
NGINX keeps two logs per site by default. The access log records every request and its status code. The error log records why something failed: upstream connection refused, permission denied, file not found, timeouts. On Debian and Ubuntu they live in /var/log/nginx/. When troubleshooting, run tail -f on the error log while reproducing the problem.
Errors in the 5xx range that NGINX generates itself (502, 504) describe the relationship between NGINX and your backend. Errors in the 4xx range (403, 404, 413) are usually about how NGINX maps the request to files or limits. Mapping each status to its usual causes turns guesswork into a quick checklist.
Status codes and their usual causes
| Status | Meaning | Most common causes |
|---|---|---|
| 502 Bad Gateway | Backend gave no valid response | App down, wrong port/socket, socket permissions, app crashed mid-request |
| 504 Gateway Timeout | Backend too slow | Slow queries, proxy_read_timeout too low, deadlocked app |
| 403 Forbidden | NGINX refused to serve | File/directory permissions, no index file with autoindex off, deny rules |
| 404 Not Found | No file at the mapped path | Wrong root vs alias, missing try_files fallback for SPA routes |
| 413 Request Entity Too Large | Body over limit | client_max_body_size (default 1m) |
| 499 | Client closed connection | Client timeout or user navigated away; often a slow backend |
The debugging order
- nginx -t: is the configuration valid at all?
- tail the error log while reproducing the request
- Check the backend directly with curl on its port or socket
- Check file and socket permissions for the worker user
- Check SELinux/AppArmor denials (audit log) if permissions look right
A 502 solved in four commands
403 and 404: the root vs alias trap
root appends the full request URI to the path; alias replaces the matched location prefix. Mixing them up is the single most common cause of mysterious 404s for static folders.
# Request: /static/app.css
location /static/ { root /srv/site; } # → /srv/site/static/app.css
location /static/ { alias /srv/site/assets/; } # → /srv/site/assets/app.css
# 403: the worker user must be able to traverse every parent directory
namei -l /srv/site/assets/app.css
sudo -u www-data cat /srv/site/assets/app.cssPractical examples
Validate config and find the broken line
nginx -t names the file and line number.
$ sudo nginx -t
nginx: [emerg] unexpected "}" in /etc/nginx/sites-enabled/shop:23
nginx: configuration file /etc/nginx/nginx.conf test failedFix single-page app 404s on refresh
Send unknown paths to index.html so the client router handles them.
location / {
try_files $uri $uri/ /index.html;
}Raise timeouts and upload size for one location
Scope generous limits to where they are needed.
location /api/reports/ {
proxy_read_timeout 300s;
proxy_pass http://app;
}
location /api/upload/ {
client_max_body_size 100m;
proxy_pass http://app;
}Common mistakes
- Raising timeouts to hide a slow query instead of fixing it.
- chmod 777 to fix a 403, opening files to every user on the system.
- Looking only at the access log; the error log explains the cause.
- Forgetting SELinux on RHEL: httpd_can_network_connect must be on for NGINX to proxy to a TCP port.
- Reloading with a broken config. nginx -t first; a failed reload keeps the old config but leaves you confused.
Key takeaways
- Read the error log first; it nearly always names the cause.
- 502 and 504 are backend problems; 403 and 404 are usually paths and permissions.
- Test the backend directly with curl to split NGINX problems from app problems.
- Know root vs alias and try_files to avoid most 404s.
FAQ
Why do I get 502 only sometimes?
Usually the backend restarting during deploys, worker exhaustion in PHP-FPM (pm.max_children reached), or upstream keepalive connections closed by the app before NGINX reuses them.
How do I log upstream response times?
Add $upstream_response_time and $request_time to a custom log_format. Comparing them shows whether slowness is in the app or the network to the client.
How do I hide the NGINX version?
Set server_tokens off; in the http context. It removes the version from error pages and the Server header.