Get Number of Active Connections in Nginx

Get Number of Active Connections in Nginx

Nginx, a powerful web server and reverse proxy, is widely used to manage internet traffic efficiently. Whether you're a web developer, a system administrator, or just curious about server metrics, understanding how to monitor active connections in Nginx is important for optimizing performance and ensuring the smooth operation of the web infrastructure. This tutorial explains how to get the number of active connections in Nginx.

The ngx_http_stub_status_module module allows accessing to basic status information within Nginx. On most Linux distributions, Nginx comes compiled with this module.

The following command can be used to check if status module is available within Nginx:

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

If you see with-http_stub_status_module as output in the terminal, means the module is available.

After verifying the module, the following configuration should be added in the Nginx server block:

server {
    # ...

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

When a request is made to the /nginx_status URI, the stub_status module is invoked, providing access to basic status information about the Nginx server. The access_log off directive disables logging to reduce unnecessary disk I/O. The allow 127.0.0.1 directive permits access only to requests originating from the specific IP address. Finally, the deny all directive denies access to all other IP addresses. Ensure to replace 127.0.0.1 with your server's actual IP address.

The page will provide the number of active connections, which may look like as follows:

Number of Active Connections in Nginx

Leave a Comment

Cancel reply

Your email address will not be published.