[PATCH] Adds $orig_remote_addr in realip module

Jon Nalley code at bluebot.org
Thu Jun 25 19:51:15 UTC 2015


# HG changeset patch
# User Jon Nalley <code at bluebot.org>
# Date 1435261685 18000
#      Thu Jun 25 14:48:05 2015 -0500
# Node ID 894a268769bf9b3c806a9506ea1e8daf2dac3841
# Parent  6345822f0abb70807f635989b6c2df7852a55bd9
Adds $orig_remote_addr in realip module

When the realip module sets $remote_addr, the connecting IP is
no longer available for logging etc.  This change preserves the
connecting IP as $orig_remote_addr.

diff -r 6345822f0abb -r 894a268769bf src/core/ngx_connection.h
--- a/src/core/ngx_connection.h	Thu Jun 25 12:36:52 2015 +0300
+++ b/src/core/ngx_connection.h	Thu Jun 25 14:48:05 2015 -0500
@@ -145,6 +145,10 @@
     socklen_t           socklen;
     ngx_str_t           addr_text;
 
+#if (NGX_HTTP_REALIP)
+    ngx_str_t           orig_addr_text;
+#endif
+
     ngx_str_t           proxy_protocol_addr;
 
 #if (NGX_SSL)
diff -r 6345822f0abb -r 894a268769bf src/http/modules/ngx_http_realip_module.c
--- a/src/http/modules/ngx_http_realip_module.c	Thu Jun 25 12:36:52 2015 +0300
+++ b/src/http/modules/ngx_http_realip_module.c	Thu Jun 25 14:48:05 2015 -0500
@@ -29,10 +29,13 @@
     ngx_connection_t  *connection;
     struct sockaddr   *sockaddr;
     socklen_t          socklen;
-    ngx_str_t          addr_text;
 } ngx_http_realip_ctx_t;
 
 
+static ngx_int_t
+    ngx_http_realip_orig_remote_addr_variable(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_realip_add_variables(ngx_conf_t *cf);
 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r);
 static ngx_int_t ngx_http_realip_set_addr(ngx_http_request_t *r,
     ngx_addr_t *addr);
@@ -75,7 +78,7 @@
 
 
 static ngx_http_module_t  ngx_http_realip_module_ctx = {
-    NULL,                                  /* preconfiguration */
+    ngx_http_realip_add_variables,         /* preconfiguration */
     ngx_http_realip_init,                  /* postconfiguration */
 
     NULL,                                  /* create main configuration */
@@ -105,6 +108,15 @@
 };
 
 
+static ngx_http_variable_t  ngx_http_realip_vars[] = {
+
+    { ngx_string("orig_remote_addr"), NULL,
+      ngx_http_realip_orig_remote_addr_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
+
+    { ngx_null_string, NULL, NULL, 0, 0, 0 }
+};
+
+
 static ngx_int_t
 ngx_http_realip_handler(ngx_http_request_t *r)
 {
@@ -261,7 +273,7 @@
     ctx->connection = c;
     ctx->sockaddr = c->sockaddr;
     ctx->socklen = c->socklen;
-    ctx->addr_text = c->addr_text;
+    c->orig_addr_text = c->addr_text;
 
     c->sockaddr = addr->sockaddr;
     c->socklen = addr->socklen;
@@ -283,7 +295,7 @@
 
     c->sockaddr = ctx->sockaddr;
     c->socklen = ctx->socklen;
-    c->addr_text = ctx->addr_text;
+    c->addr_text = c->orig_addr_text;
 }
 
 
@@ -369,6 +381,43 @@
 }
 
 
+static ngx_int_t
+ngx_http_realip_orig_remote_addr_variable(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data)
+{
+    ngx_connection_t    *c;
+
+    c = r->connection;
+
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+    v->data = c->orig_addr_text.data;
+    v->len = c->orig_addr_text.len;
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_realip_add_variables(ngx_conf_t *cf)
+{
+    ngx_http_variable_t  *var, *v;
+
+    for (v = ngx_http_realip_vars; v->name.len; v++) {
+        var = ngx_http_add_variable(cf, &v->name, v->flags);
+        if (var == NULL) {
+            return NGX_ERROR;
+        }
+
+        var->get_handler = v->get_handler;
+        var->data = v->data;
+    }
+
+    return NGX_OK;
+}
+
+
 static void *
 ngx_http_realip_create_loc_conf(ngx_conf_t *cf)
 {



More information about the nginx-devel mailing list