rewritten upstream
diff --git a/src/http/modules/ngx_http_fastcgi_module.c b/src/http/modules/ngx_http_fastcgi_module.c
index fdefb83..ab21477 100644
--- a/src/http/modules/ngx_http_fastcgi_module.c
+++ b/src/http/modules/ngx_http_fastcgi_module.c
@@ -2010,7 +2010,6 @@
     ngx_memzero(&u, sizeof(ngx_url_t));
 
     u.url = value[1];
-    u.upstream = 1;
     u.no_resolve = 1;
 
     lcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
diff --git a/src/http/modules/ngx_http_memcached_module.c b/src/http/modules/ngx_http_memcached_module.c
index 5265c38..bd8b837 100644
--- a/src/http/modules/ngx_http_memcached_module.c
+++ b/src/http/modules/ngx_http_memcached_module.c
@@ -583,7 +583,6 @@
     ngx_memzero(&u, sizeof(ngx_url_t));
 
     u.url = value[1];
-    u.upstream = 1;
     u.no_resolve = 1;
     /* u.uri_part = 1;  may be used as namespace */
 
diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c
index dbca2ab..2c26df9 100644
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -51,7 +51,7 @@
 
     ngx_str_t                      method;
     ngx_str_t                      host_header;
-    ngx_str_t                      port_text;
+    ngx_str_t                      port;
 
     ngx_flag_t                     redirect;
 } ngx_http_proxy_loc_conf_t;
@@ -1232,11 +1232,11 @@
 
     plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
 
-    v->len = plcf->port_text.len;
+    v->len = plcf->port.len;
     v->valid = 1;
     v->no_cachable = 0;
     v->not_found = 0;
-    v->data = plcf->port_text.data;
+    v->data = plcf->port.data;
 
     return NGX_OK;
 }
@@ -1817,7 +1817,7 @@
         conf->upstream.upstream = prev->upstream.upstream;
 
         conf->host_header = prev->host_header;
-        conf->port_text = prev->port_text;
+        conf->port = prev->port;
         conf->upstream.schema = prev->upstream.schema;
     }
 
@@ -2093,6 +2093,7 @@
 {
     ngx_http_proxy_loc_conf_t *plcf = conf;
 
+    u_char                      *p;
     size_t                       add;
     u_short                      port;
     ngx_str_t                   *value, *url;
@@ -2158,18 +2159,49 @@
 
     u.url.len = url->len - add;
     u.url.data = url->data + add;
-    u.default_portn = port;
-    u.upstream = 1;
-    u.no_resolve = 1;
+    u.default_port = port;
     u.uri_part = 1;
+    u.no_resolve = 1;
 
     plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
     if (plcf->upstream.upstream == NULL) {
         return NGX_CONF_ERROR;
     }
 
-    plcf->host_header = u.host_header;
-    plcf->port_text = u.port;
+    if (!u.unix_socket) {
+        if (u.no_port || u.port == port) {
+            plcf->host_header = u.host;
+
+            if (port == 80) {
+                plcf->port.len = sizeof("80") - 1;
+                plcf->port.data = (u_char *) "80";
+            } else {
+                plcf->port.len = sizeof("443") - 1;
+                plcf->port.data = (u_char *) "443";
+            }
+
+        } else {
+            p = ngx_palloc(cf->pool, u.host.len + sizeof(":65536") - 1);
+            if (p == NULL) {
+                return NGX_CONF_ERROR;
+            }
+
+            plcf->host_header.len = ngx_sprintf(p, "%V:%d", &u.host, u.port)
+                                        - p;
+            plcf->host_header.data = p;
+
+            plcf->port.len = plcf->host_header.len -  u.host.len - 1;
+            plcf->port.data = p + u.host.len + 1;
+        }
+
+
+    } else {
+        plcf->host_header.len = sizeof("localhost") - 1;
+        plcf->host_header.data = (u_char *) "localhost";
+        plcf->port.len = 0;
+        plcf->port.data = (u_char *) "";
+    }
+
     plcf->upstream.uri = u.uri;
 
     plcf->upstream.schema.len = add;
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 7f12dae..34149c4 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -2390,7 +2390,7 @@
 
     u.url = value[1];
     u.listen = 1;
-    u.default_portn = 80;
+    u.default_port = 80;
 
     if (ngx_parse_url(cf, &u) != NGX_OK) {
         if (u.err) {
@@ -2411,7 +2411,7 @@
 
     ls->family = AF_INET;
     ls->addr = u.addr.in_addr;
-    ls->port = u.portn;
+    ls->port = u.port;
     ls->file_name = cf->conf_file->file.name;
     ls->line = cf->conf_file->line;
     ls->conf.backlog = -1;
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
index a917dcf..00fa457 100644
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -2598,7 +2598,6 @@
 
     value = cf->args->elts;
     u.host = value[1];
-    u.upstream = 1;
     u.no_resolve = 1;
 
     uscf = ngx_http_upstream_add(cf, &u, NGX_HTTP_UPSTREAM_CREATE
@@ -2722,7 +2721,7 @@
     ngx_memzero(&u, sizeof(ngx_url_t));
 
     u.url = value[1];
-    u.default_portn = 80;
+    u.default_port = 80;
 
     if (ngx_parse_url(cf, &u) != NGX_OK) {
         if (u.err) {
@@ -2843,8 +2842,8 @@
     uscfp = umcf->upstreams.elts;
 
     for (i = 0; i < umcf->upstreams.nelts; i++) {
-        if ((uscfp[i]->port && uscfp[i]->port != u->portn)
-            || uscfp[i]->host.len != u->host.len
+
+        if (uscfp[i]->host.len != u->host.len
             || ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len)
                != 0)
         {
@@ -2859,10 +2858,23 @@
             return NULL;
         }
 
-        if (uscfp[i]->port == 0 && u->portn && !u->no_port) {
+        if ((uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE) && u->port) {
             ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
-                               "upstream \"%V\" port %d is ignored",
-                               &u->host, u->portn);
+                               "upstream \"%V\" may not have port %d",
+                               &u->host, u->port);
+            return NULL;
+        }
+
+        if ((flags & NGX_HTTP_UPSTREAM_CREATE) && uscfp[i]->port) {
+            ngx_log_error(NGX_LOG_WARN, cf->log, 0,
+                          "upstream \"%V\" may not have port %d in %s:%ui",
+                          &u->host, uscfp[i]->port,
+                          uscfp[i]->file_name.data, uscfp[i]->line);
+            return NULL;
+        }
+
+        if (uscfp[i]->port != u->port) {
+            continue;
         }
 
         return uscfp[i];
@@ -2877,7 +2889,8 @@
     uscf->host = u->host;
     uscf->file_name = cf->conf_file->file.name;
     uscf->line = cf->conf_file->line;
-    uscf->port = u->portn;
+    uscf->port = u->port;
+    uscf->default_port = u->default_port;
 
     if (u->naddrs == 1) {
         uscf->servers = ngx_array_create(cf->pool, 1,
diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h
index 185964e..aa5cc35 100644
--- a/src/http/ngx_http_upstream.h
+++ b/src/http/ngx_http_upstream.h
@@ -94,6 +94,7 @@
     ngx_str_t                       file_name;
     ngx_uint_t                      line;
     in_port_t                       port;
+    in_port_t                       default_port;
 };
 
 
diff --git a/src/http/ngx_http_upstream_round_robin.c b/src/http/ngx_http_upstream_round_robin.c
index 82173e4..89937ef 100644
--- a/src/http/ngx_http_upstream_round_robin.c
+++ b/src/http/ngx_http_upstream_round_robin.c
@@ -61,10 +61,17 @@
 
     /* an upstream implicitly defined by proxy_pass, etc. */
 
+    if (us->port == 0 && us->default_port == 0) {
+        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+                      "no port in upstream \"%V\" in %s:%ui",
+                      &us->host, us->file_name.data, us->line);
+        return NGX_ERROR;
+    }
+
     ngx_memzero(&u, sizeof(ngx_url_t));
 
     u.host = us->host;
-    u.portn = us->port;
+    u.port = us->port ? us->port : us->default_port;
 
     if (ngx_inet_resolve_host(cf, &u) != NGX_OK) {
         if (u.err) {
@@ -76,13 +83,6 @@
         return NGX_ERROR;
     }
 
-    if (us->port == 0) {
-        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                      "no port in upstream \"%V\" in %s:%ui",
-                      &us->host, us->file_name.data, us->line);
-        return NGX_ERROR;
-    }
-
     n = u.naddrs;
 
     peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t)