How to config NGINX logging for Application Performance Monitoring

Chi Thuc Nguyen
2 min readMay 18, 2020

NGINX Built‑In Timing Variables

NGINX provides a number of built‑in timing variables that you can include in log entries. All are measured in seconds with millisecond resolution.

  • $request_time – Full request time, starting when NGINX reads the first byte from the client and ending when NGINX sends the last byte of the response body
  • $upstream_connect_time – Time spent establishing a connection with an upstream server
  • $upstream_header_time – Time between establishing a connection to an upstream server and receiving the first byte of the response header
  • $upstream_response_time – Time between establishing a connection to an upstream server and receiving the last byte of the response body

Define log format in nginx.conf file (Ubuntu 18.04 used as example here)

$ sudo vim /etc/nginx/nginx.conf

And add a log format in http section:

http {
...
##
# Logging Settings
##
log_format apm '"$time_local" client=$remote_addr '
'method=$request_method request="$request" '
'request_length=$request_length '
'status=$status bytes_sent=$bytes_sent '
'body_bytes_sent=$body_bytes_sent '
'referer=$http_referer '
'user_agent="$http_user_agent" '
'upstream_addr=$upstream_addr '
'upstream_status=$upstream_status '
'request_time=$request_time '
'upstream_response_time=$upstream_response_time '
'upstream_connect_time=$upstream_connect_time '
'upstream_header_time=$upstream_header_time';
...
}

Then in your site config, add the log format param to your access_log:

server {
...
access_log /var/log/nginx/example.com-access.log apm;
...
}

Restart nginx with command sudo service nginx restart, and you can see new params in your site’s access log file: $ tail -f /var/log/nginx/example.com-access.log

18/May/2020:11:03:51 +0700 client=10.20.28.251 method=POST request="POST /api/hello HTTP/1.1" request_length=865 status=200 bytes_sent=257 body_bytes_sent=62 referer=- user_agent="okhttp/3.9.0" upstream_addr=127.0.0.1:8803 upstream_status=200 request_time=0.014 upstream_response_time=0.012 upstream_connect_time=0.000 upstream_header_time=0.012

Reference

--

--