[nginx] Upstream: fixed "no port" detection in evaluated upstreams.

Ruslan Ermilov ru at nginx.com
Sat Nov 21 07:45:07 UTC 2015


details:   http://hg.nginx.org/nginx/rev/a93345ee8f52
branches:  
changeset: 6303:a93345ee8f52
user:      Ruslan Ermilov <ru at nginx.com>
date:      Sat Nov 21 10:44:07 2015 +0300
description:
Upstream: fixed "no port" detection in evaluated upstreams.

If an upstream with variables evaluated to address without a port,
then instead of a "no port in upstream" error an attempt was made
to connect() which failed with EADDRNOTAVAIL.

diffstat:

 src/http/modules/ngx_http_fastcgi_module.c |   5 +++--
 src/http/modules/ngx_http_proxy_module.c   |   5 +++--
 src/http/modules/ngx_http_scgi_module.c    |   5 +++--
 src/http/modules/ngx_http_uwsgi_module.c   |   5 +++--
 src/http/ngx_http_upstream.c               |  12 ++++++++++--
 5 files changed, 22 insertions(+), 10 deletions(-)

diffs (99 lines):

diff -r bec5b3093337 -r a93345ee8f52 src/http/modules/ngx_http_fastcgi_module.c
--- a/src/http/modules/ngx_http_fastcgi_module.c	Tue Nov 17 19:41:39 2015 +0300
+++ b/src/http/modules/ngx_http_fastcgi_module.c	Sat Nov 21 10:44:07 2015 +0300
@@ -773,10 +773,11 @@ ngx_http_fastcgi_eval(ngx_http_request_t
 
     } else {
         u->resolved->host = url.host;
-        u->resolved->port = url.port;
-        u->resolved->no_port = url.no_port;
     }
 
+    u->resolved->port = url.port;
+    u->resolved->no_port = url.no_port;
+
     return NGX_OK;
 }
 
diff -r bec5b3093337 -r a93345ee8f52 src/http/modules/ngx_http_proxy_module.c
--- a/src/http/modules/ngx_http_proxy_module.c	Tue Nov 17 19:41:39 2015 +0300
+++ b/src/http/modules/ngx_http_proxy_module.c	Sat Nov 21 10:44:07 2015 +0300
@@ -1015,10 +1015,11 @@ ngx_http_proxy_eval(ngx_http_request_t *
 
     } else {
         u->resolved->host = url.host;
-        u->resolved->port = (in_port_t) (url.no_port ? port : url.port);
-        u->resolved->no_port = url.no_port;
     }
 
+    u->resolved->port = (in_port_t) (url.no_port ? port : url.port);
+    u->resolved->no_port = url.no_port;
+
     return NGX_OK;
 }
 
diff -r bec5b3093337 -r a93345ee8f52 src/http/modules/ngx_http_scgi_module.c
--- a/src/http/modules/ngx_http_scgi_module.c	Tue Nov 17 19:41:39 2015 +0300
+++ b/src/http/modules/ngx_http_scgi_module.c	Sat Nov 21 10:44:07 2015 +0300
@@ -569,10 +569,11 @@ ngx_http_scgi_eval(ngx_http_request_t *r
 
     } else {
         u->resolved->host = url.host;
-        u->resolved->port = url.port;
-        u->resolved->no_port = url.no_port;
     }
 
+    u->resolved->port = url.port;
+    u->resolved->no_port = url.no_port;
+
     return NGX_OK;
 }
 
diff -r bec5b3093337 -r a93345ee8f52 src/http/modules/ngx_http_uwsgi_module.c
--- a/src/http/modules/ngx_http_uwsgi_module.c	Tue Nov 17 19:41:39 2015 +0300
+++ b/src/http/modules/ngx_http_uwsgi_module.c	Sat Nov 21 10:44:07 2015 +0300
@@ -771,10 +771,11 @@ ngx_http_uwsgi_eval(ngx_http_request_t *
 
     } else {
         u->resolved->host = url.host;
-        u->resolved->port = url.port;
-        u->resolved->no_port = url.no_port;
     }
 
+    u->resolved->port = url.port;
+    u->resolved->no_port = url.no_port;
+
     return NGX_OK;
 }
 
diff -r bec5b3093337 -r a93345ee8f52 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Tue Nov 17 19:41:39 2015 +0300
+++ b/src/http/ngx_http_upstream.c	Sat Nov 21 10:44:07 2015 +0300
@@ -633,8 +633,18 @@ ngx_http_upstream_init_request(ngx_http_
         u->ssl_name = u->resolved->host;
 #endif
 
+        host = &u->resolved->host;
+
         if (u->resolved->sockaddr) {
 
+            if (u->resolved->port == 0) {
+                ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                              "no port in upstream \"%V\"", host);
+                ngx_http_upstream_finalize_request(r, u,
+                                                   NGX_HTTP_INTERNAL_SERVER_ERROR);
+                return;
+            }
+
             if (ngx_http_upstream_create_round_robin_peer(r, u->resolved)
                 != NGX_OK)
             {
@@ -648,8 +658,6 @@ ngx_http_upstream_init_request(ngx_http_
             return;
         }
 
-        host = &u->resolved->host;
-
         umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
 
         uscfp = umcf->upstreams.elts;



More information about the nginx-devel mailing list