This tutorial will show how to collect Nginx real-time connection metrics, like active
, reading
, writing
, waiting
. Including protection with simple auth_basic
.
Do not forget to change the value in between <
and >
symbols to your own value!
# Go to nginx directory
cd /etc/nginx
# There are various online websites
# where you can easily generate
# login / password pair
# if you don't trust them
# use htpasswd CLI as shown below
htpasswd .htpasswd <username>
# You will be asked to prompt a password for the user.
Let's assume you're editing /etc/nginx/sites-available/default
# Go to the sites-available directory
cd /etc/nginx/sites-available/
# Edit `default` file, we using nano in the example below, you're free to use your favorite editor
nano default
Configuration:
server {
listen 80 default_server deferred;
server_name example.com;
location / {
# Return empty response or do something else here
return 204;
}
location ~ ^/ngstat/ {
# Use auth_basic with .htpasswd created above
auth_basic "Show your pass please";
auth_basic_user_file /etc/nginx/.htpasswd;
try_files $uri @stat;
}
location @stat {
# Internal redirect
rewrite ^(.*)$ /__$1 last;
}
# Return as JSON
location /__/ngstat/json {
# This block is accessible only
# after successful authentication
internal;
default_type "application/json";
return 200 "{\"active\":$connections_active, \"reading\":$connections_reading, \"writing\":$connections_writing, \"waiting\":$connections_waiting}";
}
# Return as XML
location /__/ngstat/xml {
# This block is accessible only
# after successful authentication
internal;
default_type "text/xml";
return 200 "<data><active>$connections_active</active><reading>$connections_reading</reading><writing>$connections_writing</writing><waiting>$connections_waiting</waiting></data>";
}
}
service nginx configtest
service nginx reload
Next URLs will ask for login and password and return Nginx stats upon successful authentication:
http://example.com/ngstat/json
http://example.com/ngstat/xml
Add endpoint at ostr.io:
- Go to Monitoring
- Click on "Add Endpoint"
- To select server type-in its name
- Select "HTTP" (or HTTPS) in "Protocol" drop-down list
- Select desired "Check Frequency"
- Click on "Add authentication"
- Enter username provided to
htpasswd
command - Enter password provided to
htpasswd
command - Click on "Add" button
- Done!