diff -rupN nginx-1.5.8/auto/modules nginx-1.5.8-proxy/auto/modules
--- nginx-1.5.8/auto/modules	2013-12-17 14:46:27.000000000 +0100
+++ nginx-1.5.8-proxy/auto/modules	2014-01-18 00:52:25.398636700 +0100
@@ -303,6 +303,10 @@ if [ $HTTP_SSL = YES ]; then
     HTTP_SRCS="$HTTP_SRCS $HTTP_SSL_SRCS"
 fi
 
+if [ $PROXY_PROTOCOL = YES ]; then
+    have=NGX_PROXY_PROTOCOL . auto/have
+fi
+
 if [ $HTTP_PROXY = YES ]; then
     have=NGX_HTTP_X_FORWARDED_FOR . auto/have
     #USE_MD5=YES
diff -rupN nginx-1.5.8/auto/options nginx-1.5.8-proxy/auto/options
--- nginx-1.5.8/auto/options	2013-12-17 14:46:27.000000000 +0100
+++ nginx-1.5.8-proxy/auto/options	2014-01-18 00:52:25.400636800 +0100
@@ -47,6 +47,8 @@ USE_THREADS=NO
 NGX_FILE_AIO=NO
 NGX_IPV6=NO
 
+PROXY_PROTOCOL=NO
+
 HTTP=YES
 
 NGX_HTTP_LOG_PATH=
@@ -193,6 +195,8 @@ do
         --with-file-aio)                 NGX_FILE_AIO=YES           ;;
         --with-ipv6)                     NGX_IPV6=YES               ;;
 
+        --with-proxy-protocol)           PROXY_PROTOCOL=YES         ;;
+
         --without-http)                  HTTP=NO                    ;;
         --without-http-cache)            HTTP_CACHE=NO              ;;
 
@@ -352,6 +356,8 @@ cat << END
   --with-file-aio                    enable file AIO support
   --with-ipv6                        enable IPv6 support
 
+  --with-proxy-protocol              enable proxy protocol support
+
   --with-http_ssl_module             enable ngx_http_ssl_module
   --with-http_spdy_module            enable ngx_http_spdy_module
   --with-http_realip_module          enable ngx_http_realip_module
diff -rupN nginx-1.5.8/auto/sources nginx-1.5.8-proxy/auto/sources
--- nginx-1.5.8/auto/sources	2013-12-17 14:46:27.000000000 +0100
+++ nginx-1.5.8-proxy/auto/sources	2014-01-18 00:52:25.404637100 +0100
@@ -36,7 +36,8 @@ CORE_DEPS="src/core/nginx.h \
            src/core/ngx_conf_file.h \
            src/core/ngx_resolver.h \
            src/core/ngx_open_file_cache.h \
-           src/core/ngx_crypt.h"
+           src/core/ngx_crypt.h \
+           src/core/ngx_proxy_protocol.h"
 
 
 CORE_SRCS="src/core/nginx.c \
@@ -67,7 +68,8 @@ CORE_SRCS="src/core/nginx.c \
            src/core/ngx_conf_file.c \
            src/core/ngx_resolver.c \
            src/core/ngx_open_file_cache.c \
-           src/core/ngx_crypt.c"
+           src/core/ngx_crypt.c \
+           src/core/ngx_proxy_protocol.c"
 
 
 REGEX_MODULE=ngx_regex_module
diff -rupN nginx-1.5.8/src/core/ngx_connection.h nginx-1.5.8-proxy/src/core/ngx_connection.h
--- nginx-1.5.8/src/core/ngx_connection.h	2013-12-17 14:46:27.000000000 +0100
+++ nginx-1.5.8-proxy/src/core/ngx_connection.h	2014-01-18 00:52:25.407637200 +0100
@@ -63,6 +63,10 @@ struct ngx_listening_s {
     unsigned            shared:1;    /* shared between threads or processes */
     unsigned            addr_ntop:1;
 
+#if (NGX_PROXY_PROTOCOL)
+    unsigned            accept_proxy_protocol:2; /* proxy_protocol flag */
+#endif
+
 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
     unsigned            ipv6only:1;
 #endif
@@ -153,6 +157,10 @@ struct ngx_connection_s {
 
     ngx_uint_t          requests;
 
+#if (NGX_PROXY_PROTOCOL)
+    ngx_uint_t          proxy_protocol;
+#endif
+
     unsigned            buffered:8;
 
     unsigned            log_error:3;     /* ngx_connection_log_error_e */
diff -rupN nginx-1.5.8/src/core/ngx_core.h nginx-1.5.8-proxy/src/core/ngx_core.h
--- nginx-1.5.8/src/core/ngx_core.h	2013-12-17 14:46:27.000000000 +0100
+++ nginx-1.5.8-proxy/src/core/ngx_core.h	2014-01-18 00:52:25.412637500 +0100
@@ -21,6 +21,9 @@ typedef struct ngx_file_s        ngx_fil
 typedef struct ngx_event_s       ngx_event_t;
 typedef struct ngx_event_aio_s   ngx_event_aio_t;
 typedef struct ngx_connection_s  ngx_connection_t;
+#if (NGX_PROXY_PROTOCOL)
+typedef struct ngx_proxy_protocol_s ngx_proxy_protocol_t;
+#endif
 
 typedef void (*ngx_event_handler_pt)(ngx_event_t *ev);
 typedef void (*ngx_connection_handler_pt)(ngx_connection_t *c);
@@ -77,6 +80,9 @@ typedef void (*ngx_connection_handler_pt
 #include <ngx_open_file_cache.h>
 #include <ngx_os.h>
 #include <ngx_connection.h>
+#if (NGX_PROXY_PROTOCOL)
+#include <ngx_proxy_protocol.h>
+#endif
 
 
 #define LF     (u_char) 10
diff -rupN nginx-1.5.8/src/http/modules/ngx_http_proxy_module.c nginx-1.5.8-proxy/src/http/modules/ngx_http_proxy_module.c
--- nginx-1.5.8/src/http/modules/ngx_http_proxy_module.c	2013-12-17 14:46:27.000000000 +0100
+++ nginx-1.5.8-proxy/src/http/modules/ngx_http_proxy_module.c	2014-01-18 00:52:25.422638100 +0100
@@ -8,7 +8,9 @@
 #include <ngx_config.h>
 #include <ngx_core.h>
 #include <ngx_http.h>
-
+#if (NGX_PROXY_PROTOCOL)
+#include <ngx_proxy_protocol.h>
+#endif
 
 typedef struct ngx_http_proxy_rewrite_s  ngx_http_proxy_rewrite_t;
 
@@ -386,6 +388,17 @@ static ngx_command_t  ngx_http_proxy_com
       offsetof(ngx_http_proxy_loc_conf_t, upstream.busy_buffers_size_conf),
       NULL },
 
+#if (NGX_PROXY_PROTOCOL)
+
+    { ngx_string("send_proxy_protocol"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_proxy_loc_conf_t, upstream.send_proxy_protocol),
+      NULL },
+
+#endif
+
 #if (NGX_HTTP_CACHE)
 
     { ngx_string("proxy_cache"),
@@ -2441,6 +2454,11 @@ ngx_http_proxy_create_loc_conf(ngx_conf_
     conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;
 
     conf->upstream.intercept_errors = NGX_CONF_UNSET;
+
+#if (NGX_PROXY_PROTOCOL)
+    conf->upstream.send_proxy_protocol = NGX_CONF_UNSET;
+#endif
+
 #if (NGX_HTTP_SSL)
     conf->upstream.ssl_session_reuse = NGX_CONF_UNSET;
 #endif
@@ -2713,6 +2731,11 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t
     ngx_conf_merge_value(conf->upstream.intercept_errors,
                               prev->upstream.intercept_errors, 0);
 
+#if (NGX_PROXY_PROTOCOL)
+    ngx_conf_merge_value(conf->upstream.send_proxy_protocol,
+                              prev->upstream.send_proxy_protocol, 0);
+#endif
+
 #if (NGX_HTTP_SSL)
     ngx_conf_merge_value(conf->upstream.ssl_session_reuse,
                               prev->upstream.ssl_session_reuse, 1);
diff -rupN nginx-1.5.8/src/http/ngx_http.c nginx-1.5.8-proxy/src/http/ngx_http.c
--- nginx-1.5.8/src/http/ngx_http.c	2013-12-17 14:46:27.000000000 +0100
+++ nginx-1.5.8-proxy/src/http/ngx_http.c	2014-01-18 00:52:25.426638300 +0100
@@ -1231,6 +1231,9 @@ ngx_http_add_addresses(ngx_conf_t *cf, n
 #if (NGX_HTTP_SPDY)
     ngx_uint_t             spdy;
 #endif
+#if (NGX_PROXY_PROTOCOL)
+    ngx_uint_t             accept_proxy_protocol;
+#endif
 
     /*
      * we cannot compare whole sockaddr struct's as kernel
@@ -1286,6 +1289,10 @@ ngx_http_add_addresses(ngx_conf_t *cf, n
 #if (NGX_HTTP_SPDY)
         spdy = lsopt->spdy || addr[i].opt.spdy;
 #endif
+#if (NGX_PROXY_PROTOCOL)
+        accept_proxy_protocol = lsopt->accept_proxy_protocol
+                                || addr[i].opt.accept_proxy_protocol;
+#endif
 
         if (lsopt->set) {
 
@@ -1319,6 +1326,9 @@ ngx_http_add_addresses(ngx_conf_t *cf, n
 #if (NGX_HTTP_SPDY)
         addr[i].opt.spdy = spdy;
 #endif
+#if (NGX_PROXY_PROTOCOL)
+        addr[i].opt.accept_proxy_protocol = accept_proxy_protocol;
+#endif
 
         return NGX_OK;
     }
@@ -1765,6 +1775,11 @@ ngx_http_add_listening(ngx_conf_t *cf, n
     ls->pool_size = cscf->connection_pool_size;
     ls->post_accept_timeout = cscf->client_header_timeout;
 
+#if (NGX_PROXY_PROTOCOL)
+// CLEANUP:    ls->accept_proxy_protocol = cscf->accept_proxy_protocol;
+    ls->accept_proxy_protocol = addr->opt.accept_proxy_protocol;
+#endif
+
     clcf = cscf->ctx->loc_conf[ngx_http_core_module.ctx_index];
 
     ls->logp = clcf->error_log;
@@ -1847,6 +1862,9 @@ ngx_http_add_addrs(ngx_conf_t *cf, ngx_h
 #if (NGX_HTTP_SPDY)
         addrs[i].conf.spdy = addr[i].opt.spdy;
 #endif
+#if (NGX_PROXY_PROTOCOL)
+        addrs[i].conf.accept_proxy_protocol = addr[i].opt.accept_proxy_protocol;
+#endif
 
         if (addr[i].hash.buckets == NULL
             && (addr[i].wc_head == NULL
@@ -1911,6 +1929,9 @@ ngx_http_add_addrs6(ngx_conf_t *cf, ngx_
 #if (NGX_HTTP_SPDY)
         addrs6[i].conf.spdy = addr[i].opt.spdy;
 #endif
+#if (NGX_PROXY_PROTOCOL)
+        addrs6[i].conf.accept_proxy_protocol = addr[i].opt.accept_proxy_protocol;
+#endif
 
         if (addr[i].hash.buckets == NULL
             && (addr[i].wc_head == NULL
diff -rupN nginx-1.5.8/src/http/ngx_http_core_module.c nginx-1.5.8-proxy/src/http/ngx_http_core_module.c
--- nginx-1.5.8/src/http/ngx_http_core_module.c	2013-12-17 14:46:28.000000000 +0100
+++ nginx-1.5.8-proxy/src/http/ngx_http_core_module.c	2014-01-18 00:52:25.433638700 +0100
@@ -4125,6 +4125,15 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx
             continue;
         }
 
+#if (NGX_PROXY_PROTOCOL)
+        if (ngx_strncmp(value[n].data, "accept_proxy_protocol=on", 24) == 0) {
+            lsopt.accept_proxy_protocol = 1;
+            lsopt.set = 1;
+            lsopt.bind = 1;
+            continue;
+        }
+#endif
+
         if (ngx_strncmp(value[n].data, "ipv6only=o", 10) == 0) {
 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
             struct sockaddr  *sa;
diff -rupN nginx-1.5.8/src/http/ngx_http_core_module.h nginx-1.5.8-proxy/src/http/ngx_http_core_module.h
--- nginx-1.5.8/src/http/ngx_http_core_module.h	2013-12-17 14:46:28.000000000 +0100
+++ nginx-1.5.8-proxy/src/http/ngx_http_core_module.h	2014-01-18 00:52:25.436638900 +0100
@@ -78,6 +78,11 @@ typedef struct {
 #if (NGX_HTTP_SPDY)
     unsigned                   spdy:1;
 #endif
+
+#if (NGX_PROXY_PROTOCOL)
+    unsigned                   accept_proxy_protocol:2;
+#endif
+
 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
     unsigned                   ipv6only:1;
 #endif
@@ -237,6 +242,10 @@ struct ngx_http_addr_conf_s {
 
     ngx_http_virtual_names_t  *virtual_names;
 
+#if (NGX_PROXY_PROTOCOL)
+    ngx_flag_t                  accept_proxy_protocol;
+#endif
+
 #if (NGX_HTTP_SSL)
     unsigned                   ssl:1;
 #endif
diff -rupN nginx-1.5.8/src/http/ngx_http_request.c nginx-1.5.8-proxy/src/http/ngx_http_request.c
--- nginx-1.5.8/src/http/ngx_http_request.c	2013-12-17 14:46:28.000000000 +0100
+++ nginx-1.5.8-proxy/src/http/ngx_http_request.c	2014-01-18 00:52:25.441639200 +0100
@@ -63,6 +63,9 @@ static void ngx_http_ssl_handshake(ngx_e
 static void ngx_http_ssl_handshake_handler(ngx_connection_t *c);
 #endif
 
+#if (NGX_PROXY_PROTOCOL)
+static void ngx_http_proxy_protocol(ngx_event_t *rev);
+#endif
 
 static char *ngx_http_client_errors[] = {
 
@@ -343,6 +346,14 @@ ngx_http_init_connection(ngx_connection_
     }
 #endif
 
+#if (NGX_PROXY_PROTOCOL)
+    {
+        if (hc->addr_conf->accept_proxy_protocol) {
+            rev->handler = ngx_http_proxy_protocol;
+        }
+    }
+#endif
+
     if (rev->ready) {
         /* the deferred accept(), rtsig, aio, iocp */
 
@@ -364,7 +375,6 @@ ngx_http_init_connection(ngx_connection_
     }
 }
 
-
 static void
 ngx_http_wait_request_handler(ngx_event_t *rev)
 {
@@ -483,6 +493,12 @@ ngx_http_wait_request_handler(ngx_event_
     }
 
     rev->handler = ngx_http_process_request_line;
+
+#if (NGX_PROXY_PROTOCOL)
+    if (hc->addr_conf->accept_proxy_protocol)
+        rev->handler = ngx_http_proxy_protocol;
+#endif
+
     ngx_http_process_request_line(rev);
 }
 
@@ -597,6 +613,67 @@ ngx_http_create_request(ngx_connection_t
     return r;
 }
 
+#if (NGX_PROXY_PROTOCOL)
+
+static void
+ngx_http_proxy_protocol(ngx_event_t *rev)
+{
+    ssize_t                n;
+    size_t                 size = 1024;
+    u_char                 tmpbuf[size];
+    ngx_connection_t      *c;
+    ngx_http_connection_t *hc;
+
+    c = rev->data;
+    hc = c->data;
+    rev->handler = ngx_http_wait_request_handler;
+
+#if (NGX_HTTP_SPDY)
+    {
+        if (hc->addr_conf->spdy) {
+            rev->handler = ngx_http_spdy_init;
+        }
+    }
+#endif
+
+#if (NGX_HTTP_SSL)
+    {
+        if (hc->addr_conf->ssl) {
+            rev->handler = ngx_http_ssl_handshake;
+        }
+    }
+#endif
+
+    n = recv(c->fd, tmpbuf, size, MSG_PEEK);
+
+    if ((n <= 0) && (c->listening)
+            && (hc->addr_conf->accept_proxy_protocol)
+	    && (!c->proxy_protocol)) {
+        ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "ngx_http_proxy_protocol: pp required but not found");
+        return;
+    }
+    if ((n > 0) && (c->listening)
+            && (hc->addr_conf->accept_proxy_protocol)
+	    && (!c->proxy_protocol)) {
+        ssize_t m;
+        if (!(m = ngx_recv_proxy_protocol(c, tmpbuf, n))) {
+            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "ngx_http_proxy_protocol: pp required but not found");
+            ngx_http_close_connection(c);
+            return;
+        }
+        ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "ngx_http_proxy_protocol: pp required and found");
+
+        c->proxy_protocol = 1;
+
+        /* strip the proxy protocol string from the buffer */
+        recv(c->fd, tmpbuf, m, 0);
+    }
+
+    rev->handler(rev);
+}
+
+#endif
+
 
 #if (NGX_HTTP_SSL)
 
@@ -1315,6 +1392,10 @@ ngx_http_read_request_header(ngx_http_re
     c = r->connection;
     rev = c->read;
 
+fprintf(stderr, "DEBUG: pos: %p, last: %p, start: %p, end: %p\n",
+		r->header_in->pos, r->header_in->last, r->header_in->start,
+		r->header_in->end);
+
     n = r->header_in->last - r->header_in->pos;
 
     if (n > 0) {
diff -rupN nginx-1.5.8/src/http/ngx_http_upstream.c nginx-1.5.8-proxy/src/http/ngx_http_upstream.c
--- nginx-1.5.8/src/http/ngx_http_upstream.c	2013-12-17 14:46:28.000000000 +0100
+++ nginx-1.5.8-proxy/src/http/ngx_http_upstream.c	2014-01-18 00:52:25.449639600 +0100
@@ -33,6 +33,10 @@ static ngx_int_t ngx_http_upstream_reini
     ngx_http_upstream_t *u);
 static void ngx_http_upstream_send_request(ngx_http_request_t *r,
     ngx_http_upstream_t *u);
+#if (NGX_PROXY_PROTOCOL)
+static void ngx_http_upstream_send_proxy_protocol(ngx_http_request_t *r,
+    ngx_http_upstream_t *u);
+#endif
 static void ngx_http_upstream_send_request_handler(ngx_http_request_t *r,
     ngx_http_upstream_t *u);
 static void ngx_http_upstream_process_header(ngx_http_request_t *r,
@@ -1312,6 +1316,13 @@ ngx_http_upstream_connect(ngx_http_reque
 
     u->request_sent = 0;
 
+#if (NGX_PROXY_PROTOCOL)
+    if (u->conf->send_proxy_protocol && !(u->ssl && c->ssl == NULL)) {
+        ngx_http_upstream_send_proxy_protocol(r, u);
+	return;
+    }
+#endif
+
     if (rc == NGX_AGAIN) {
         ngx_add_timer(c->write, u->conf->connect_timeout);
         return;
@@ -1554,6 +1565,228 @@ ngx_http_upstream_send_request(ngx_http_
 }
 
 
+#if (NGX_PROXY_PROTOCOL)
+
+static void
+ngx_http_upstream_send_proxy_protocol(ngx_http_request_t *r, ngx_http_upstream_t *u)
+{
+    size_t                   len;
+    ngx_int_t                rc;
+    ngx_connection_t        *uc;
+    ngx_connection_t        *cc;
+    ngx_chain_t             *pp_string;
+    ngx_proxy_protocol_t     pp;
+    ngx_buf_t               *b;
+    char                     port[6];
+    u_char                  *addr;
+    struct sockaddr_storage  sa_src;
+    struct sockaddr_storage  sa_dst;
+    socklen_t                addrlen = NGX_SOCKADDRLEN;
+    struct sockaddr_in      *sin_src;
+    struct sockaddr_in      *sin_dst;
+
+#if (NGX_HAVE_INET6)
+
+    struct sockaddr_in6     *sin6_src;
+    struct sockaddr_in6     *sin6_dst;
+
+#endif
+
+
+    uc = u->peer.connection;
+    cc = r->connection;
+
+    if ( !(u->conf->send_proxy_protocol) ) {
+        return;
+    }
+
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, uc->log, 0,
+                   "http upstream send proxy protocol");
+
+    if (!u->request_sent && ngx_http_upstream_test_connect(uc) != NGX_OK) {
+        ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
+        return;
+    }
+
+    uc->log->action = "sending proxy protocol to upstream";
+
+    len = 0;
+
+    if (r->connection->proxy_protocol) {
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, uc->log, 0,
+                   "PP: got proxy-protocol from client connection");
+
+        switch (cc->sockaddr->sa_family) {
+
+#if (NGX_HAVE_INET6)
+
+            case AF_INET6:
+
+                pp.pp_proto = NGX_PP_PROTO_TCP6;
+                sin6_dst = (struct sockaddr_in6 *) cc->local_sockaddr;
+                sin6_src = (struct sockaddr_in6 *) cc->sockaddr;
+
+                break;
+
+#endif
+
+            default:
+                pp.pp_proto = NGX_PP_PROTO_TCP4;
+                sin_dst = (struct sockaddr_in *) cc->local_sockaddr;
+                sin_src = (struct sockaddr_in *) cc->sockaddr;
+
+        }
+
+    } else {
+
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, uc->log, 0,
+                   "PP: collecting information from socket fd");
+
+        getsockname(cc->fd, (struct sockaddr *) &sa_dst, &addrlen);
+
+        switch (sa_dst.ss_family) {
+
+#if (NGX_HAVE_INET6)
+
+            case AF_INET6:
+
+                pp.pp_proto = NGX_PP_PROTO_TCP6;
+                sin6_dst = (struct sockaddr_in6 *) &sa_dst;
+
+                getpeername(cc->fd, (struct sockaddr *) &sa_src, &addrlen);
+                sin6_src = (struct sockaddr_in6 *) &sa_src;
+
+                break;
+
+#endif
+
+            default:
+
+                pp.pp_proto = NGX_PP_PROTO_TCP4;
+                sin_dst = (struct sockaddr_in *) &sa_dst;
+                getpeername(cc->fd, (struct sockaddr *) &sa_src, &addrlen);
+                sin_src = (struct sockaddr_in *) &sa_src;
+        }
+
+
+    }
+
+    switch (pp.pp_proto) {
+
+#if (NGX_HAVE_INET6)
+
+        case NGX_PP_PROTO_TCP6:
+
+            /* dst3 and dst4 */
+            addr = ngx_pcalloc(r->pool, NGX_INET6_ADDRSTRLEN);
+            ngx_inet_ntop(AF_INET6, &sin6_dst->sin6_addr, addr, NGX_INET6_ADDRSTRLEN);
+            pp.pp_dst3_text.data = ngx_pcalloc(r->pool, NGX_INET6_ADDRSTRLEN);
+            pp.pp_dst3_text.len = ngx_strlen(addr);
+            ngx_memcpy(pp.pp_dst3_text.data, addr, pp.pp_dst3_text.len);
+            pp.pp_dst4 = htons(sin6_dst->sin6_port);
+
+            ngx_memzero(addr, NGX_INET6_ADDRSTRLEN);
+
+            /* src3 and src4 */
+            ngx_inet_ntop(AF_INET6, &sin6_src->sin6_addr, addr, NGX_INET6_ADDRSTRLEN);
+            pp.pp_src3_text.data = ngx_pcalloc(r->pool, NGX_INET6_ADDRSTRLEN);
+            pp.pp_src3_text.len = ngx_strlen(addr);
+            ngx_memcpy(pp.pp_src3_text.data, addr, pp.pp_src3_text.len);
+            pp.pp_src4 = htons(sin6_src->sin6_port);
+
+        break;
+
+#endif
+
+        default:
+
+            /* dst3 and dst4 */
+            addr = ngx_pcalloc(r->pool, NGX_INET_ADDRSTRLEN);
+            ngx_inet_ntop(AF_INET, &sin_dst->sin_addr, addr, NGX_INET_ADDRSTRLEN);
+            pp.pp_dst3_text.data = ngx_pcalloc(r->pool, NGX_INET_ADDRSTRLEN);
+            pp.pp_dst3_text.len = ngx_strlen(addr);
+            ngx_memcpy(pp.pp_dst3_text.data, addr, pp.pp_dst3_text.len);
+            pp.pp_dst4 = htons(sin_dst->sin_port);
+
+            ngx_memzero(addr, NGX_INET_ADDRSTRLEN);
+
+            /* src3 and src4 */
+            ngx_inet_ntop(AF_INET, &sin_src->sin_addr, addr, NGX_INET_ADDRSTRLEN);
+            pp.pp_src3_text.data = ngx_pcalloc(r->pool, NGX_INET_ADDRSTRLEN);
+            pp.pp_src3_text.len = ngx_strlen(addr);
+            ngx_memcpy(pp.pp_src3_text.data, addr, pp.pp_src3_text.len);
+            pp.pp_src4 = htons(sin_src->sin_port);
+
+    }
+
+    len += ngx_proxy_protocol_string_length(&pp);
+
+    ngx_print_proxy_protocol(&pp, uc->log);
+
+    b = ngx_create_temp_buf(uc->pool, len);
+    if (b == NULL) {
+        return;
+    }
+
+    pp_string = ngx_alloc_chain_link(uc->pool);
+    if (pp_string == NULL) {
+        return;
+    }
+
+    pp_string->buf = b;
+    pp_string->next = NULL;
+
+    b->last = ngx_cpymem(b->last, "PROXY ", sizeof("PROXY ") - 1);
+
+    switch (pp.pp_proto) {
+        case NGX_PP_PROTO_TCP4:
+            b->last = ngx_cpymem(b->last, "TCP4 ", sizeof("TCP4 ") - 1);
+        break;
+        case NGX_PP_PROTO_TCP6:
+            b->last = ngx_cpymem(b->last, "TCP6 ", sizeof("TCP6 ") - 1);
+        break;
+    }
+
+    /* src3 */
+    b->last = ngx_cpymem(b->last, pp.pp_src3_text.data, pp.pp_src3_text.len);
+    b->last = ngx_cpymem(b->last, " ", 1);
+
+    /* dst3 */
+    b->last = ngx_cpymem(b->last, pp.pp_dst3_text.data, pp.pp_dst3_text.len);
+    b->last = ngx_cpymem(b->last, " ", 1);
+
+    /* src4 */
+    ngx_memzero(port, 6);
+    sprintf(port,"%d", pp.pp_src4);
+    b->last = ngx_cpymem(b->last, port, strlen(port));
+    b->last = ngx_cpymem(b->last, " ", 1);
+
+    /* dst4 */
+    ngx_memzero(port, 6);
+    sprintf(port,"%d", pp.pp_dst4);
+    b->last = ngx_cpymem(b->last, port, strlen(port));
+
+    /* CRLF */
+    b->last = ngx_cpymem(b->last, CRLF, sizeof(CRLF) - 1);
+
+    ngx_log_debug3(NGX_LOG_DEBUG_HTTP, uc->log, 0,
+            "http upstream send proxy protocol: %d -%*s-",
+            ngx_proxy_protocol_string_length(&pp),
+            ngx_proxy_protocol_string_length(&pp) - 2,
+            b->start);
+
+    rc = ngx_output_chain(&u->output, pp_string);
+
+    if (rc == NGX_ERROR) {
+        ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
+        return;
+    }
+
+}
+
+#endif
+
+
 static void
 ngx_http_upstream_send_request_handler(ngx_http_request_t *r,
     ngx_http_upstream_t *u)
diff -rupN nginx-1.5.8/src/http/ngx_http_upstream.h nginx-1.5.8-proxy/src/http/ngx_http_upstream.h
--- nginx-1.5.8/src/http/ngx_http_upstream.h	2013-12-17 14:46:28.000000000 +0100
+++ nginx-1.5.8-proxy/src/http/ngx_http_upstream.h	2014-01-18 00:52:25.452639800 +0100
@@ -192,6 +192,10 @@ typedef struct {
     unsigned                         intercept_404:1;
     unsigned                         change_buffering:1;
 
+#if (NGX_PROXY_PROTOCOL)
+    ngx_flag_t                       send_proxy_protocol;
+#endif
+
 #if (NGX_HTTP_SSL)
     ngx_ssl_t                       *ssl;
     ngx_flag_t                       ssl_session_reuse;
