متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 66Observability · Nginx

How to find slow requests in Nginx logs

"The site is slow" — but which pages, since when, and is it Nginx or the app behind it? The default Nginx log does not contain the response time. This how-to adds $request_time and $upstream_response_time to the log, then uses awk and sort to list the slowest requests, the slowest URLs on average, and the 5xx errors per minute — fast answers from the logs you already have.

  • 5 الدروس
  • 10 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

66.1

The quick answer

/etc/nginx/conf.d/00-log-timed.conf
log_format timed '$remote_addr - [$time_local] "$request" $status $body_bytes_sent '
                 '"$http_referer" "$http_user_agent" rt=$request_time urt=$upstream_response_time';
access_log /var/log/nginx/access.log timed;
nginx -t && systemctl reload nginx
awk '{for(i=1;i<=NF;i++) if($i ~ /^rt=/){t=substr($i,4); if(t+0 > 1) print t, $6}}' /var/log/nginx/access.log | sort -rn | head -20

The last line lists requests slower than 1 second: time and URL. With this format the fields are: $1 IP, $3 time, $5 method, $6 URL, $8 status.

66.2

Step by step

  1. Log the times (quick answer). log_format goes in the http context — a file in conf.d/ is included there.
ValueMeans
$request_timeTotal time from the first byte of the request to the last byte sent (seconds, ms precision).
$upstream_response_timeTime the backend (app) needed. - if there was no backend.

rt high and urt high = the app is slow. rt high and urt low = slow client or network (big downloads, mobile).

  1. The slowest single requests (quick answer). Older rotated logs are gzipped — read them with zcat:
zcat /var/log/nginx/access.log.2.gz | awk '{for(i=1;i<=NF;i++) if($i ~ /^rt=/) print substr($i,4), $6}' | sort -rn | head
  1. Slowest URLs on average (with count), without query strings:
awk '{for(i=1;i<=NF;i++) if($i ~ /^rt=/){u=$6; sub(/\?.*/,"",u); s[u]+=substr($i,4); n[u]++}} END {for(u in s) if(n[u]>=20) printf "%.3f %6d %s\n", s[u]/n[u], n[u], u}' /var/log/nginx/access.log | sort -rn | head -15
0.842    311 /api/search
0.410   1022 /checkout
  1. Errors per minute (5xx) — did it start at a deploy?
awk '$8 ~ /^5/ {print substr($3,2,17)}' /var/log/nginx/access.log | uniq -c | tail -20
  1. Requests per IP (a bot or an attack can be the reason for "slow"):
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head
  1. For many servers or long periods use a log system: JSON logs (log_format … escape=json) into Loki or Elasticsearch, then query and graph in Grafana or Kibana. For a quick terminal report: goaccess.

66.3

Check that it worked

New log lines end with rt=0.012 urt=0.010, the commands print the slow URLs, and after a fix (index, cache, bigger pool) the average time of that URL goes down.

tail -1 /var/log/nginx/access.log

الأوامر في هذا الدرس

66.4

Common problems

ProblemCauseFix
No rt= in the logThe access_log in the server block still uses the default format.Add timed to that access_log line too.
nginx: [emerg] "log_format" directive is not allowed hereIt is inside a server block.Put log_format in http (a conf.d file).
urt=-Static file or cached answer — no backend was asked.Normal.
urt=0.002, 1.204 (two values)Nginx tried two backends (the first failed).Check the upstream errors in error.log.
The field numbers ($6, $8) are wrongAnother log format.Count the fields in one line, or use JSON logs and jq.
Very big logs, awk is slowWeeks of logs on a busy site.Filter by time first (grep '19/Nov/2026:10:'), or use Loki/Elasticsearch.

الأوامر في هذا الدرس

66.5

Go further

1Which Nginx variables give the total time and the backend time of a request?

$request_time (total) and $upstream_response_time (backend).

2rt is high but urt is low. Where is the time lost?

Not in the app — between Nginx and the client (slow network, big response).

كل الأوامر في هذا الفصل