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

How to Create a WordPress Salesforce Integration

Elasticsearch for Jekyll, Part 2

Advantages of the Crodo project !!!

Frontend Development & UI Design Tools of Mine

[Two Pointers] LC 881. Boats to Save People

Git Merges

Skype for Business 2019 vs Microsoft Teams 2021

Skype for Business vs Microsoft Teams

{UPDATE} Math Dino : Addition Hack Free Resources Generator

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

Helm— K8S Package Manager

Connect multiple databases MySQL + MongoDB (Docker-compose) in single spring boot application

Custom Image Deployment with Docker

Introduction to NodeJS