LLI Wiki — My Digital Knowledge Base

Nginx: monitoring with stub_status

Enable and test the nginx stub_status endpoint, and count client IPs from journalctl.

Nginx can expose basic connection counters through the stub_status module, which is handy for monitoring. This note shows how to check that the module is available, enable the endpoint for local access only, and test it.

Check that the module is compiled in

nginx -V 2>&1 | grep --color -- --with-http_stub_status_module

The access log is by default in /var/log/nginx/access.log.

Expose the status endpoint

Add a location block inside the server block of the relevant site configuration (for example a file in /etc/nginx/conf.d/). Access is restricted to localhost:

location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
}

Validate, reload and test

sudo nginx -t
sudo nginx -s reload
# or
sudo systemctl restart nginx

curl -k https://127.0.0.1/nginx_status

If the site is selected by server name, send the expected host header:

curl -k -H "Host: www.example.com" https://localhost/nginx_status

See the curl documentation on connection name resolution.

Count client IPs from the journal

For an application served through uWSGI, this extracts the IP addresses logged for a given application and ranks them by number of requests:

journalctl -u 'uwsgi*' -b --no-pager | grep "MediaWiki" | grep -oP '(?<=\] )([0-9]{1,3}\.){3}[0-9]{1,3}' | sort | uniq -c | sort -nr

Adapt the grep "MediaWiki" filter to the application name that appears in your logs.

Mastodon LLIMastodon SocialFosstodon