Track and log slow nginx requests
Sometimes, we need to find slowly response times on our server delivered by nginx from proxied servers like PHP, Node or Angular SSR. Some technologies used on Reyesoft projects.
First of all, we need to log all nginx access logs with their resepective request time. On nginx configuration you need to change access_log to:
log_format timed_combined '$request_time $upstream_response_time'
' [$time_local]'
' "$request" $status $body_bytes_sent';
access_log /var/log/nginx/access.log timed_combined;
The previous code, do this on your access.log file
$ tail -f /var/log/nginx/access.log
0.002 0.000 [25/Feb/2022:19:06:29 +0000] "GET /1630-es2015.js HTTP/1.1" 200 4524
0.003 0.004 [25/Feb/2022:19:06:29 +0000] "GET /assets/landing-page.jpg HTTP/1.1" 200 814
1.765 1.764 . [25/Feb/2022:19:06:34 +0000] "GET /saldo/banco/ HTTP/1.1" 200 98333
0.007 0.008 . [25/Feb/2022:19:06:55 +0000] "GET /6997-es2015.f767960602ec613104db.js HTTP/1.1" 200 933
As you can see, GET /saldo/banco/ of our site of Intercambio de Saldo are taking a lot of time: 1.765 seconds.
But, we need to see and watch only slowly requests. Then we just need run this:
$ tail -f /var/log/nginx/access.log | awk '$1+0 >= 0.5'
1.765 1.764 . [25/Feb/2022:19:06:34 +0000] "GET /saldo/banco/ HTTP/1.1" 200 98333
🎉 Voalá!