Sub topic 11
Reading Access and Error Logs
Assume you can use the Linux shell, run commands with sudo, edit Nginx configuration, and distinguish an HTTP request from a systemd service event. This chapter focuses on using logs to explain what happened when a request failed.
Two logs, two questions
Nginx usually writes these files on Debian, Ubuntu, and many other Linux systems:
/var/log/nginx/access.logrecords completed client requests./var/log/nginx/error.logrecords problems and diagnostic messages.
The access log answers, “What request did Nginx receive, and what response did it send?” The error log answers, “What prevented Nginx from handling the request normally?”
A default access-log line often looks like this:
203.0.113.17 - - [28/Nov/2026:04:20:15 +0000] "GET /images/logo.png HTTP/1.1" 404 153 "-" "curl/8.5.0"| Field | Meaning |
|---|---|
| `203.0.113.17` | Client address seen by Nginx |
| `GET /images/logo.png` | HTTP method and requested path |
| `404` | Response status |
| `153` | Response body size in bytes |
| `curl/8.5.0` | User-agent string |
The exact fields depend on the log_format directive. A reverse proxy may use a custom format that also records upstream status and request time.
Watch requests while testing
Use tail to view the newest lines. The -F option continues following the file even if log rotation replaces it.
sudo tail -F /var/log/nginx/access.log /var/log/nginx/error.logIn another terminal, make a request:
curl -i http://localhost/
curl -i http://localhost/missing-fileLook for the status code:
2xxusually means success.3xxmeans a redirect.4xxusually indicates a client or routing problem, such as a missing file or denied request.5xxusually indicates a server, upstream, or configuration problem.
The access log alone does not prove why a 502 Bad Gateway occurred. Check the error log at the same time. Nginx may report that it could not connect to an application, received an invalid response, or timed out.
Configure useful log destinations
Logging is configured inside the http, server, or location context. More-specific contexts can override broader settings.
http {
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log warn;
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log notice;
}
}Error levels become more detailed as you move from emerg through alert, crit, error, warn, notice, info, and debug. A lower threshold, such as info, records more messages and can increase disk usage. Use debug only temporarily and only when needed.
After changing log settings, test and reload the configuration:
sudo nginx -t && sudo systemctl reload nginxA repeatable troubleshooting path
Start with the access-log status and timestamp. Then inspect the matching error-log lines:
sudo grep '28/Nov/2026:04:20' /var/log/nginx/access.log
sudo grep -iE 'error|failed|refused|timeout' /var/log/nginx/error.logFor service startup failures, use systemd's journal as well:
sudo journalctl -u nginx --since "15 minutes ago"Access and error logs can contain user-controlled URLs, referrers, and user-agent strings. Treat displayed values as untrusted text, and avoid copying secrets or tokens into tickets.
Log rotation prevents logs from consuming the entire disk. Check the system's rotation policy, commonly under /etc/logrotate.d/nginx, and verify that the rotated files remain readable when investigating an older incident.
Exercise
A request returns 502, but the access log contains only the status and size. What should you inspect next?
Show answer
Match the request time in Nginx's error log, then check whether the configured upstream application is listening, reachable, and returning a valid response.