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.
$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:
# Logging Settings
log_format apm '"$time_local" client=$remote_addr '
'method=$request_method request="$request" '
'status=$status bytes_sent=$bytes_sent '
Then in your site config, add the log format param to your access_log:
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