[PATCH] Stream: added extra variables for logging

Balazs Hinel gollam6 at gmail.com
Thu Jul 9 14:20:16 UTC 2020


# HG changeset patch
# User Balazs Hinel <balazs.hinel at nokia.com>
# Date 1594225255 -7200
#      Wed Jul 08 18:20:55 2020 +0200
# Node ID ab1359d9762109d1cccb06fbe85e17168b51ebed
# Parent  2ab0ecfe5a5f02f7214b02c8a1f9d6c5843d310e
Stream: added extra variables for logging.

As a result, the local and remote connection details (address and port)
used in the upstream connection can be logged.

diff -r 2ab0ecfe5a5f -r ab1359d97621 src/stream/ngx_stream.h
--- a/src/stream/ngx_stream.h	Tue Jul 07 18:56:06 2020 +0300
+++ b/src/stream/ngx_stream.h	Wed Jul 08 18:20:55 2020 +0200
@@ -192,6 +192,14 @@
 } ngx_stream_core_srv_conf_t;
 
 
+typedef struct {
+    ngx_str_t                      upstream_remote_addr;
+    in_port_t                      upstream_remote_port;
+    ngx_str_t                      upstream_local_addr;
+    in_port_t                      upstream_local_port;
+} ngx_stream_upstream_addr_t;
+
+
 struct ngx_stream_session_s {
     uint32_t                       signature;         /* "STRM" */
 
@@ -212,6 +220,8 @@
                                            /* of ngx_stream_upstream_state_t */
     ngx_stream_variable_value_t   *variables;
 
+    ngx_stream_upstream_addr_t     upstream_addrs;
+
 #if (NGX_PCRE)
     ngx_uint_t                     ncaptures;
     int                           *captures;
diff -r 2ab0ecfe5a5f -r ab1359d97621 src/stream/ngx_stream_proxy_module.c
--- a/src/stream/ngx_stream_proxy_module.c	Tue Jul 07 18:56:06 2020 +0300
+++ b/src/stream/ngx_stream_proxy_module.c	Wed Jul 08 18:20:55 2020 +0200
@@ -761,6 +761,8 @@
     ngx_stream_upstream_t        *u;
     ngx_stream_core_srv_conf_t   *cscf;
     ngx_stream_proxy_srv_conf_t  *pscf;
+    ngx_str_t                     local_sockaddr_str;
+    u_char                        addr[NGX_SOCKADDR_STRLEN];
 
     u = s->upstream;
     pc = u->peer.connection;
@@ -799,21 +801,42 @@
 
     c = s->connection;
 
-    if (c->log->log_level >= NGX_LOG_INFO) {
-        ngx_str_t  str;
-        u_char     addr[NGX_SOCKADDR_STRLEN];
-
-        str.len = NGX_SOCKADDR_STRLEN;
-        str.data = addr;
-
-        if (ngx_connection_local_sockaddr(pc, &str, 1) == NGX_OK) {
+    local_sockaddr_str.len = NGX_SOCKADDR_STRLEN;
+    local_sockaddr_str.data = addr;
+
+    if (ngx_connection_local_sockaddr(pc, &local_sockaddr_str, 1) == NGX_OK) {
+        s->upstream_addrs.upstream_remote_addr.data =
+                                              ngx_pnalloc(s->connection->pool,
+                                                          NGX_SOCKADDR_STRLEN);
+        s->upstream_addrs.upstream_remote_addr.len =
+                     ngx_sock_ntop(u->peer.sockaddr,
+                                   u->peer.socklen,
+                                   s->upstream_addrs.upstream_remote_addr.data,
+                                   NGX_SOCKADDR_STRLEN,
+                                   0);
+        s->upstream_addrs.upstream_remote_port =
+                                           ngx_inet_get_port(u->peer.sockaddr);
+
+        s->upstream_addrs.upstream_local_addr.data =
+                                              ngx_pnalloc(s->connection->pool,
+                                                          NGX_SOCKADDR_STRLEN);
+        s->upstream_addrs.upstream_local_addr.len =
+                      ngx_sock_ntop(pc->local_sockaddr,
+                                    pc->local_socklen,
+                                    s->upstream_addrs.upstream_local_addr.data,
+                                    NGX_SOCKADDR_STRLEN,
+                                    0);
+        s->upstream_addrs.upstream_local_port =
+                                         ngx_inet_get_port(pc->local_sockaddr);
+
+        if (c->log->log_level >= NGX_LOG_INFO) {
             handler = c->log->handler;
             c->log->handler = NULL;
 
             ngx_log_error(NGX_LOG_INFO, c->log, 0,
                           "%sproxy %V connected to %V",
                           pc->type == SOCK_DGRAM ? "udp " : "",
-                          &str, u->peer.name);
+                          &local_sockaddr_str, u->peer.name);
 
             c->log->handler = handler;
         }
diff -r 2ab0ecfe5a5f -r ab1359d97621 src/stream/ngx_stream_variables.c
--- a/src/stream/ngx_stream_variables.c	Tue Jul 07 18:56:06 2020 +0300
+++ b/src/stream/ngx_stream_variables.c	Wed Jul 08 18:20:55 2020 +0200
@@ -19,6 +19,14 @@
     ngx_stream_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_stream_variable_remote_port(ngx_stream_session_t *s,
     ngx_stream_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_stream_variable_upstream_remote_addr(
+    ngx_stream_session_t *s, ngx_stream_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_stream_variable_upstream_remote_port(
+    ngx_stream_session_t *s, ngx_stream_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_stream_variable_upstream_local_addr(
+    ngx_stream_session_t *s, ngx_stream_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_stream_variable_upstream_local_port(
+    ngx_stream_session_t *s, ngx_stream_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_stream_variable_proxy_protocol_addr(
     ngx_stream_session_t *s, ngx_stream_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_stream_variable_proxy_protocol_port(
@@ -63,6 +71,18 @@
     { ngx_string("remote_port"), NULL,
       ngx_stream_variable_remote_port, 0, 0, 0 },
 
+    { ngx_string("upstream_remote_addr"), NULL,
+      ngx_stream_variable_upstream_remote_addr, 0, 0, 0 },
+
+    { ngx_string("upstream_remote_port"), NULL,
+      ngx_stream_variable_upstream_remote_port, 0, 0, 0 },
+
+    { ngx_string("upstream_local_addr"), NULL,
+      ngx_stream_variable_upstream_local_addr, 0, 0, 0 },
+
+    { ngx_string("upstream_local_port"), NULL,
+      ngx_stream_variable_upstream_local_port, 0, 0, 0 },
+
     { ngx_string("proxy_protocol_addr"), NULL,
       ngx_stream_variable_proxy_protocol_addr,
       offsetof(ngx_proxy_protocol_t, src_addr), 0, 0 },
@@ -562,6 +582,85 @@
     return NGX_OK;
 }
 
+static ngx_int_t
+ngx_stream_variable_upstream_remote_addr(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
+{
+    v->len = s->upstream_addrs.upstream_remote_addr.len;
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+    v->data = s->upstream_addrs.upstream_remote_addr.data;
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_stream_variable_upstream_remote_port(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
+{
+    ngx_uint_t  port;
+
+    v->len = 0;
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+
+    v->data = ngx_pnalloc(s->connection->pool, sizeof("65535") - 1);
+    if (v->data == NULL) {
+        return NGX_ERROR;
+    }
+
+    port = s->upstream_addrs.upstream_remote_port;
+
+    if (port > 0 && port < 65536) {
+        v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
+    }
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_stream_variable_upstream_local_addr(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
+{
+    v->len = s->upstream_addrs.upstream_local_addr.len;
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+    v->data = s->upstream_addrs.upstream_local_addr.data;
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_stream_variable_upstream_local_port(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
+{
+    ngx_uint_t  port;
+
+    v->len = 0;
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+
+    v->data = ngx_pnalloc(s->connection->pool, sizeof("65535") - 1);
+    if (v->data == NULL) {
+        return NGX_ERROR;
+    }
+
+    port = s->upstream_addrs.upstream_local_port;
+
+    if (port > 0 && port < 65536) {
+        v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
+    }
+
+    return NGX_OK;
+}
+
 
 static ngx_int_t
 ngx_stream_variable_proxy_protocol_addr(ngx_stream_session_t *s,


More information about the nginx-devel mailing list