[nginx] Stream: support for $remote_port in proxy_bind.

Roman Arutyunyan arut at nginx.com
Mon Jun 20 09:50:09 UTC 2016


details:   http://hg.nginx.org/nginx/rev/0c98c4092440
branches:  
changeset: 6595:0c98c4092440
user:      Roman Arutyunyan <arut at nginx.com>
date:      Mon Jun 20 11:50:44 2016 +0300
description:
Stream: support for $remote_port in proxy_bind.

The following two types of bind addresses are supported in addition to
$remote_addr and address literals:

- $remote_addr:$remote_port
- [$remote_addr]:$remote_port

In both cases client remote address with port is used in upstream socket bind.

diffstat:

 src/stream/ngx_stream_proxy_module.c |  29 ++++++++++++++++++++---------
 1 files changed, 20 insertions(+), 9 deletions(-)

diffs (63 lines):

diff -r 3c87b82b17d4 -r 0c98c4092440 src/stream/ngx_stream_proxy_module.c
--- a/src/stream/ngx_stream_proxy_module.c	Mon Jun 20 11:50:43 2016 +0300
+++ b/src/stream/ngx_stream_proxy_module.c	Mon Jun 20 11:50:44 2016 +0300
@@ -13,8 +13,9 @@
 typedef struct {
     ngx_addr_t                      *addr;
 #if (NGX_HAVE_TRANSPARENT_PROXY)
-    ngx_uint_t                       transparent; /* unsigned  transparent:1; */
+    unsigned                         transparent:1;
 #endif
+    unsigned                         no_port:1;
 } ngx_stream_upstream_local_t;
 
 
@@ -463,7 +464,7 @@ ngx_stream_proxy_set_local(ngx_stream_se
         return NGX_OK;
     }
 
-    /* $remote_addr */
+    /* $remote_addr, $remote_addr:$remote_port, [$remote_addr]:$remote_port */
 
     c = s->connection;
 
@@ -474,14 +475,19 @@ ngx_stream_proxy_set_local(ngx_stream_se
 
     addr->socklen = c->socklen;
 
-    addr->sockaddr = ngx_palloc(c->pool, addr->socklen);
-    if (addr->sockaddr == NULL) {
-        return NGX_ERROR;
+    if (local->no_port) {
+        addr->sockaddr = ngx_palloc(c->pool, addr->socklen);
+        if (addr->sockaddr == NULL) {
+            return NGX_ERROR;
+        }
+
+        ngx_memcpy(addr->sockaddr, c->sockaddr, c->socklen);
+        ngx_inet_set_port(addr->sockaddr, 0);
+
+    } else {
+        addr->sockaddr = c->sockaddr;
     }
 
-    ngx_memcpy(addr->sockaddr, c->sockaddr, c->socklen);
-    ngx_inet_set_port(addr->sockaddr, 0);
-
     addr->name = c->addr_text;
     u->peer.local = addr;
 
@@ -1714,7 +1720,12 @@ ngx_stream_proxy_bind(ngx_conf_t *cf, ng
 
     pscf->local = local;
 
-    if (ngx_strcmp(value[1].data, "$remote_addr") != 0) {
+    if (ngx_strcmp(value[1].data, "$remote_addr") == 0) {
+        local->no_port = 1;
+
+    } else if (ngx_strcmp(value[1].data, "$remote_addr:$remote_port") != 0
+               && ngx_strcmp(value[1].data, "[$remote_addr]:$remote_port") != 0)
+    {
         local->addr = ngx_palloc(cf->pool, sizeof(ngx_addr_t));
         if (local->addr == NULL) {
             return NGX_CONF_ERROR;



More information about the nginx-devel mailing list