nginx-0.3.17-RELEASE import

    *) Change: now on Linux configure checks the presence of epoll and
       sendfile64() in kernel.

    *) Feature: the "map" directive supports domain names in the
       ".domain.tld" form.

    *) Bugfix: the timeouts were not used in SSL handshake; the bug had
       appeared in 0.2.4.

    *) Bugfix: in the HTTPS protocol in the "proxy_pass" directive.

    *) Bugfix: when the HTTPS protocol was used in the "proxy_pass"
       directive the port 80 was used by default.
diff --git a/src/http/modules/ngx_http_map_module.c b/src/http/modules/ngx_http_map_module.c
index 2769028..04e2e0a 100644
--- a/src/http/modules/ngx_http_map_module.c
+++ b/src/http/modules/ngx_http_map_module.c
@@ -9,8 +9,6 @@
 #include <ngx_http.h>
 
 
-#define NGX_HTTP_MAP_HASH       10007
-
 typedef struct {
     ngx_uint_t                  hash_max_size;
     ngx_uint_t                  hash_bucket_size;
@@ -18,13 +16,7 @@
 
 
 typedef struct {
-    ngx_pool_t                 *pool;
-
-    ngx_array_t                 keys;
-    ngx_array_t                *keys_hash;
-
-    ngx_array_t                 dns_wildcards;
-    ngx_array_t                *dns_hash;
+    ngx_http_hash_conf_t        hash;
 
     ngx_array_t                *values_hash;
 
@@ -253,40 +245,44 @@
         return NGX_CONF_ERROR;
     }
 
-    if (ngx_array_init(&ctx.keys, pool, 16384, sizeof(ngx_hash_key_t))
+    if (ngx_array_init(&ctx.hash.keys, pool, 16384, sizeof(ngx_hash_key_t))
         != NGX_OK)
     {
         ngx_destroy_pool(pool);
         return NGX_CONF_ERROR;
     }
 
-    if (ngx_array_init(&ctx.dns_wildcards, pool, 16384, sizeof(ngx_hash_key_t))
+    if (ngx_array_init(&ctx.hash.dns_wildcards, pool, 16384,
+                       sizeof(ngx_hash_key_t))
         != NGX_OK)
     {
         ngx_destroy_pool(pool);
         return NGX_CONF_ERROR;
     }
 
-    ctx.keys_hash = ngx_pcalloc(pool, sizeof(ngx_array_t) * NGX_HTTP_MAP_HASH);
-    if (ctx.keys_hash == NULL) {
+    ctx.hash.keys_hash = ngx_pcalloc(pool,
+                                    sizeof(ngx_array_t) * NGX_HTTP_CONFIG_HASH);
+    if (ctx.hash.keys_hash == NULL) {
         ngx_destroy_pool(pool);
         return NGX_CONF_ERROR;
     }
 
-    ctx.dns_hash = ngx_pcalloc(pool, sizeof(ngx_array_t) * NGX_HTTP_MAP_HASH);
-    if (ctx.dns_hash == NULL) {
+    ctx.hash.dns_hash = ngx_pcalloc(pool,
+                                    sizeof(ngx_array_t) * NGX_HTTP_CONFIG_HASH);
+    if (ctx.hash.dns_hash == NULL) {
         ngx_destroy_pool(pool);
         return NGX_CONF_ERROR;
     }
 
     ctx.values_hash = ngx_pcalloc(pool,
-                                  sizeof(ngx_array_t) * NGX_HTTP_MAP_HASH);
+                                  sizeof(ngx_array_t) * NGX_HTTP_CONFIG_HASH);
     if (ctx.values_hash == NULL) {
         ngx_destroy_pool(pool);
         return NGX_CONF_ERROR;
     }
 
-    ctx.pool = cf->pool;
+    ctx.hash.pool = cf->pool;
+    ctx.hash.temp_pool = pool;
     ctx.default_value = NULL;
     ctx.hostnames = 0;
 
@@ -312,11 +308,13 @@
     hash.name = "map_hash";
     hash.pool = cf->pool;
 
-    if (ctx.keys.nelts) {
+    if (ctx.hash.keys.nelts) {
         hash.hash = &map->hash;
         hash.temp_pool = NULL;
 
-        if (ngx_hash_init(&hash, ctx.keys.elts, ctx.keys.nelts) != NGX_OK) {
+        if (ngx_hash_init(&hash, ctx.hash.keys.elts, ctx.hash.keys.nelts)
+            != NGX_OK)
+        {
             return NGX_CONF_ERROR;
         }
     }
@@ -324,16 +322,17 @@
     map->default_value = ctx.default_value ? ctx.default_value:
                                              &ngx_http_variable_null_value;
 
-    if (ctx.dns_wildcards.nelts) {
+    if (ctx.hash.dns_wildcards.nelts) {
 
-        ngx_qsort(ctx.dns_wildcards.elts, (size_t) ctx.dns_wildcards.nelts,
+        ngx_qsort(ctx.hash.dns_wildcards.elts,
+                  (size_t) ctx.hash.dns_wildcards.nelts,
                   sizeof(ngx_hash_key_t), ngx_http_map_cmp_dns_wildcards);
 
         hash.hash = NULL;
         hash.temp_pool = pool;
 
-        if (ngx_hash_wildcard_init(&hash, ctx.dns_wildcards.elts,
-                                   ctx.dns_wildcards.nelts)
+        if (ngx_hash_wildcard_init(&hash, ctx.hash.dns_wildcards.elts,
+                                   ctx.hash.dns_wildcards.nelts)
             != NGX_OK)
         {
             return NGX_CONF_ERROR;
@@ -363,13 +362,12 @@
 static char *
 ngx_http_map(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
 {
-    size_t                      len;
-    ngx_str_t                  *value, file, *name;
-    ngx_uint_t                  i, n, key;
-    ngx_hash_key_t             *m;
+    u_char                      ch;
+    ngx_int_t                   rc;
+    ngx_str_t                  *value, file;
+    ngx_uint_t                  i, key, flags;
     ngx_http_map_conf_ctx_t    *ctx;
-    ngx_http_variable_value_t  *var, *old, **vp;
-    u_char                      buf[2048];
+    ngx_http_variable_value_t  *var, **vp;
 
     ctx = cf->ctx;
 
@@ -410,7 +408,7 @@
         key = ngx_hash(key, value[1].data[i]);
     }
 
-    key %= NGX_HTTP_MAP_HASH;
+    key %= NGX_HTTP_CONFIG_HASH;
 
     vp = ctx->values_hash[key].elts;
 
@@ -435,13 +433,13 @@
         }
     }
 
-    var = ngx_palloc(ctx->pool, sizeof(ngx_http_variable_value_t));
+    var = ngx_palloc(ctx->hash.pool, sizeof(ngx_http_variable_value_t));
     if (var == NULL) {
         return NGX_CONF_ERROR;
     }
 
     var->len = value[1].len;
-    var->data = ngx_pstrdup(ctx->pool, &value[1]);
+    var->data = ngx_pstrdup(ctx->hash.pool, &value[1]);
     if (var->data == NULL) {
         return NGX_CONF_ERROR;
     }
@@ -459,179 +457,54 @@
 
 found:
 
-    if (value[0].data[0] != '*' || ctx->hostnames == 0) {
+    ch = value[0].data[0];
 
-        if (ngx_strcmp(value[0].data, "default") != 0) {
+    if ((ch != '*' && ch != '.') || ctx->hostnames == 0) {
 
-            if (value[0].len && value[0].data[0] == '!') {
-                value[0].len--;
-                value[0].data++;
-            }
+        if (ngx_strcmp(value[0].data, "default") == 0) {
 
-            key = 0;
-
-            for (i = 0; i < value[0].len; i++) {
-                value[0].data[i] = ngx_tolower(value[0].data[i]);
-                key = ngx_hash(key, value[0].data[i]);
-            }
-
-            key %= NGX_HTTP_MAP_HASH;
-
-            name = ctx->keys_hash[key].elts;
-
-            if (name) {
-                for (i = 0; i < ctx->keys_hash[key].nelts; i++) {
-                    if (value[0].len != name[i].len) {
-                        continue;
-                    }
-
-                    if (ngx_strncmp(value[0].data, name[i].data, value[0].len)
-                        == 0)
-                    {
-                        m = ctx->keys.elts;
-                        for (i = 0; i < ctx->keys.nelts; i++) {
-                            if (ngx_strcmp(value[0].data, m[i].key.data) == 0) {
-                                old = m[i].value;
-                                m[i].value = var;
-
-                                goto duplicate;
-                            }
-                        }
-                    }
-                }
-
-            } else {
-                if (ngx_array_init(&ctx->keys_hash[key], cf->pool, 4,
-                                   sizeof(ngx_str_t))
-                    != NGX_OK)
-                {
-                    return NGX_CONF_ERROR;
-                }
-            }
-
-            name = ngx_array_push(&ctx->keys_hash[key]);
-            if (name == NULL) {
-                return NGX_CONF_ERROR;
-            }
-
-            *name = value[0];
-
-            m = ngx_array_push(&ctx->keys);
-            if (m == NULL) {
-                return NGX_CONF_ERROR;
-            }
-
-            m->key = value[0];
-            m->key_hash = ngx_hash_key(value[0].data, value[0].len);
-            m->value = var;
-
-        } else {
             if (ctx->default_value) {
-                old = ctx->default_value;
-                ctx->default_value = var;
-
-                goto duplicate;
+                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                                   "duplicate default map parameter");
+                return NGX_CONF_ERROR;
             }
 
             ctx->default_value = var;
+
+            return NGX_CONF_OK;
         }
 
+        if (value[0].len && ch == '!') {
+            value[0].len--;
+            value[0].data++;
+        }
+
+        flags = 0;
+
     } else {
 
-        if (value[0].len < 3 || value[0].data[1] != '.') {
+        if ((ch == '*' && (value[0].len < 3 || value[0].data[1] != '.'))
+            || (ch == '.' && value[0].len < 2))
+        {
             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                "invalid DNS wildcard \"%V\"", &value[0]);
+
             return NGX_CONF_ERROR;
         }
 
-        key = 0;
-
-        for (i = 2; i < value[0].len; i++) {
-            value[0].data[i] = ngx_tolower(value[0].data[i]);
-            key = ngx_hash(key, value[0].data[i]);
-        }
-
-        key %= NGX_HTTP_MAP_HASH;
-
-        /* convert "*.example.com" into "com.example.\0" */
-
-        len = 0;
-        n = 0;
-
-        for (i = value[0].len - 1; i; i--) {
-            if (value[0].data[i] == '.') {
-                ngx_memcpy(&buf[n], &value[0].data[i + 1], len);
-                n += len;
-                buf[n++] = '.';
-                len = 0;
-                continue;
-            }
-
-            len++;
-        }
-
-        buf[n] = '\0';
-
-        name = ctx->dns_hash[key].elts;
-
-        if (name) {
-            for (i = 0; i < ctx->dns_hash[key].nelts; i++) {
-                if (value[0].len != name[i].len) {
-                    continue;
-                }
-
-                if (ngx_strncmp(value[0].data, name[i].data, value[0].len)
-                    == 0)
-                {
-                    m = ctx->dns_wildcards.elts;
-                    for (i = 0; i < ctx->dns_wildcards.nelts; i++) {
-                        if (ngx_strcmp(buf, m[i].key.data) == 0) {
-                            old = m[i].value;
-                            m[i].value = var;
-
-                            goto duplicate;
-                        }
-                    }
-                }
-            }
-
-        } else {
-            if (ngx_array_init(&ctx->dns_hash[key], cf->pool, 4,
-                               sizeof(ngx_str_t))
-                != NGX_OK)
-            {
-                return NGX_CONF_ERROR;
-            }
-        }
-
-        name = ngx_array_push(&ctx->dns_hash[key]);
-        if (name == NULL) {
-            return NGX_CONF_ERROR;
-        }
-
-        *name = value[0];
-
-        ngx_memcpy(value[0].data, buf, value[0].len);
-        value[0].len--;
-
-        m = ngx_array_push(&ctx->dns_wildcards);
-        if (m == NULL) {
-            return NGX_CONF_ERROR;
-        }
-
-        m->key = value[0];
-        m->key_hash = 0;
-        m->value = var;
+        flags = NGX_HTTP_WILDCARD_HASH;
     }
 
-    return NGX_CONF_OK;
+    rc = ngx_http_config_add_hash(&ctx->hash, &value[0], var, flags);
 
-duplicate:
+    if (rc == NGX_OK) {
+        return NGX_CONF_OK;
+    }
 
-    ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
-                       "duplicate parameter \"%V\", value: \"%V\", "
-                       "old value: \"%V\"",
-                       &value[0], var, old);
+    if (rc == NGX_BUSY) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "conflicting parameter \"%V\"", &value[0]);
+    }
 
-    return NGX_CONF_OK;
+    return NGX_CONF_ERROR;
 }
diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c
index c39f320..c2be2c4 100644
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -1926,6 +1926,7 @@
 
     size_t                       add;
     ngx_str_t                   *value, *url;
+    ngx_uint_t                   port;
     ngx_inet_upstream_t          inet_upstream;
     ngx_http_core_loc_conf_t    *clcf;
 #if (NGX_HTTP_SSL)
@@ -1945,12 +1946,14 @@
 
     if (ngx_strncasecmp(url->data, "http://", 7) == 0) {
         add = 7;
+        port = 80;
 
     } else if (ngx_strncasecmp(url->data, "https://", 8) == 0) {
 
 #if (NGX_HTTP_SSL)
 
         add = 8;
+        port = 443;
 
         plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
         if (plcf->upstream.ssl == NULL) {
@@ -2019,7 +2022,7 @@
         inet_upstream.name = *url;
         inet_upstream.url.len = url->len - add;
         inet_upstream.url.data = url->data + add;
-        inet_upstream.default_port_value = 80;
+        inet_upstream.default_port_value = port;
         inet_upstream.uri_part = 1;
 
         plcf->peers = ngx_inet_upstream_parse(cf, &inet_upstream);
@@ -2032,8 +2035,8 @@
         plcf->upstream.uri = inet_upstream.uri;
     }
 
-    plcf->upstream.schema.len = sizeof("http://") - 1;
-    plcf->upstream.schema.data = (u_char *) "http://";
+    plcf->upstream.schema.len = add;
+    plcf->upstream.schema.data = url->data;
 
     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
 
diff --git a/src/http/ngx_http_config.c b/src/http/ngx_http_config.c
new file mode 100644
index 0000000..965625d
--- /dev/null
+++ b/src/http/ngx_http_config.c
@@ -0,0 +1,214 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_event.h>
+#include <ngx_http.h>
+
+
+ngx_int_t
+ngx_http_config_add_hash(ngx_http_hash_conf_t *h, ngx_str_t *key, void *value,
+    ngx_uint_t flags)
+{
+    size_t           len;
+    ngx_str_t       *name;
+    ngx_uint_t       i, k, n, skip;
+    ngx_hash_key_t  *hk;
+    u_char           buf[2048];
+
+    if (!(flags & NGX_HTTP_WILDCARD_HASH)) {
+
+        /* exact hash */
+
+        k = 0;
+
+        for (i = 0; i < key->len; i++) {
+            key->data[i] = ngx_tolower(key->data[i]);
+            k = ngx_hash(k, key->data[i]);
+        }
+
+        k %= NGX_HTTP_CONFIG_HASH;
+
+        /* check conflicts in exact hash */
+
+        name = h->keys_hash[k].elts;
+
+        if (name) {
+            for (i = 0; i < h->keys_hash[k].nelts; i++) {
+                if (key->len != name[i].len) {
+                    continue;
+                }
+
+                if (ngx_strncmp(key->data, name[i].data, key->len) == 0) {
+                    return NGX_BUSY;
+                }
+            }
+
+        } else {
+            if (ngx_array_init(&h->keys_hash[k], h->temp_pool, 4,
+                               sizeof(ngx_str_t))
+                != NGX_OK)
+            {
+                return NGX_ERROR;
+            }
+        }
+
+        name = ngx_array_push(&h->keys_hash[k]);
+        if (name == NULL) {
+            return NGX_ERROR;
+        }
+
+        *name = *key;
+
+        hk = ngx_array_push(&h->keys);
+        if (hk == NULL) {
+            return NGX_ERROR;
+        }
+
+        hk->key = *key;
+        hk->key_hash = ngx_hash_key(key->data, key->len);
+        hk->value = value;
+
+    } else {
+
+        /* wildcard hash */
+
+        skip = (key->data[0] == '*') ? 2 : 1;
+        k = 0;
+
+        for (i = skip; i < key->len; i++) {
+            key->data[i] = ngx_tolower(key->data[i]);
+            k = ngx_hash(k, key->data[i]);
+        }
+
+        k %= NGX_HTTP_CONFIG_HASH;
+
+        if (skip == 1) {
+
+            /* check conflicts in exact hash for ".example.com" */
+
+            name = h->keys_hash[k].elts;
+
+            if (name) {
+                len = key->len - skip;
+
+                for (i = 0; i < h->keys_hash[k].nelts; i++) {
+                    if (len != name[i].len) {
+                        continue;
+                    }
+
+                    if (ngx_strncmp(&key->data[1], name[i].data, len) == 0) {
+                        return NGX_BUSY;
+                    }
+                }
+
+            } else {
+                if (ngx_array_init(&h->keys_hash[k], h->temp_pool, 4,
+                                   sizeof(ngx_str_t))
+                    != NGX_OK)
+                {
+                    return NGX_ERROR;
+                }
+            }
+
+            name = ngx_array_push(&h->keys_hash[k]);
+            if (name == NULL) {
+                return NGX_ERROR;
+            }
+
+            name->len = key->len - 1;
+            name->data = ngx_palloc(h->temp_pool, name->len);
+            if (name->data == NULL) {
+                return NGX_ERROR;
+            }
+
+            ngx_memcpy(name->data, &key->data[1], name->len);
+        }
+
+
+        /*
+         * convert "*.example.com" to "com.example.\0"
+         *      and ".example.com" to "com.example\0"
+         */
+
+        len = 0;
+        n = 0;
+
+        for (i = key->len - 1; i; i--) {
+            if (key->data[i] == '.') {
+                ngx_memcpy(&buf[n], &key->data[i + 1], len);
+                n += len;
+                buf[n++] = '.';
+                len = 0;
+                continue;
+            }
+
+            len++;
+        }
+
+        if (len) {
+            ngx_memcpy(&buf[n], &key->data[1], len);
+            n += len;
+        }
+
+        buf[n] = '\0';
+
+
+        /* check conflicts in wildcard hash */
+
+        name = h->dns_hash[k].elts;
+
+        if (name) {
+            len = key->len - skip;
+
+            for (i = 0; i < h->dns_hash[k].nelts; i++) {
+                if (len != name[i].len) {
+                    continue;
+                }
+
+                if (ngx_strncmp(key->data + skip, name[i].data, len) == 0) {
+                    return NGX_BUSY;
+                }
+            }
+
+        } else {
+            if (ngx_array_init(&h->dns_hash[k], h->temp_pool, 4,
+                               sizeof(ngx_str_t))
+                != NGX_OK)
+            {
+                return NGX_ERROR;
+            }
+        }
+
+        name = ngx_array_push(&h->dns_hash[k]);
+        if (name == NULL) {
+            return NGX_ERROR;
+        }
+
+        name->len = key->len - skip;
+        name->data = ngx_palloc(h->temp_pool, name->len);
+        if (name->data == NULL) {
+            return NGX_ERROR;
+        }
+        ngx_memcpy(name->data, key->data + skip, name->len);
+
+
+        ngx_memcpy(key->data, buf, key->len);
+        key->len--;
+
+        hk = ngx_array_push(&h->dns_wildcards);
+        if (hk == NULL) {
+            return NGX_ERROR;
+        }
+
+        hk->key = *key;
+        hk->key_hash = 0;
+        hk->value = value;
+    }
+
+    return NGX_OK;
+}
diff --git a/src/http/ngx_http_config.h b/src/http/ngx_http_config.h
index d034351..4768085 100644
--- a/src/http/ngx_http_config.h
+++ b/src/http/ngx_http_config.h
@@ -14,9 +14,9 @@
 
 
 typedef struct {
-    void   **main_conf;
-    void   **srv_conf;
-    void   **loc_conf;
+    void        **main_conf;
+    void        **srv_conf;
+    void        **loc_conf;
 } ngx_http_conf_ctx_t;
 
 
@@ -72,5 +72,25 @@
          cycle->conf_ctx[ngx_http_module.index])->main_conf[module.ctx_index]
 
 
+#define NGX_HTTP_CONFIG_HASH    10007
+
+#define NGX_HTTP_WILDCARD_HASH  1
+
+typedef struct {
+    ngx_pool_t   *pool;
+    ngx_pool_t   *temp_pool;
+
+    ngx_array_t   keys;
+    ngx_array_t  *keys_hash;
+
+    ngx_array_t   dns_wildcards;
+    ngx_array_t  *dns_hash;
+} ngx_http_hash_conf_t;
+
+
+ngx_int_t ngx_http_config_add_hash(ngx_http_hash_conf_t *h, ngx_str_t *key,
+    void *value, ngx_uint_t flags);
+
+
 
 #endif /* _NGX_HTTP_CONFIG_H_INCLUDED_ */
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
index fc57fe4..5711b4a 100644
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -90,6 +90,8 @@
 static char *ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf);
 
 #if (NGX_HTTP_SSL)
+static void ngx_http_upstream_ssl_init_connection(ngx_http_request_t *,
+    ngx_http_upstream_t *u, ngx_connection_t *c);
 static void ngx_http_upstream_ssl_handshake(ngx_connection_t *c);
 static void ngx_http_upstream_ssl_shutdown(ngx_connection_t *c,
     ngx_peer_t *peer);
@@ -498,9 +500,8 @@
 static void
 ngx_http_upstream_connect(ngx_http_request_t *r, ngx_http_upstream_t *u)
 {
-    ngx_int_t            rc;
-    ngx_peer_t          *peer;
-    ngx_connection_t    *c;
+    ngx_int_t          rc;
+    ngx_connection_t  *c;
 
     r->connection->log->action = "connecting to upstream";
 
@@ -517,8 +518,7 @@
         return;
     }
 
-    peer = &u->peer.peers->peer[u->peer.cur_peer];
-    u->state->peer = &peer->name;
+    u->state->peer = &u->peer.peers->peer[u->peer.cur_peer].name;
 
     if (rc == NGX_BUSY) {
         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no live upstreams");
@@ -534,6 +534,7 @@
     c = u->peer.connection;
 
     c->data = r;
+
     c->write->handler = ngx_http_upstream_send_request_handler;
     c->read->handler = ngx_http_upstream_process_header;
 
@@ -587,40 +588,10 @@
         return;
     }
 
-    /* rc == NGX_OK */
-
 #if (NGX_HTTP_SSL)
 
-    if (u->conf->ssl) {
-        if (c->ssl == NULL) {
-
-            if (ngx_ssl_create_connection(u->conf->ssl, c,
-                                          NGX_SSL_BUFFER|NGX_SSL_CLIENT)
-                == NGX_ERROR)
-            {
-                ngx_http_upstream_finalize_request(r, u,
-                                               NGX_HTTP_INTERNAL_SERVER_ERROR);
-                return;
-            }
-
-            c->sendfile = 0;
-        }
-
-        if (ngx_ssl_set_session(c, peer->ssl_session) != NGX_OK) {
-            ngx_http_upstream_finalize_request(r, u,
-                                               NGX_HTTP_INTERNAL_SERVER_ERROR);
-            return;
-        }
-
-        rc = ngx_ssl_handshake(c);
-
-        if (rc == NGX_AGAIN) {
-            c->ssl->handler = ngx_http_upstream_ssl_handshake;
-            return;
-        }
-
-        ngx_http_upstream_ssl_handshake(c);
-
+    if (u->conf->ssl && c->ssl == NULL) {
+        ngx_http_upstream_ssl_init_connection(r, u, c);
         return;
     }
 
@@ -633,6 +604,43 @@
 #if (NGX_HTTP_SSL)
 
 static void
+ngx_http_upstream_ssl_init_connection(ngx_http_request_t *r,
+    ngx_http_upstream_t *u, ngx_connection_t *c)
+{
+    ngx_int_t    rc;
+    ngx_peer_t  *peer;
+
+    if (ngx_ssl_create_connection(u->conf->ssl, c,
+                                  NGX_SSL_BUFFER|NGX_SSL_CLIENT)
+        == NGX_ERROR)
+    {
+        ngx_http_upstream_finalize_request(r, u,
+                                           NGX_HTTP_INTERNAL_SERVER_ERROR);
+        return;
+    }
+
+    c->sendfile = 0;
+
+    peer = &u->peer.peers->peer[u->peer.cur_peer];
+
+    if (ngx_ssl_set_session(c, peer->ssl_session) != NGX_OK) {
+        ngx_http_upstream_finalize_request(r, u,
+                                           NGX_HTTP_INTERNAL_SERVER_ERROR);
+        return;
+    }
+
+    rc = ngx_ssl_handshake(c);
+
+    if (rc == NGX_AGAIN) {
+        c->ssl->handler = ngx_http_upstream_ssl_handshake;
+        return;
+    }
+
+    ngx_http_upstream_ssl_handshake(c);
+}
+
+
+static void
 ngx_http_upstream_ssl_handshake(ngx_connection_t *c)
 {
     ngx_http_request_t   *r;
@@ -671,7 +679,8 @@
                 sizeof(ngx_http_upstream_headers_in_t));
 
     if (ngx_list_init(&r->upstream->headers_in.headers, r->pool, 8,
-                      sizeof(ngx_table_elt_t)) != NGX_OK)
+                      sizeof(ngx_table_elt_t))
+        != NGX_OK)
     {
         return NGX_ERROR;
     }
@@ -851,6 +860,15 @@
         return;
     }
 
+#if (NGX_HTTP_SSL)
+
+    if (u->conf->ssl && c->ssl == NULL) {
+        ngx_http_upstream_ssl_init_connection(r, u, c);
+        return;
+    }
+
+#endif
+
     ngx_http_upstream_send_request(r, u);
 }