Traffic Accounting

traffic-accounting-nginx-module is a cost-effective solution to monitor the traffic metrics/status of application requests.

How it works?

The module keeps in its context a list of metrics identified by accounting_id.

When a new request hits the server, the module will try to find its accounting_id, calculate statistics, and aggregate them into the corresponding metrics by accounting_id.

For every period (defined by interval), a timer event is triggered, those metrics are rotated and exported to log files or sent to remote log servers.


Starting from nginx 1.9.11, it’s possible to compile this module as a dynamic module, by using the --add-dynamic-module=PATH option instead of --add-module=PATH on the ./configure command line. Then it’s possible to explicitly load the module in a nginx.conf via the load_module ( directive:

load_module modules/; load_module modules/;

Example configuration

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

    server {

        accounting_id  $http_host;  # set accounting_id string by variable

        location / {

            accounting_id  "accounting_id_str";  # set accounting_id string by location

        location /api {
            accounting_id  "API_PC";   # for pc

            if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
                accounting_id  "API_MOBILE";   # for mobile




Syntax:accounting <on|off>
Context:http, stream


Syntax:accounting_log </path/to/accounting_log> [level]
Context:http, stream

Support same kind of log target as error_log: local file, syslog, stderr and memory. Refer to error_log <> synopsis.


Syntax:accounting_interval <seconds>
Context:http, stream

Specifies the reporting interval. Defaults to 60 seconds.


Syntax:accounting_interval <on|off>
Context:http, stream

Randomly staggers the reporting interval by 20% from the usual time.


Syntax:accounting_id <accounting_id>
Context:http, stream, server, location, if in location

Sets the accounting_id string by user defined variable. This string is used to determine which metrics a request/session should be aggregated to.

See Also

The step by step setup can be found here

Please visit github for more details.