Linux Nginx Error Codes — 502, 504, 413 & Upstream Failures
Errorweb server
Overview
Fix common Nginx errors including 502 Bad Gateway, 504 Gateway Timeout, 413 Request Entity Too Large, and upstream connection failures.
Key Details
- Nginx errors are logged to /var/log/nginx/error.log by default
- 502 Bad Gateway: upstream (backend) returned invalid response or is not running
- 504 Gateway Timeout: upstream did not respond within proxy_read_timeout
- 413 Request Entity Too Large: request body exceeds client_max_body_size (default 1MB)
- Connection refused: upstream is not listening on the configured address/port
Common Causes
- 502: backend application crashed, not running, or returning malformed response
- 504: backend processing time exceeds Nginx timeout settings
- 413: file upload or POST body exceeds client_max_body_size
- Connection refused: backend not started or wrong port in upstream config
- Permission denied: Nginx worker cannot connect to upstream socket
Steps
- 1Check Nginx error log: tail -f /var/log/nginx/error.log
- 2For 502: verify backend is running — curl -v http://127.0.0.1:BACKEND_PORT
- 3For 504: increase timeout — proxy_read_timeout 300s; proxy_connect_timeout 300s;
- 4For 413: increase limit — client_max_body_size 50M; in server or location block
- 5Test config: nginx -t, then reload: systemctl reload nginx
Tags
linuxnginx502504web-server
More in Web Server
Frequently Asked Questions
In nginx.conf inside http, server, or location block. Server block applies to that virtual host, location block to that specific path.