← All guides

Check Nginx & Apache Response Time: curl -w, Logs and ab

By Grzegorz Bukat, Founder of Zuzia · Updated:

Use curl -w for a quick measurement and the access log for real traffic. The access log only contains response time if you add it to the log format; by default neither nginx nor Apache records it.

curl -o /dev/null -s -w "status=%{http_code} ttfb=%{time_starttransfer}s total=%{time_total}s\n" http://localhost/

ttfb is the time until the first byte arrived, which is what the web server and application spent. total adds the download of the response.

Cheat Sheet

Task Command
One request, status and timing curl -o /dev/null -s -w "%{http_code} %{time_total}\n" http://localhost/
Time to first byte curl -o /dev/null -s -w "%{time_starttransfer}\n" http://localhost/
Timing by stage curl -o /dev/null -s -w "dns=%{time_namelookup} connect=%{time_connect} ttfb=%{time_starttransfer}\n" URL
Slowest nginx requests sort -k4 -rn on a log with $request_time (see below)
Benchmark 200 requests, 10 at a time ab -n 200 -c 10 http://localhost/
Is the server busy uptime, ss -s
nginx status and workers systemctl status nginx
Apache status and workers systemctl status apache2 (httpd on RHEL)

Measure With curl

# Several URLs at once
for url in http://localhost/ http://localhost/api http://localhost/health; do
  printf '%s ' "$url"
  curl -o /dev/null -s -w "%{http_code} %{time_total}s\n" "$url"
done

# Repeat every 5 seconds
while true; do echo "$(date +%T) $(curl -o /dev/null -s -w '%{time_total}' http://localhost/)"; sleep 5; done

Run it on the server against localhost to remove the network. If localhost is fast but the same URL from outside is slow, the problem is the network, TLS or a CDN; compare with the stage timings in Slow API response times.

Read Response Time From the Access Log

Add the time to the log format. nginx, in the http block:

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;

$request_time is the whole request in seconds with millisecond resolution; $upstream_response_time is the time spent waiting for PHP-FPM, Node or another upstream (it is - for static files). Reload with nginx -t && systemctl reload nginx.

Apache, in the virtual host or main config:

LogFormat "%h %t \"%r\" %>s %b %D" timed
CustomLog ${APACHE_LOG_DIR}/access.log timed

%D is the time in microseconds (%T is whole seconds). Then analyse the log:

# nginx: slowest 10 requests (request_time is the second-to-last field)
awk '{print $(NF-1), $7}' /var/log/nginx/access.log | sort -rn | head -10

# nginx: requests slower than 1 second
awk '$(NF-1) > 1.0 {print $(NF-1), $7}' /var/log/nginx/access.log | tail

# nginx: average request time
awk '{s+=$(NF-1); n++} END {print s/n " s average over " n " requests"}' /var/log/nginx/access.log

# Apache: requests slower than 1 second (%D is the last field, in microseconds)
awk '$NF > 1000000 {print $NF/1000000 " s", $7}' /var/log/apache2/access.log | tail

Field positions assume the formats above; if your log format differs, adjust the field numbers. When $request_time is high but $upstream_response_time is low, the delay is outside the application, usually a slow client. When both are high, the application or database is the slow part.

Benchmark With ab

sudo apt install apache2-utils     # Debian, Ubuntu (provides ab)
sudo dnf install httpd-tools       # RHEL, Rocky, Alma
ab -n 200 -c 10 http://localhost/

Read Requests per second, Time per request and the percentile table at the bottom (50%, 95%, 99%). Failed requests should be 0. Benchmark a staging copy or a quiet period: ab generates real load.

When Responses Are Slow

uptime                          # load against nproc
free -h                         # memory and swap
ss -s                           # connection counts
tail -50 /var/log/nginx/error.log
Symptom in the error log Meaning
upstream timed out (110: Connection timed out) while reading response header from upstream The application took longer than proxy_read_timeout or fastcgi_read_timeout; see 502 Bad Gateway
worker_connections are not enough nginx hit its connection limit; raise worker_connections
connect() failed (111: Connection refused) while connecting to upstream The backend is down
Apache server reached MaxRequestWorkers setting All Apache workers busy; requests queue

Get alerted when this changes

Commands from this guide only help while you are looking at them. In Zuzia you save one as a scheduled task on the server, choose how often it runs and turn on "Monitor the result". The task alerts you when the command fails or its status line says WARNING or CRITICAL, by email, Slack, Discord, push or SMS, and the output of every run is kept. For that to work, the command has to fail when something is wrong, for example:

out=$(curl -fsS -m 2 -o /dev/null -w "%{time_total}s" http://localhost/ 2>&1) || { echo "CRITICAL - $out"; exit 2; }
echo "OK - $out"

Exit 0 means OK, 1 a warning and 2 critical. Here -m 2 makes curl fail when the page takes longer than 2 seconds.

FAQ

How do I check nginx response time?

Add $request_time to the log_format and read it from the access log, or measure one request with curl -o /dev/null -s -w "%{time_total}\n" <url>.

Does the nginx access log show response time by default?

No. The default combined format has no timing field. Add $request_time in a custom log_format.

What is the difference between $request_time and $upstream_response_time?

$request_time covers the whole request from the first byte read from the client to the last byte sent. $upstream_response_time is only the time the backend took.

What is a good response time?

For pages generated by an application, under 200-500 ms for time to first byte is good and over 1 second feels slow. Set limits from your own baseline.

Can ab or curl checks slow the server down?

A single curl per minute is negligible. ab produces load on purpose, so run it against staging or off-peak.

Related guides

We use cookies to ensure the proper functioning of our website.