accounting_id

The `accounting_id` directive sets a unique identifier for traffic accounting metrics associated with incoming requests in NGINX.

Syntaxaccounting_id string;
Defaultnone
Contextstream, stream server
Arguments1

Description

The accounting_id directive is a critical component of the NGINX traffic accounting module, enabling the identification and organization of traffic metrics for incoming requests based on specified criteria. By defining the accounting_id, users can categorize and aggregate traffic data related to different routes or conditions, allowing for detailed monitoring and analysis of traffic. This metric is evaluated during the request processing phase, where the module retrieves the associated accounting_id to update traffic statistics accordingly.

When used within a server or location context, accounting_id can accept both predefined strings and NGINX variables, providing flexibility in how traffic is grouped. For example, it can dynamically set the identifier based on request headers (like $http_host) or specific routing conditions (like different user agents). The values assigned to accounting_id help the module keep track of metrics that are exported on a defined interval, aiding in historical data analysis and real-time monitoring. This system allows administrators to pinpoint usage patterns, detect anomalies, and better allocate server resources based on traffic demands.

It is important to configure the accounting directive to on and set the accounting_log directive to log the metrics appropriately for the accounting module to operate effectively. The configuration allows the logging mechanism to generate real-time reports or dashboard visualizations for easier traffic management.

Config Example

http {
    accounting on;
    accounting_log logs/http-accounting.log;
    server {
        server_name example.com;
        accounting_id $http_host;
        location / {
            accounting_id accounting_id_str;
        }
        location /api {
            accounting_id API_PC;
            if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
                accounting_id API_MOBILE;
            }
        }
    }
}

Ensure the accounting module is enabled; otherwise, the accounting_id directive will not function as expected.

Avoid using complex logic in the accounting_id variable assignments, as it may lead to performance issues when processing requests.

Be cautious about the level of detail in metrics; too many unique accounting_ids can complicate data analysis.

← Back to all directives