[PATCH] Adds $orig_remote_addr in realip module

Jon Nalley code at bluebot.org
Sat Jun 6 03:18:52 UTC 2015


# HG changeset patch
# User Jon Nalley <code at bluebot.org>
# Date 1433560679 18000
#      Fri Jun 05 22:17:59 2015 -0500
# Node ID b7c07ccca1f9be7038379cfd1077fb5885b3f388
# Parent  62869a9b2e7d1b2b698cb1ca67ad664b37171113
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 62869a9b2e7d -r b7c07ccca1f9 src/http/modules/ngx_http_realip_module.c
--- a/src/http/modules/ngx_http_realip_module.c	Fri Jun 05 22:23:26 2015 +0300
+++ b/src/http/modules/ngx_http_realip_module.c	Fri Jun 05 22:17:59 2015 -0500
@@ -33,6 +33,10 @@
 } 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 +79,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 +109,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)
 {
@@ -369,6 +382,55 @@
 }
 
 
+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_http_realip_ctx_t *ctx;
+
+    ctx = ngx_http_get_module_ctx(r, ngx_http_realip_module);
+
+    if (ctx == NULL) {
+        v->not_found = 1;
+        return NGX_OK;
+    }
+
+    v->len = ctx->addr_text.len;
+    v->data = ngx_pnalloc(r->pool, v->len);
+
+    if (v->data == NULL) {
+        return NGX_ERROR;
+    }
+
+    ngx_memcpy(v->data, ctx->addr_text.data, v->len);
+
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+
+    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