{"id":19256,"date":"2015-04-24T09:50:26","date_gmt":"2015-04-24T04:20:26","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=19256"},"modified":"2015-04-28T12:37:00","modified_gmt":"2015-04-28T07:07:00","slug":"monitoring-nginx-connections","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/monitoring-nginx-connections\/","title":{"rendered":"Monitoring Nginx Connections"},"content":{"rendered":"<p>The Nginx web server comes with a bundled module &#8220;HttpStubStatusModule&#8221;. By enabling this module we will get some insight of our nginx connections. This module provides the following information.<br \/>\n<strong><\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">Active Connections<\/h3>\n<h3 style=\"text-align: justify;color: #ff9900\">Connection Status (Accepted \/ Handled)<\/h3>\n<h3 style=\"text-align: justify;color: #ff9900\">Connection Request \/ Second<\/h3>\n<p><\/strong><\/p>\n<p><strong>Enable Module<\/strong><br \/>\nTo get connection stats, we need to enable the status module on <strong>nginx VirtualHost<\/strong> file by adding below localtion block.<br \/>\n[js]<br \/>\nlocation \/nginx_status {<br \/>\n        # Turn on stats<br \/>\n        stub_status on;<br \/>\n        allow 127.0.0.1;<br \/>\n        deny all;<br \/>\n}<\/p>\n<p>Replace 127.0.0.1 with the IP from which you wants to access \/nginx_status<br \/>\n[\/js]<br \/>\n[js]$ sudo service nginx reload [\/js]<br \/>\n[js]$ curl localhost\/nginx_status [\/js]<\/p>\n<p>[js]<br \/>\nActive connections: 32<br \/>\nserver accepts handled requests<br \/>\n48932 48932 2594381<br \/>\nReading: 0 Writing: 2 Waiting: 30<br \/>\n[\/js]<\/p>\n<p>Let&#8217;s break down the above parts.<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">1. Active Connections (Number of Active Connection): <\/h3>\n<p> This will give all open connections. This doesn&#8217;t mean number of users on site.<\/p>\n<h3 style=\"text-align: justify;color: #ff9900\">2. Server accepts handled requests:<\/h3>\n<p>We have three values for this.<br \/>\n<em><br \/>\nFirst Column: 48392 ( Total Accepted Connection by Web Server )<br \/>\nSecond Column: 48932 ( Total Connections handled by Web  Server )<br \/>\nThird Column :  2594381 ( Number of handled requests )<br \/>\n<\/em><\/p>\n<p>If we divide third column by second column, we&#8217;ll get the number of requests \/ second<\/p>\n<p>Hence, <\/p>\n<h6 style=\"text-align: justify;color: #ff9900\">Number of Requests\/Second = 2595381 \/ 48392 =  53.63 <\/h6>\n<p> requests is being served per second by nginx.<\/p>\n<p>You can also use the below script and execute it as a cron and check Connection Status on a timely basis.<\/p>\n<p>[js]<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>numberOfActiveConnections=&quot;&quot;<br \/>\nnumberOfTotalAcceptedConnections=&quot;&quot;<br \/>\nnumberOfTotalhandledConnections=&quot;&quot;<\/p>\n<p>#Number of Requests \/ Second<br \/>\nnumberOfRequestsPerSecond=&quot;&quot;<\/p>\n<p>nginxPath=&#8217;nginx_status&#8217;<\/p>\n<p>ApplicationURL=&#8217; List of IPAddress\/DomainName separated by Space &#8216;<\/p>\n<p>echo $ApplicationURL<\/p>\n<p>for URL in ${ApplicationURL[@]};do<\/p>\n<p>    file=&quot;$URL&quot;<br \/>\n    echo &quot;Getting Nginx Status Page of $URL&quot;<\/p>\n<p>    curl -s $URL\/$nginxPath -o $file<\/p>\n<p>    numberOfActiveConnections=$(cat $file  | awk &#8216;\/Active \/&#8217; | awk &#8216;{print $3}&#8217;)<br \/>\n    echo &quot;$file : Active Connection $numberOfActiveConnections&quot;<\/p>\n<p>    numberOfTotalAcceptedConnections=$(cat $file | grep -A 1 server | tail -1 | awk &#8216;{print $1}&#8217;)<br \/>\n    echo &quot;$file : Total Accepted Connection $numberOfTotalAcceptedConnections&quot;<\/p>\n<p>    numberOfTotalhandledConnections=$(cat $file | grep -A 1 server | tail -1 | awk &#8216;{print $2}&#8217;)<br \/>\n    echo &quot;$file : Total Handled Connection $numberOfTotalhandledConnections&quot;<\/p>\n<p>    echo &quot;Number of Accepted and Handle remains same most of the time.&quot;<br \/>\n    totalRequests=$(cat $file | grep -A 1 server | tail -1 | awk &#8216;{print $3}&#8217;)<\/p>\n<p>    numberOfRequestsPerSecond=$(echo &#8216;scale=2;&#8217;$totalRequests\/$numberOfTotalAcceptedConnections | bc -l)<br \/>\n    echo &quot;Currently Server is serving $numberOfRequestsPerSecond \/ second.&quot;<\/p>\n<p>    echo &quot;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&quot;<\/p>\n<p>done<\/p>\n<p>[\/js]<\/p>\n<p>Output will be like: <\/p>\n<p><strong>Getting Nginx Status Page of Application URL<br \/>\nApplication URL : Active Connection 39<br \/>\nApplication URL : Total Accepted Connection 62032<br \/>\nApplication URL : Total Handled Connection 59739<br \/>\nNumber of Accepted and Handle remains same most of the time.<br \/>\nCurrently Server is serving 43.02 \/ second.<br \/>\n<\/strong><br \/>\nLeave a comment if you have any questions regarding this article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Nginx web server comes with a bundled module &#8220;HttpStubStatusModule&#8221;. 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 [&hellip;]<\/p>\n","protected":false},"author":100,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":35},"categories":[1],"tags":[519,1499,1336],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19256"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/100"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=19256"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19256\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=19256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=19256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=19256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}