Sub topic 11
Reading access and error logs
Nginx records what it does in two kinds of log. The access log has one line per request. The error log holds Nginx's own diagnostics. This chapter assumes you can edit Nginx configuration, reload it safely with nginx -t, and read basic HTTP status codes. It covers where the logs are, how to read them, how to change their format, and how to use them to find problems.
Where the logs live
On Debian and Ubuntu packages, both logs are in /var/log/nginx/. The defaults are set in /etc/nginx/nginx.conf.
| Log | Default path | Directive | Contents |
|---|---|---|---|
| Access log | `/var/log/nginx/access.log` | `access_log` | One line per completed request |
| Error log | `/var/log/nginx/error.log` | `error_log` | Startup problems, upstream failures, file errors, warnings |
Reading these files usually requires root or membership in the adm group. Start with these commands:
sudo ls -l /var/log/nginx/
sudo tail -n 20 /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.logtail -f keeps the file open and prints new lines as they arrive. Press Ctrl+C to stop it.
The default access log format
If you set no format, Nginx uses a predefined format named combined. A line looks like this:
203.0.113.7 - - [28/Nov/2026:04:06:42 +0000] "GET /index.html HTTP/1.1" 200 612 "https://example.com/" "Mozilla/5.0 (X11; Linux x86_64)"The fields are, in order:
203.0.113.7is the client address ($remote_addr).-is a legacy identity field. It is almost always empty.-is the authenticated user ($remote_user). It is empty unless you use HTTP basic auth.[28/Nov/2026:04:06:42 +0000]is the local time of the request ($time_local)."GET /index.html HTTP/1.1"is the request line ($request).200is the response status ($status).612is the size of the response body in bytes ($body_bytes_sent).- The referer is the page that linked to this one (
$http_referer). - The user agent identifies the client software (
$http_user_agent).
The status code is the first thing to check. 2xx means success, 3xx a redirect, 4xx a client-side problem such as a missing file, and 5xx a server-side failure.
Changing the format and location
Define a format with log_format in the http context. Then select it with access_log. The example below adds timing fields, which the default format lacks:
http {
log_format timed '$remote_addr [$time_local] "$request" '
'$status $body_bytes_sent '
'rt=$request_time urt=$upstream_response_time '
'host=$host';
access_log /var/log/nginx/access.log timed;
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/example.com.access.log timed;
}
}Key points:
$request_timeis the total time from the first byte read from the client until the last byte is sent.$upstream_response_timeis the time the proxied backend took. It is empty for requests Nginx answered itself, such as static files.- An
access_loginsideserverorlocationreplaces the one inherited fromhttp. It does not add to it. To write to two files, list twoaccess_logdirectives in the same context. access_log off;disables logging in that context. This is useful for health-check endpoints that would otherwise flood the file.
Per-site log files keep each virtual host's traffic separate, which makes later analysis simpler.
The error log and its levels
The error log has a severity threshold, set by the second argument of error_log:
error_log /var/log/nginx/error.log warn;From least to most severe, the levels are debug, info, notice, warn, error, crit, alert, and emerg. A level shows messages at that severity and above. The default is error. The debug level works only if Nginx was built with debugging support, and it produces very large output. Use it briefly and turn it off.
A typical error entry:
2026/11/28 04:10:03 [error] 1234#1234: *57 open() "/var/www/example.com/html/missing.png" failed (2: No such file or directory), client: 203.0.113.7, server: example.com, request: "GET /missing.png HTTP/1.1", host: "example.com"Read it this way:
2026/11/28 04:10:03is the timestamp.[error]is the severity.1234#1234is the process ID and thread ID of the worker.*57is the connection number. Lines from the same connection share it, so you can follow one request through several messages.- The message describes the failure. Here the file does not exist. The text in parentheses is the operating system error:
2isENOENT. - The trailing fields give the client, server name, request, and host.
Matching the two logs is a common technique. A 404 in the access log has a corresponding open() ... failed line in the error log that names the exact path Nginx tried. This often reveals a wrong root or alias value.
Diagnosing problems from log entries
Some messages recur often enough to learn by heart.
| Error log message | Likely cause | Fix |
|---|---|---|
| `open() ... failed (2: No such file or directory)` | The path built from `root` and the URI does not exist | Check `root`, `alias`, and the file's location |
| `open() ... failed (13: Permission denied)` | The `nginx` worker user cannot read the file or a parent directory | Fix ownership or add execute permission on each directory in the path |
| `connect() failed (111: Connection refused) while connecting to upstream` | Nothing is listening at the `proxy_pass` address | Start the application or correct the address and port |
| `upstream timed out (110: Connection timed out)` | The backend was too slow to answer | Investigate the backend, or raise `proxy_read_timeout` if the delay is legitimate |
| `directory index of "..." is forbidden` | The directory has no index file and autoindex is off | Add the index file or adjust `index` |
| `limiting requests, excess: ...` | The `limit_req` rule rejected a request | Expected under load. Tune `rate` and `burst` if clients are legitimate |
The status codes in the access log point to the same problems from the other side:
404often pairs with a "No such file" error.403pairs with permission errors or a forbidden directory index.502 Bad Gatewaypairs with a refused connection or a malformed backend response.504 Gateway Timeoutpairs with an upstream timeout.
Analyzing logs with shell tools
The default format is whitespace-separated enough for awk. In combined format, field 9 is the status and field 7 is the request path.
# Count responses by status code
sudo awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rn
# Top 10 requested paths that returned 404
sudo awk '$9 == 404 {print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10
# Top 10 client addresses
sudo awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10
# Recent 5xx responses
sudo awk '$9 ~ /^5/' /var/log/nginx/access.log | tail -n 20These field numbers apply only to the combined format. If you define a custom format, the positions change, so recount them. For heavy analysis, a custom format that is easier to parse, such as JSON through escape=json on log_format, is a better choice.
Log rotation
Log files grow without limit. Debian and Ubuntu packages install a logrotate rule at /etc/logrotate.d/nginx. It rotates the files on a schedule, compresses old copies, and then tells Nginx to reopen its log files.
That last step matters. After a rename, Nginx still holds the old file open and keeps writing to it. A USR1 signal makes the master process reopen its files. You can send it by hand:
sudo mv /var/log/nginx/access.log /var/log/nginx/access.log.1
sudo nginx -s reopennginx -s reopen sends the USR1 signal for you. If a log file appears empty after rotation while the old one grows, a missing reopen is the usual cause.
Also check disk space with df -h /var/log. A full disk can stop Nginx from logging, and it can cause failures elsewhere on the server.
The request flow into the logs
The diagram shows when each log receives an entry.
Every request ends with an access log line, whether it succeeded or not. Only some requests also produce an error log entry. A request with a 404 for a missing file produces both. A request for a path that a return 404; rule blocks on purpose produces only the access line.
A worked diagnosis
Suppose users report that /api/ returns errors. You run:
sudo tail -n 5 /var/log/nginx/access.log
sudo tail -n 5 /var/log/nginx/error.logThe access log shows "GET /api/users HTTP/1.1" 502 157. The error log shows:
2026/11/28 04:15:21 [error] 1234#1234: *90 connect() failed (111: Connection refused) while connecting to upstream, client: 203.0.113.7, server: example.com, request: "GET /api/users HTTP/1.1", upstream: "http://127.0.0.1:3000/api/users", host: "example.com"The message names the upstream address, 127.0.0.1:3000. You check whether anything listens there:
sudo ss -tlnp | grep ':3000'If there is no output, the application is down. Starting it restores service, and the next access log line shows 200. No Nginx configuration change was needed.
Exercise
A page returns 404 and the error log shows open() "/var/www/example.com/html/assets/logo.png" failed (2: No such file or directory). You are sure the file is at /var/www/example.com/html/img/logo.png. What do you check, and which log field told you the path Nginx tried?
Show answer
The error log message names the exact path Nginx tried: /var/www/example.com/html/assets/logo.png. Because it was built from root plus the request URI /assets/logo.png, either the page links to the wrong URL (/assets/ instead of /img/) or a location block needs a different root or alias. Check the HTML that requests the file and the matching location block, then run sudo nginx -t && sudo systemctl reload nginx after any change.
Exercise
You add access_log /var/log/nginx/api.log timed; inside a location /api/ block. Requests to /api/ no longer appear in the main access.log. Why, and how can you log to both files?
Show answer
An access_log directive in an inner context replaces the one inherited from the outer context. To write to both files, put both access_log directives in the location block, one for /var/log/nginx/access.log and one for /var/log/nginx/api.log timed.