Diagnose 404s, 502s, and Slow Requests with Logs

Use access logs to identify who returned a 404, error logs to diagnose 502s, and request and upstream timings to investigate latency.

🎯 Introduction

When a browser displays 404, you know only that the requested content was not found. Did Nginx fail to find a file on disk, or does the backend lack that endpoint? The result looks the same, but the next troubleshooting step differs.

Start with the access log to find the request's result and whether Nginx forwarded it to a backend. A backend receiving forwarded requests is also called an upstream. If forwarding failed, look in the error log for the same time and path; it may explain the connection failure. For slow requests, start with access log timings.


🛠 Log Upstream Details and Timings

Open $HOME/nginx-course/conf/nginx.conf. Inside the existing http { ... } block, replace access_log logs/access.log; with the following. The download is already configured, so you can proceed directly to the experiments.

conf/nginx.conf
    log_format course '$remote_addr [$time_local] "$request" status=$status '
        'bytes=$body_bytes_sent rt=$request_time '
        'upstream=$upstream_addr us=$upstream_status urt=$upstream_response_time';
    access_log logs/access.log course;

log_format joins the three quoted strings into one line to define the course format. For example, $status becomes the status sent to the client, producing status=404. access_log uses that format to write logs/access.log. Apply the change with ngx -t && ngx -s reload. Nginx log module


🔎 For a 404, Check Whether an Upstream Was Used

First, follow new entries in both logs from one terminal.

tail -n 0 -f "$HOME/nginx-course/logs/access.log" "$HOME/nginx-course/logs/error.log"

With the download, run tail -n 0 -f logs/access.log logs/error.log inside the extracted directory, and use sh ./ngx for subsequent checks and reloads.

Open another terminal and request a missing static file and an endpoint not defined by the mock backend.

curl -i http://127.0.0.1:8080/assets/missing.css
curl -i http://127.0.0.1:8080/api/missing

Both access log entries have status=404, the result the client sees. Next, inspect the backend address in upstream and the status in us. Fields with no value appear as -.

"GET /assets/missing.css HTTP/1.1" status=404 … upstream=- us=- …
"GET /api/missing HTTP/1.1" status=404 … upstream=127.0.0.1:9001 us=404 …
  • /assets/missing.css has upstream=-, which only tells you that this entry has no upstream address. Check the site configuration next: Nginx looks up /assets/ files directly, and try_files ... =404 returns 404 when none is found. Investigate the static file path. This explicit 404 may appear only in the access log.
  • /api/missing has an upstream address on port 9001 or 9002. us=404 means the mock backend returned 404. The entry point and mock backend share an access log, so you will also see the backend's GET /missing entry. If the backend received an unexpected path, check the proxy path mapping. If the path is correct, check whether the backend provides that endpoint.

🔎 For a 502, Confirm the Cause in the Error Log

Simulate both backends being unreachable. In notes_api in conf/servers/upstream.conf, temporarily replace 9001 and 9002 with unused ports 9998 and 9999. Both connections will fail. Check and reload, then send a request.

curl -i http://127.0.0.1:8080/api/notes

Find GET /api/notes in the access log. It shows that the client received 502 and Nginx tried both backends. us=502, 502 records the outcomes Nginx assigned to the two failed connections.

"GET /api/notes HTTP/1.1" status=502 … upstream=127.0.0.1:9998, 127.0.0.1:9999 us=502, 502 …

Now find the error log entry for the same time and path.

connect() failed (…: Connection refused) while connecting to upstream, … request: "GET /api/notes HTTP/1.1", upstream: "http://127.0.0.1:9998/notes"

Connection refused means the connection was rejected. The target port is 9998, the unused port you configured. When this occurs outside the exercise, first check that the backend is running and that its actual listening address and port match the configuration.

Restore ports 9001 and 9002, check and reload, then request the same URL and confirm that it returns 200 again.


⏱ Compare rt and urt for Slow Requests

A request can return 200 and still be slow. Two access log timings help narrow the cause; both are in seconds.

  • rt is the overall request time as seen by Nginx, including receiving and processing the request, communicating with the upstream, and sending the response.
  • urt is the response time for one upstream attempt. Retried requests may have multiple urt values, corresponding in order to the addresses in upstream. Nginx upstream variables
Log with a single upstream attemptWhat to investigate next
rt=2.104 urt=2.081Upstream time is close to total time. Check backend processing time and the network connection between Nginx and the backend first.
rt=2.104 urt=0.006The upstream took only about 6 milliseconds. The remaining time may involve receiving the request, Nginx processing, or sending the response. These values only narrow the possibilities.

🧪 Exercise: Observe Logs During a Slower Download

Compare two requests using large.txt from the compression exercise. Run this command first and record rt in the access log.

curl -sS -o /dev/null -w 'HTTP %{http_code}\n' http://127.0.0.1:8080/assets/large.txt

Temporarily add limit_rate 2k; inside location ^~ /assets/ { ... } in conf/servers/site.conf, making Nginx send roughly 2 KB per second. Check and reload, then repeat the same command. Nginx limit_rate documentation

In one observed run, both requests returned 200, while rate limiting increased rt to about 3 seconds. Both had urt=- because the entry point served the static file directly.

status=200 bytes=7501 rt=0.000 upstream=- us=- urt=-
status=200 bytes=7501 rt=3.015 upstream=- us=- urt=-

Remove limit_rate 2k; after the exercise, then check and reload.