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:

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

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Lets talk AWS Simple Storage Service

The little triangle hidden by requirement

Part 2 Challenge #8 Solution

Why Every Bit Is Not Equal — a Primer in Computer Memory

Terraform tips for newcomers

Stream your data: On-Prem MS-SQL to CloudSQL SQL Server to BigQuery (Part-1)

IS IT SAFE AND SECURE TO USE SELECTORSHUB?

Coding Tutor Tip-4-Suit Up With Essential Computer Gear

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chi Thuc Nguyen

Chi Thuc Nguyen

More from Medium

An intermediate’s guide to Git Version Control — in terminal: branching and merging

Software Deployment With Docker

Oracle MySQL Heatwave — Tips and Hacks

HOW TO DEPLOY A .NET CORE API TO AWS USING TERRAFORM AND DOCKER