How to config NGINX logging for Application Performance Monitoring

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.

  • $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
$ sudo vim /etc/nginx/nginx.conf
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';
...
}
server {
...
access_log /var/log/nginx/example.com-access.log apm;
...
}
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