可以使用openresty的log_by_lua指令来记录access_log的字节数。

首先在nginx.conf中添加以下代码:

http {
    log_format bytes_log '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent';
    access_log /path/to/access.log bytes_log;

    ...
}

然后在location或server中使用log_by_lua指令,将body_bytes_sent变量赋值给一个全局变量:

location / {
    set $bytes_sent '-';
    log_by_lua '
        ngx.var.bytes_sent = ngx.var.body_bytes_sent;
    ';

    ...
}

最后就可以在access.log中看到每个请求的字节数了:

127.0.0.1 - - [07/Jul/2021:09:51:10 +0800] "GET / HTTP/1.1" 200 1234
``
怎么用openresty 记录access_log的字节数

原文地址: https://www.cveoy.top/t/topic/fJQJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录