accounting

The accounting directive enables real-time monitoring of incoming and outgoing traffic metrics in NGINX's stream context.

Syntaxaccounting on | off;
Defaultoff
Contextstream
Arguments1

Description

The 'accounting' directive in NGINX provides the ability to monitor and log incoming and outgoing traffic metrics for stream connections, offering insights into resource usage and traffic patterns. By enabling this directive, users gain access to detailed traffic statistics that can be aggregated and logged for analysis. It requires the specification of one argument to turn the accounting feature on or off, thereby allowing users to customize their monitoring setup based on specific needs or resource constraints.

This directive operates within the stream context, which is designed for handling TCP/UDP traffic rather than HTTP. When 'accounting' is set to 'on', NGINX begins tracking the traffic metrics associated with the specified streams. It maintains records of requests and responses while also tracking metrics identified by a unique 'accounting_id'. The results of this data collection are periodically exported to configured log files or monitoring systems, depending on the logging setup in conjunction with the 'accounting_log' directive.

In addition to the 'accounting' directive, users must also utilize supporting directives like 'accounting_log' to specify where to store the logs and 'accounting_id' to define how resources should be segregated based on traffic identifiers. This enhances the ability to perform granular traffic analysis across different services and user segments.

Config Example

stream {
    accounting on;
    accounting_log logs/stream-accounting.log;

    server {
        listen 12345;
        accounting_id $remote_addr;  # Track traffic by remote client IP address
    }
}

Ensure the 'accounting' directive is only used within the stream context; attempting to use it within other contexts will result in an error.

Make sure to define the 'accounting_log' directive to capture the logged metrics; otherwise, no data will be recorded.

Carefully choose the 'accounting_id' format to avoid collisions between different streams.

← Back to all directives