Stats for NginX Upstream

When using NginX as a load balancer, the question comes up of how to properly track traffic delivery stats for the servers in the Upstream section.

Unfortunately Nginx has no built-in functionality like HaProxy for this. And you can’t add this functionality by installing an extra module via yum or apt-get.

To get NginX to have build from source and enable the third-party upstream-stats module during the build.

This module is compatible with nginx-1.2.2. I couldn’t get it to build against the latest NginX version.
Download NginX and unpack it:

wget http://nginx.org/download/**nginx-1.2.2**.tar.gz  
tar xf nginx-1.2.2.tar.gz

Download upstream-stats and unpack it:

wget /wp-content/uploads/2014/11/ustats.tgz  
tar xf ustats.tgz

Copy the files into the NginX folders

cp -r ustats-read-only/ustats nginx-1.2.2/src/http/modules/  
cp ustats-read-only/nginx.patch nginx-1.2.2/

All that’s left is to apply the patch and move on to building NginX

cd nginx-1.2.2/  
patch -p1 -i nginx.patch

The basic configure line looks like this:

./configure -prefix=/etc/nginx -user=nginx -group=nginx -with-http_ssl_module **-add-module=src/http/modules/ustats** -http-log-path=/var/log/nginx/

If you need extra options, check out this article. It also has an example init.d script and the list of packages needed for the build.

To get the stats working, add the following lines to the server section of any site:

location /ustats {
  ustats memsize=1m;
  ustats_refresh_interval 6000;
  ustats_html_table_width 95;
  ustats_html_table_height 95;
}

shot1