Welcome! Log In Create A New Profile

Advanced

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

June 20, 2022 02:57PM
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;
}

---------------------------------------------------------------------------------------------
Subject Author Posted

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

alireza June 20, 2022 02:57PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 312
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready