Monitoring Nginx Connections
The Nginx web server comes with a bundled module “HttpStubStatusModule”. By enabling this module we will get some insight of our nginx connections. This module provides the following information.
Active Connections
Connection Status (Accepted / Handled)
Connection Request / Second
Enable Module
To get connection stats, we need to enable the status module on nginx VirtualHost file by adding below localtion block.
[js]
location /nginx_status {
# Turn on stats
stub_status on;
allow 127.0.0.1;
deny all;
}
Replace 127.0.0.1 with the IP from which you wants to access /nginx_status
[/js]
[js]$ sudo service nginx reload [/js]
[js]$ curl localhost/nginx_status [/js]
[js]
Active connections: 32
server accepts handled requests
48932 48932 2594381
Reading: 0 Writing: 2 Waiting: 30
[/js]
Let’s break down the above parts.
1. Active Connections (Number of Active Connection):
This will give all open connections. This doesn’t mean number of users on site.
2. Server accepts handled requests:
We have three values for this.
First Column: 48392 ( Total Accepted Connection by Web Server )
Second Column: 48932 ( Total Connections handled by Web Server )
Third Column : 2594381 ( Number of handled requests )
If we divide third column by second column, we’ll get the number of requests / second
Hence,
Number of Requests/Second = 2595381 / 48392 = 53.63
requests is being served per second by nginx.
You can also use the below script and execute it as a cron and check Connection Status on a timely basis.
[js]
#!/bin/bash
numberOfActiveConnections=""
numberOfTotalAcceptedConnections=""
numberOfTotalhandledConnections=""
#Number of Requests / Second
numberOfRequestsPerSecond=""
nginxPath=’nginx_status’
ApplicationURL=’ List of IPAddress/DomainName separated by Space ‘
echo $ApplicationURL
for URL in ${ApplicationURL[@]};do
file="$URL"
echo "Getting Nginx Status Page of $URL"
curl -s $URL/$nginxPath -o $file
numberOfActiveConnections=$(cat $file | awk ‘/Active /’ | awk ‘{print $3}’)
echo "$file : Active Connection $numberOfActiveConnections"
numberOfTotalAcceptedConnections=$(cat $file | grep -A 1 server | tail -1 | awk ‘{print $1}’)
echo "$file : Total Accepted Connection $numberOfTotalAcceptedConnections"
numberOfTotalhandledConnections=$(cat $file | grep -A 1 server | tail -1 | awk ‘{print $2}’)
echo "$file : Total Handled Connection $numberOfTotalhandledConnections"
echo "Number of Accepted and Handle remains same most of the time."
totalRequests=$(cat $file | grep -A 1 server | tail -1 | awk ‘{print $3}’)
numberOfRequestsPerSecond=$(echo ‘scale=2;’$totalRequests/$numberOfTotalAcceptedConnections | bc -l)
echo "Currently Server is serving $numberOfRequestsPerSecond / second."
echo "—————————————————————————————————————"
done
[/js]
Output will be like:
Getting Nginx Status Page of Application URL
Application URL : Active Connection 39
Application URL : Total Accepted Connection 62032
Application URL : Total Handled Connection 59739
Number of Accepted and Handle remains same most of the time.
Currently Server is serving 43.02 / second.
Leave a comment if you have any questions regarding this article.