[PATCH] Adds $orig_remote_addr in realip module

Jon Nalley code at bluebot.org
Thu May 28 21:39:08 UTC 2015


# HG changeset patch
# User Jon Nalley <code at bluebot.org>
# Date 1432848566 18000
#      Thu May 28 16:29:26 2015 -0500
# Node ID 4e59c130d468ee6757b5ba97f912b6c72c3f7c0d
# Parent  0a096e2e51fcbb536007d94bf3edfc308e214f56
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 0a096e2e51fc -r 4e59c130d468 src/http/modules/ngx_http_realip_module.c
--- a/src/http/modules/ngx_http_realip_module.c	Tue May 26 16:49:51 2015 +0300
+++ b/src/http/modules/ngx_http_realip_module.c	Thu May 28 16:29:26 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,48 @@
 }
 
 
+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->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+    v->data = ctx->addr_text.data;
+
+    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