GrepBoard/Manuals
605 manuals ·

Add response times to the Nginx log and find slow requests

·Tested on Ubuntu 24.04 · nginx 1.24.0 · GNU awk 5.2.1 · log generated behind a test backend·Test scope ↓·7 steps

The default Nginx log has no timing. Add request_time and upstream_response_time with log_format, then find slow requests and slow paths with awk.

The default combined format does not record how long a request took. Define a new log_format with two timing variables, then filter the log. To count errors by status code instead, see Find which paths return 5xx.

[1] Define the format in the http block and name it in access_log. The file is usually /etc/nginx/nginx.conf.

/etc/nginx/nginx.conf
http {   log_format timed '$remote_addr [$time_local] "$request" $status $body_bytes_sent '                    '$request_time "$upstream_response_time"';   access_log /var/log/nginx/access.log timed;}

$upstream_response_time is quoted because it can hold several values such as 0.000, 0.012 (see the notes below).

[2] Test the syntax and reload.

console
$ sudo nginx -t && sudo systemctl reload nginx

[3] New lines look like this. These come from a test app that answers 1.2 seconds late on purpose; client addresses are replaced.

output
203.0.113.7 [19/Oct/2026:14:50:49 +0900] "GET /api/report HTTP/1.1" 200 200 1.201 "1.201"203.0.113.7 [19/Oct/2026:14:50:53 +0900] "GET /static/a.css HTTP/1.1" 200 3 0.000 "-"

The last two values are $request_time and $upstream_response_time. In this format the path is field 5 and the response time is field 9.

[4] List the slowest requests first. Sort on field 9, numeric, descending.

console
$ sort -k9 -rn access.log | head -3203.0.113.7 [19/Oct/2026:14:50:52 +0900] "GET /download HTTP/1.1" 200 20000000 3.038 "0.049"203.0.113.7 [19/Oct/2026:14:50:46 +0900] "GET /api/report HTTP/1.1" 200 200 1.204 "1.203"203.0.113.7 [19/Oct/2026:14:50:46 +0900] "GET /api/report HTTP/1.1" 200 200 1.203 "1.203"

To count everything above a threshold, use a comparison: awk '$9 > 1' access.log | wc -l. In the test log (158 lines) 7 requests took more than 1 second.

[5] Get count, average and maximum per path. Strip the query string and fold numbers into :id so /api/items/1 and /api/items/2 become one row.

console
$ awk '{split($5,p,"?"); k=p[1]; gsub(/[0-9]+/,":id",k); n[k]++; s[k]+=$9; if ($9>m[k]) m[k]=$9} END {for (x in n) printf "%-14s %4d %7.3f %7.3f\n", x, n[x], s[x]/n[x], m[x]}' access.log | sort -k4 -rn/download         1   3.038   3.038/api/report       6   1.202   1.204/api/search      40   0.401   0.405/login           30   0.022   0.030/api/items/:id   80   0.012   0.017/static/a.css     1   0.000   0.000

Columns are path, count, average and maximum in seconds. Sorting on the total (s[k]) instead pulls up paths like /api/search that are only a little slow but hit often.

[6] For percentiles, extract the values, sort them, and read by position. An average hides a handful of slow requests.

console
$ awk '{print $9}' access.log | sort -n | awk '{a[NR]=$1} END {print a[int(NR*0.95)], a[int(NR*0.99)]}'0.404 1.203

The 95th percentile is 0.404 s and the 99th is 1.203 s.

[7] Compare $request_time with $upstream_response_time. Per the documentation, the first runs from the first bytes read from the client to the log write after the last bytes are sent; the second is the time spent receiving the response from the upstream. A large gap points at a slow client transfer, not the app.

console
$ awk 'match($0, /"[^"]*"$/) {u=substr($0,RSTART+1,RLENGTH-2); n=split(u,a,", "); if (a[n]!="-" && $9-a[n] > 0.5) print $5, $9, a[n]}' access.log/download 3.038 0.049

/download was a 20 MB response sent to a client reading at 4 MB/s. The upstream finished in 0.049 s and the remaining ~3 s went to the transfer. Tuning the app would not help here.

Analyze your Nginx log in the browser ↗ — nothing is uploaded.

#nginx#awk#logs#performance