Nginx: Show active connections

Here is something I stumbled upon some time ago and did put it on my “To do later when I have more time” list.

It’s nothing that will change the world as we know it, but it will give you some statistics about your NGINX server.

NGINGX has a module called  HttpStubStatusModule which provides you statistics about number of requests handled and  connections made to your nginx server.

Today I got a few minutes over and I decided it was time to test it on one of my Virtual Private Servers.

First of all you need to check if your nginx installation has the module compiled, check it by running “nginx -V“:

if you have this “–with-http_sub_module” option in configure arguments, you are good to go. The results might look something like this

[precode]

# nginx -V
nginx version: nginx/1.2.7
TLS SNI support enabled
configure arguments:  –with-http_sub_module

[/precode]

(the configure arguments part above is incomplete, it should contain alot more. I’ve shortened it for readability).

So, now that we have established that the module is available it’s time to edit your nginx configuration file.

In my case, using debian and MINSTALL, I decided to add it to the default.conf file found in /etc/nginx/hosts.d/

[precode]

nano /etc/nginx/hosts.d/default.conf

[/precode]

Add this into the server block

[precode]

location /nginx_status {
stub_status on;
access_log off;
#If you want to secure access to this virtual directory
allow 192.168.1.100; #Change this to your IP
#Uncomment the next line if you want to allow access to this from anywhere
#allow all;
deny all;
}

[/precode]

Save and restart nginx. Test it

[precode]

curl http://localhost/nginx_status

Active connections: 6
server accepts handled requests
31 31 202
Reading: 0 Writing: 1 Waiting: 5
[/precode]

And some explanation of the output

[precode]
active connections – number of all open connections
server accepts handled requests – nginx accepted 5 connections, handled 5 connections (no one was closed just it was accepted), and handles 8 requests (1.6 requests per connection)

reading – nginx reads request header
writing – nginx reads request body, processes request, or writes response to a client
waiting – keep-alive connections, actually it is active – (reading + writing)
[/precode]

So what can we do with this information? It’s not that interesting when presented as pure text.
While reading about the HttpStubStatusModule module I noticed a link to a webpage that took advantage of this status module and used rrdtool to create some nice graphics from the result.
Now that is something all people love, graphs!

Read the instruction on how to use rrdtool here.

2 comments for “Nginx: Show active connections

  1. SplitIce
    August 11, 2013 at 10:23

    If you need access to the data for use in rrdtool or other pieces of monitoring software a script like the following could help (and doesn’t require perl!).

    Replace $1 with the IP and $2 with the Port

    wget -O- -q $1:$2 | awk ‘/^Active/ {print $NF}’
    wget -O- -q $1:$2 | awk ‘/Reading/ {print $$2}’
    wget -O- -q $1:$2 | awk ‘/Writing/ {print $$4}’
    wget -O- -q $1:$2 | awk ‘/Waiting/ {print $$6}’
    wget -O- -q $1:$2 | awk ‘/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$1}’
    wget -O- -q $1:$2 | awk ‘/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$2}’
    UserParameter=nginx.requests[*],wget -O- -q $1:$2 | awk ‘/^[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+[0-9]+/ {print $$3}’

    Or in the format needed for a Zabbix configuration file: http://www.x4b.net/files/zabbix-nginx.txt

    This assumes you have a specific IP:Port combination (e.g 127.0.0.1:78) that you use for revealing status information in a secure manner.

    I hope this helps.

  2. SplitIce
    August 11, 2013 at 10:24

    Correction remove “UserParameter=nginx.requests[*],” as that is zabbix specific.

Leave a Reply

Your email address will not be published. Required fields are marked *