Sub topic 14
Reading Logs and Troubleshooting 502, 504 and Other Upstream Errors
Assume you know basic Nginx configuration, HTTP status codes, TCP ports, and how to run an application behind Nginx. This chapter uses an upstream group named app_backend.
Nginx is often the first component that reports an application failure. A client may see 502 Bad Gateway, 504 Gateway Timeout, or 503 Service Unavailable, while the real cause appears in the Nginx error log or in the application log.
Identify which layer failed
A reverse proxy request has several stages:
Use the status code as an initial clue:
| Status | Usual meaning | First place to investigate |
|---|---|---|
| 502 | Nginx could not get a valid response from the backend | Error log, backend process, port |
| 504 | The backend did not respond before a timeout | Backend latency, network, timeout settings |
| 503 | No usable upstream was available, or Nginx rejected the request | Upstream state, limits, application capacity |
A 502 commonly occurs when the application is stopped, the configured port is wrong, or the backend closes the connection unexpectedly. A 504 usually means that Nginx connected or attempted to connect, but the backend took too long to respond.
Record the upstream that handled each request
The default access log may show the client request but not which backend served it. Add an upstream-focused format in the http context:
log_format upstream_log '$remote_addr [$time_local] "$request" $status '
'upstream=$upstream_addr '
'upstream_status=$upstream_status '
'urt=$upstream_response_time rt=$request_time';
access_log /var/log/nginx/proxy_access.log upstream_log;$upstream_addr identifies the selected backend. $upstream_status records its response status. $upstream_response_time measures time spent waiting for the backend, while $request_time measures the complete client request.
Inspect both logs while reproducing the problem:
sudo tail -f /var/log/nginx/proxy_access.log /var/log/nginx/error.log
curl -i http://example.com/healthThe error log often contains a precise message such as connect() failed (111: Connection refused) or upstream timed out. A connection refusal usually means that no process is listening at the configured address and port. A timeout requires checking application work, network reachability, and proxy timeout values.
Use a controlled troubleshooting sequence
- Validate the configuration before changing runtime behavior.
sudo nginx -t && sudo systemctl reload nginx- Test the backend directly from the Nginx host. Replace the address and port with an actual backend.
curl -i --max-time 5 http://10.0.0.11:8080/health- Check that the process is running and listening.
sudo ss -ltnp | grep ':8080'
sudo systemctl status myapp- Compare the direct response with the response through Nginx. Differences often reveal incorrect headers, URI rewriting, or an application route that does not exist.
- Check application logs at the same timestamp as the Nginx request. Use a request identifier if your application supports one.
Separate timeout types
proxy_connect_timeout limits the time Nginx spends creating the upstream connection. proxy_send_timeout limits sending the request. proxy_read_timeout limits waiting between reads from the backend. Increasing every timeout can hide a slow or unhealthy application, so change only the limit that matches the observed failure.
Exercise
Exercise
Nginx returns 504, but a direct curl request to the backend completes in two seconds. What should you compare first?
Show answer
Compare the request path, headers, and timeout settings. Confirm that Nginx reaches the same backend and that proxyreadtimeout is longer than the backend's response time.