هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
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 -20The 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.
Step by step
- Log the times (quick answer).
log_formatgoes in thehttpcontext — a file inconf.d/is included there.
| Value | Means |
|---|---|
$request_time | Total time from the first byte of the request to the last byte sent (seconds, ms precision). |
$upstream_response_time | Time 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).
- 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- 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- Errors per minute (5xx) — did it start at a deploy?
- Requests per IP (a bot or an attack can be the reason for "slow"):
- 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.
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.logCommon problems
| Problem | Cause | Fix |
|---|---|---|
No rt= in the log | The 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 here | It 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 wrong | Another log format. | Count the fields in one line, or use JSON logs and jq. |
| Very big logs, awk is slow | Weeks of logs on a busy site. | Filter by time first (grep '19/Nov/2026:10:'), or use Loki/Elasticsearch. |
Go further
- Command pages: awk, nginx, sort, jq.
- Next how-tos: Set up Nginx as a reverse proxy, Read the logs of a service, Build a Grafana dashboard, Search logs with Loki and LogQL, Rate-limit requests in Nginx.
- Full guides: Nginx, Loki, Elasticsearch.
- Learn it: RHCSA — awk, TLCL — sort and uniq.
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).