Index: modules/proxy/mod_proxy.c
===================================================================
--- modules/proxy/mod_proxy.c	(revision 387655)
+++ modules/proxy/mod_proxy.c	(working copy)
@@ -697,6 +697,32 @@
 
     do {
         char *url = uri;
+        int use_proxy = 0;
+        apr_port_t port = 0;
+        const char *hostname = NULL;
+
+        /* find a proxy, unless a NoProxy directive is active */
+        if (!direct_connect) {
+            for (i = 0; i < proxies->nelts; i++) {
+                p2 = ap_strchr_c(ents[i].scheme, ':');  /* is it a partial URL? */
+                if (strcmp(ents[i].scheme, "*") == 0 ||
+                    (ents[i].use_regex &&
+                     ap_regexec(ents[i].regexp, url, 0, NULL, 0) == 0) ||
+                    (p2 == NULL && strcasecmp(scheme, ents[i].scheme) == 0) ||
+                    (p2 != NULL &&
+                    strncasecmp(url, ents[i].scheme,
+                                strlen(ents[i].scheme)) == 0)) {
+                    use_proxy = 1;
+                    if (strcmp(ents[i].protocol, "balancer") == 0)
+                        url = apr_pstrcat(r->pool, "balancer://", ents[i].hostname, NULL);
+                    else {
+                        port = ents[i].port;
+                        hostname = ents[i].hostname;
+                    }
+                }
+            }
+        }
+
         /* Try to obtain the most suitable worker */
         access_status = ap_proxy_pre_request(&worker, &balancer, r, conf, &url);
         if (access_status != OK) {
@@ -717,33 +743,38 @@
         }
         if (balancer && balancer->max_attempts_set && !max_attempts)
             max_attempts = balancer->max_attempts;
-        /* firstly, try a proxy, unless a NoProxy directive is active */
+        /* try a proxy, unless a NoProxy directive is active */
         if (!direct_connect) {
-            for (i = 0; i < proxies->nelts; i++) {
-                p2 = ap_strchr_c(ents[i].scheme, ':');  /* is it a partial URL? */
-                if (strcmp(ents[i].scheme, "*") == 0 ||
-                    (ents[i].use_regex &&
-                     ap_regexec(ents[i].regexp, url, 0, NULL, 0) == 0) ||
-                    (p2 == NULL && strcasecmp(scheme, ents[i].scheme) == 0) ||
-                    (p2 != NULL &&
-                    strncasecmp(url, ents[i].scheme,
-                                strlen(ents[i].scheme)) == 0)) {
+            if (use_proxy) {
+                /* handle the balancer */
+                if (!hostname) {
+                    apr_uri_t uri_proxy;
 
-                    /* handle the scheme */
-                    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
-                                 "Trying to run scheme_handler against proxy");
-                    access_status = proxy_run_scheme_handler(r, worker,
-                                                             conf, url,
-                                                             ents[i].hostname,
-                                                             ents[i].port);
-
-                    /* an error or success */
-                    if (access_status != DECLINED &&
-                        access_status != HTTP_BAD_GATEWAY) {
+                    if (apr_uri_parse(r->pool, url, &uri_proxy)) {
+                        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                                      "proxy: Unparseable balancer URL: %s.", url);
                         goto cleanup;
                     }
-                    /* we failed to talk to the upstream proxy */
+
+                    url = uri;
+                    port = uri_proxy.port;
+                    hostname = uri_proxy.hostname;
                 }
+
+                /* handle the scheme */
+                ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+                             "Trying to run scheme_handler against proxy");
+                access_status = proxy_run_scheme_handler(r, worker,
+                                                         conf, url,
+                                                         hostname,
+                                                         port);
+
+                /* an error or success */
+                if (access_status != DECLINED &&
+                    access_status != HTTP_BAD_GATEWAY) {
+                    goto cleanup;
+                }
+                /* we failed to talk to the upstream proxy */
             }
         }
 
