access to "sent_bytes_length" and send it to API when the user cancel downloading

alireza nginx-forum at forum.nginx.org
Mon Jun 20 18:57:11 UTC 2022


Hello my friends

I want to access "sent_bytes_length" of file which has been downloaded
incompletely by user and send it to http://locahost:8080

My API is written by nodejs

I tried ngx.fetch and r.subrequest to handle it inside "custom access log
function" by NJS but it does not allow me to use Promise function

Can you help me, please?

Thank you



nginx.config
---------------------------------------------------------------------------

js_import conf.d/download_log.js;
js_set $json_download_log download_log.downloadLog;
log_format download escape=none $json_download_log;
map $request_uri $loggable {
  ~/download/(.*) 1;
  default 0;
}
server {
.
.
.
 location /downloadlog {
   proxy_pass http://localhost:10000;
 }

 location / {
  access_log /var/log/nginx/download.log download if=$loggable;
  proxy_pass http://localhost:10000;
 }

}

download_log.js
--------------------------------------------------------------------

export default { downloadLog };

function downloadLog(r) {
    var connection = {
        "serial": Number(r.variables.connection),
        "request_count": Number(r.variables.connection_requests),
        "elapsed_time": Number(r.variables.request_time)
    }
    if (r.variables.pipe == "p") {
        connection.pipelined = true;
    } else {
        connection.pipelined = false;
    }
    if ( r.variables.ssl_protocol !== undefined ) {
        connection.ssl = sslInfo(r);
    }

    var request = {
        "client": r.variables.remote_addr,
        "port": Number(r.variables.server_port),
        "host": r.variables.host,
        "method": r.method,
        "uri": r.uri,
        "http_version": Number(r.httpVersion),
        "bytes_received": Number(r.variables.request_length)
    };
    request.headers = {};
    for (var h in r.headersIn) {
        request.headers[h] = r.headersIn[h];
    }

    var upstreams = [];
    if ( r.variables.upstream_status !== undefined ) {
        upstreams = upstreamArray(r);
    }

    var response = {
        "status": Number(r.variables.status),
        "bytes_sent": Number(r.variables.bytes_sent),
    }
    response.headers = {};
    for (var h in r.headersOut) {
        response.headers[h] = r.headersOut[h];
    }


    let LOG = JSON.stringify({
        "timestamp": r.variables.time_iso8601,
        "connection": connection,
        "request": request,
        "upstreams": upstreams,
        "response": response
    });

/******************************************************/
 
    I want to send this LOG to http://localhost:8080

/******************************************************/

    return LOG;
}

---------------------------------------------------------------------------------------------

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,294526,294526#msg-294526



More information about the nginx mailing list