We might have faced application performance issues such as slow loading , API’s are not responding etc.In order to track down what is causing this issue why it is taking a lot of time to load the applications ,
We need to customise our Nginx configuration So that we can monitor the request and response time of each requests from the clients.
Using Built-in Nginx Timing Variables:
NGINX provides a number of timing variables which you can include in the nginx configuration.
All the responses are measured in milliseconds.
- $upstream_response_time : Time between establish a connection to the upstream server and receiving the last byte of the response body.
- $request_time : This is the Full request time , Starting from , the nginx reading the bytes from the client , till Nginx sends the last byte of the response body to the client.
- $upstream_connect_time : The time spent establishing a connection with the upstream server
- $upstream_header_time : Time between establishing a connection to an upstream server and receiving the first byte of the response header.
Real-time Nginx Configuration:
Here is the log format which i have introduced into /etc/nginx/nginx.conf to track application request and response time.
log_format timed_combined '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' '$request_time $upstream_response_time;
Also I have implemented a new logging format timed_combined which will give us some extra information in the log.
To configure the same , Login to the server and Go to the Nginx root directory, /etc/nginx/nginx.conf
if you dont have Nginx web server , You can follow my other articles:
Okay, Lets continue with this,
Add the above configuration.
Once more thing we need to modify is , We have to tell the access_log to use the above log format.
Edit the access_log configuration as shown below.
access_log /var/log/nginx/access.log timed_combined;
Save and close, restart the Nginx service.
Here the example output after the change in Nginx logging.
126.96.36.199 - - [12/Jan/2020:14:45:48 +0530] "GET /test/healthz HTTP/1.1" 200 76 "-" "Go-http-client/1.1" 0.001 0.004
188.8.131.52 – IP from where we got request from
/test/healthz – Request was sent to this API
GET – Request method
0.001 0.004 – time take to server the complete request.
As there are multiple APM (Application performance Monitoring) tools available to investigate the performance of the applications But it’s quite time consuming and expensive setup.
This is the one best simple and easy solution to immediately analyze and rectify the issues within the applications.
Hope you learnt something about Nginx custom logging.
If you liked it, check out my other articles.