# Proxying request - HTTP/1.1 502 Bad Gateway

**URL:** <https://www.ruby-forum.com/t/proxying-request-http-1-1-502-bad-gateway/210465>\
**Category:** NGINX\
**Created:** [August 9, 2011, 8:12am UTC](https://www.ruby-forum.com/t/proxying-request-http-1-1-502-bad-gateway/210465 "2011-08-09T08:12:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [August 9, 2011, 8:12am UTC](https://www.ruby-forum.com/t/proxying-request-http-1-1-502-bad-gateway/210465/1 "2011-08-09T08:12:49Z")

</div>

Hello,  
we have problem with proxying some RPC (POST) requests to a backend  
server.  
When a request is sent without proxy to the backend directly everrything  
is OK. So problem is probably caused by nginx proxy.  
When the request is sent from the same server as proxy is installed,  
response is allways OK as well.  
The problem occurs just when a method with many parameters is called  
(large POST request). And the strangest thing is that the same request  
is sometimes OK and sometimes produces 502 Bad Gateway.

A part of http section from nginx.conf:  
client\_header\_buffer\_size 32k;  
large\_client\_header\_buffers 4 64k;  
client\_body\_buffer\_size 32k;  
proxy\_buffer\_size 32k;  
proxy\_buffers 8 16k;  
proxy\_busy\_buffers\_size 64k;  
sendfile on;  
#tcp\_nopush on;  
tcp\_nodelay on;  
keepalive\_timeout 5;  
keepalive\_requests 50;  
server\_tokens off

```
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

```

and the location just:  
location ~ “^/public/RPC2$” {  
proxy\_pass [http://localhost:3352](http://localhost:3352);  
}

Part of debug log, when the request is not processed correctly:  
2011/08/08 16:05:25 [debug] 23971#0: \*218 post event 00002B69C5051350  
2011/08/08 16:05:25 [debug] 23971#0: \*218 post event 00002B69C51F2350  
2011/08/08 16:05:25 [debug] 23971#0: \*218 delete posted event  
00002B69C51F2350  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http upstream request:  
“/public/RPC2?”  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http upstream dummy handler  
2011/08/08 16:05:25 [debug] 23971#0: \*218 delete posted event  
00002B69C5051350  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http upstream request:  
“/public/RPC2?”  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http upstream process header  
2011/08/08 16:05:25 [debug] 23971#0: \*218 malloc:  
000000001BC070A0:32768  
2011/08/08 16:05:25 [debug] 23971#0: \*218 recv: fd:48 0 of 32768  
2011/08/08 16:05:25 [error] 23971#0: \*218 upstream prematurely closed  
connection while reading response header from upstream, client:  
10.0.132.6, server: xxxxxxxxxxxx, request: “POST /public/RPC2 HTTP/1.1”,  
upstream: “[http://127.0.0.1:3352/public/RPC2](http://127.0.0.1:3352/public/RPC2)”, host: “yyyyyyyyyyyyyy”  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http next upstream, 2  
2011/08/08 16:05:25 [debug] 23971#0: \*218 free rr peer 1 4  
2011/08/08 16:05:25 [debug] 23971#0: \*218 finalize http upstream  
request: 502  
2011/08/08 16:05:25 [debug] 23971#0: \*218 finalize http proxy request  
2011/08/08 16:05:25 [debug] 23971#0: \*218 free rr peer 0 0  
2011/08/08 16:05:25 [debug] 23971#0: \*218 close http upstream  
connection: 48  
2011/08/08 16:05:25 [debug] 23971#0: \*218 event timer del: 48:  
1312812380324  
2011/08/08 16:05:25 [debug] 23971#0: \*218 reusable connection: 0  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http finalize request: 502,  
“/public/RPC2?” a:1, c:2  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http special response: 502,  
“/public/RPC2?”  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http set discard body  
2011/08/08 16:05:25 [debug] 23971#0: \*218 HTTP/1.1 502 Bad Gateway  
Server: nginx  
Date: Mon, 08 Aug 2011 14:05:25 GMT  
Content-Type: text/html  
Content-Length: 166  
Connection: keep-alive

2011/08/08 16:05:25 [debug] 23971#0: \*218 write new buf t:1 f:0  
000000001BBAB4F0, pos 000000001BBAB4F0, size: 150 file: 0, size: 0  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http write filter: l:0 f:0  
s:150  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http output filter  
“/public/RPC2?”  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http copy filter:  
“/public/RPC2?”  
2011/08/08 16:05:25 [debug] 23971#0: \*218 write old buf t:1 f:0  
000000001BBAB4F0, pos 000000001BBAB4F0, size: 150 file: 0, size: 0  
2011/08/08 16:05:25 [debug] 23971#0: \*218 write new buf t:0 f:0  
0000000000000000, pos 000000000068C360, size: 120 file: 0, size: 0  
2011/08/08 16:05:25 [debug] 23971#0: \*218 write new buf t:0 f:0  
0000000000000000, pos 000000000068B200, size: 46 file: 0, size: 0  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http write filter: l:1 f:0  
s:316  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http write filter limit 0  
2011/08/08 16:05:25 [debug] 23971#0: \*218 writev: 316  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http write filter  
0000000000000000  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http copy filter: 0  
“/public/RPC2?”  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http finalize request: 0,  
“/public/RPC2?” a:1, c:2  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http request count:2 blk:0  
2011/08/08 16:05:25 [debug] 23971#0: \*218 post event 00002B69C5051218  
2011/08/08 16:05:25 [debug] 23971#0: \*218 post event 00002B69C51F2218  
2011/08/08 16:05:25 [debug] 23971#0: \*218 delete posted event  
00002B69C51F2218  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http run request:  
“/public/RPC2?”  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http request empty handler  
2011/08/08 16:05:25 [debug] 23971#0: \*218 delete posted event  
00002B69C5051218  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http run request:  
“/public/RPC2?”  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http upstream check client,  
write event:0, “/public/RPC2”  
2011/08/08 16:05:25 [debug] 23971#0: \*218 http upstream recv(): 1

Posted at Nginx Forum:

> **[Proxying request - HTTP/1.1 502 Bad Gateway](https://forum.nginx.org/read.php?2,213552,213552#msg-213552)**
>
> Hello, we have problem with proxying some RPC (POST) requests to a backend server. When a request is sent without proxy to the backend directly everrything is OK. So problem is probably caused by nginx proxy. When the request is sent from the same...

---

<div class="post-metadata">

**Author:** ![michalkraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/aeb1de/32.png) [@michalkraus](https://www.ruby-forum.com/u/michalkraus)\
**Post date:** [August 9, 2011, 11:01am UTC](https://www.ruby-forum.com/t/proxying-request-http-1-1-502-bad-gateway/210465/2 "2011-08-09T11:01:06Z")

</div>

Hello!

On Tue, Aug 09, 2011 at 02:12:19AM -0400, michalkraus wrote:

> ```
> keepalive_timeout 5;
> keepalive_requests 50;
> server_tokens off
> 
> proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
> 
> ```
> 
> and the location just:  
> location ~ “^/public/RPC2$” {

Just a side note: it’s better to use

```
location = /public/RPC2 {

```

instead.

> ```
> proxy_pass http://localhost:3352;
> }
> 
> ```
> 
> Part of debug log, when the request is not processed correctly:

[…]

> 2011/08/08 16:05:25 [debug] 23971#0: \*218 recv: fd:48 0 of 32768  
> 2011/08/08 16:05:25 [error] 23971#0: \*218 upstream prematurely closed  
> connection while reading response header from upstream, client:  
> 10.0.132.6, server: xxxxxxxxxxxx, request: “POST /public/RPC2 HTTP/1.1”,  
> upstream: “[http://127.0.0.1:3352/public/RPC2](http://127.0.0.1:3352/public/RPC2)”, host: “yyyyyyyyyyyyyy”

Backend closed connection here. You may want to inspect backend  
logs (or debug your backend) to find out why it did that.

While it certainly possible that backend doesn’t like something in  
nginx request (compared to direct one), it’s not possible to say  
what exactly without debugging backend.

Maxim D.

---

<div class="post-metadata">

**Author:** ![michalkraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/aeb1de/32.png) [@michalkraus](https://www.ruby-forum.com/u/michalkraus)\
**Post date:** [August 10, 2011, 1:57pm UTC](https://www.ruby-forum.com/t/proxying-request-http-1-1-502-bad-gateway/210465/3 "2011-08-10T13:57:18Z")

</div>

We have found out that the problem is caused by my frpc module.  
Everything is OK withnout the module. This frpc module reads POST  
request body and thed generates a response. In location /public/RPC2 the  
module is off but some initial function of the module are carried out.  
There is probably an incorrect construnction in module initialization  
that doesn’t allow to run proxy module.

The major part of my module’s source code (C++ used):

typedef struct {  
ngx\_str\_t frpc\_log;  
ngx\_str\_t frpc\_service;  
} ngx\_http\_ap\_frpc\_ctx\_t;

typedef struct {  
ngx\_flag\_t enable;  
ngx\_int\_t gsidindex;  
ngx\_str\_t services;  
ngx\_str\_t controlserver;  
ngx\_str\_t servertype;  
} ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t;

static ngx\_command\_t ngx\_http\_ap\_frpc\_handler\_commands[] = {  
{ ngx\_string(“ap\_frpc”),

NGX\_HTTP\_MAIN\_CONF|NGX\_HTTP\_SRV\_CONF|NGX\_HTTP\_LOC\_CONF|NGX\_HTTP\_LIF\_CONF|NGX\_CONF\_TAKE1,  
ngx\_conf\_set\_flag\_slot,  
NGX\_HTTP\_LOC\_CONF\_OFFSET,  
offsetof(ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t, enable),  
NULL },  
// cookie vygenerovana ap\_filter modulem  
{ ngx\_string(“ap\_frpc\_gsid”),

NGX\_HTTP\_MAIN\_CONF|NGX\_HTTP\_SRV\_CONF|NGX\_HTTP\_LOC\_CONF|NGX\_HTTP\_LIF\_CONF|NGX\_CONF\_TAKE1,  
ngx\_http\_ap\_frpc\_gsid,  
NGX\_HTTP\_LOC\_CONF\_OFFSET,  
0,  
NULL },  
// seznam rozrazovanych sluzeb z konfiguracniho souboru - ostatni se  
loguji do other  
{ ngx\_string(“ap\_frpc\_services”),

NGX\_HTTP\_MAIN\_CONF|NGX\_HTTP\_SRV\_CONF|NGX\_HTTP\_LOC\_CONF|NGX\_HTTP\_LIF\_CONF|NGX\_CONF\_TAKE1,  
ngx\_conf\_set\_str\_slot,  
NGX\_HTTP\_LOC\_CONF\_OFFSET,  
offsetof(ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t, services),  
NULL },  
// adresa controlserveru pro ohlasovani APcka  
{ ngx\_string(“ap\_frpc\_controlserver”),

NGX\_HTTP\_MAIN\_CONF|NGX\_HTTP\_SRV\_CONF|NGX\_HTTP\_LOC\_CONF|NGX\_HTTP\_LIF\_CONF|NGX\_CONF\_TAKE1,  
ngx\_conf\_set\_str\_slot,  
NGX\_HTTP\_LOC\_CONF\_OFFSET,  
offsetof(ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t, controlserver),  
NULL },  
// server\_type pro ohlasovani na control server  
{ ngx\_string(“ap\_frpc\_servertype”),

NGX\_HTTP\_MAIN\_CONF|NGX\_HTTP\_SRV\_CONF|NGX\_HTTP\_LOC\_CONF|NGX\_HTTP\_LIF\_CONF|NGX\_CONF\_TAKE1,  
ngx\_conf\_set\_str\_slot,  
NGX\_HTTP\_LOC\_CONF\_OFFSET,  
offsetof(ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t, servertype),  
NULL },  
ngx\_null\_command  
};  
static ngx\_http\_module\_t ngx\_http\_ap\_frpc\_handler\_module\_ctx = {  
ngx\_http\_ap\_frpc\_add\_variables, /\* preconfiguration _/  
ngx\_http\_ap\_frpc\_init, /_ postconfiguration _/  
NULL, /_ create main configuration _/  
NULL, /_ init main configuration _/  
NULL, /_ create server configuration _/  
NULL, /_ merge server configuration _/  
ngx\_http\_ap\_frpc\_handler\_create\_loc\_conf, /_ create location  
configuration _/  
ngx\_http\_ap\_frpc\_handler\_merge\_loc\_conf /_ merge location  
configuration \*/  
};

ngx\_module\_t ngx\_http\_ap\_frpc\_handler\_module = {  
NGX\_MODULE\_V1,  
&ngx\_http\_ap\_frpc\_handler\_module\_ctx, /\* module context _/  
ngx\_http\_ap\_frpc\_handler\_commands, /_ module directives _/  
NGX\_HTTP\_MODULE, /_ module type _/  
NULL, /_ init master _/  
NULL, /_ init module _/  
NULL, /_ init process _/  
NULL, /_ init thread _/  
NULL, /_ exit thread _/  
NULL, /_ exit process _/  
NULL, /_ exit master \*/  
NGX\_MODULE\_V1\_PADDING  
};

static ngx\_http\_variable\_t ngx\_http\_ap\_frpc\_vars[] = {  
// $ap\_frc\_log - informace k zalogovani  
{ ngx\_string(“ap\_frpc\_log”), NULL, ngx\_http\_ap\_frpc\_log\_variable,  
0,

NGX\_HTTP\_VAR\_CHANGEABLE|NGX\_HTTP\_VAR\_NOCACHEABLE|NGX\_HTTP\_VAR\_NOHASH, 0  
},  
{ ngx\_string(“ap\_frpc\_service”), NULL,  
ngx\_http\_ap\_frpc\_service\_variable, 0,

NGX\_HTTP\_VAR\_CHANGEABLE|NGX\_HTTP\_VAR\_NOCACHEABLE|NGX\_HTTP\_VAR\_NOHASH, 0  
},

```
{ ngx_null_string, NULL, NULL, 0, 0, 0 }

```

};

static ngx\_int\_t ngx\_http\_ap\_frpc\_add\_variables(ngx\_conf\_t \*cf) {  
ngx\_http\_variable\_t \*var, \*v;

```
for (v = ngx_http_ap_frpc_vars; v->name.len; v++) {
    var = ngx_http_add_variable(cf, &v->name, v->flags);
    if (var == NULL) {
        return NGX_ERROR;
    }

    var->get_handler = v->get_handler;
    var->data = v->data;
}

return NGX_OK;

```

}

// inicializace promenne $ap\_frpc\_log  
static ngx\_int\_t ngx\_http\_ap\_frpc\_log\_variable(ngx\_http\_request\_t \*r,  
ngx\_http\_variable\_value\_t \*v, uintptr\_t data) {  
ngx\_http\_ap\_frpc\_ctx\_t \*ctx;

```
ctx = reinterpret_cast<ngx_http_ap_frpc_ctx_t*>
    (ngx_http_get_module_ctx(r, ngx_http_ap_frpc_handler_module));

if (ctx == NULL) {
    v->not_found = 1;
    return NGX_OK;
}

v->len = ctx->frpc_log.len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = ctx->frpc_log.data;

return NGX_OK;

```

}

static ngx\_int\_t ngx\_http\_ap\_frpc\_service\_variable(ngx\_http\_request\_t  
\*r,  
ngx\_http\_variable\_value\_t \*v, uintptr\_t data) {  
ngx\_http\_ap\_frpc\_ctx\_t \*ctx;

```
ctx =

```

reinterpret\_cast\<ngx\_http\_ap\_frpc\_ctx\_t\*\>(ngx\_http\_get\_module\_ctx(r,  
ngx\_http\_ap\_frpc\_handler\_module));

```
if (ctx == NULL) {
    v->not_found = 1;
    return NGX_OK;
}

v->len = ctx->frpc_service.len;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = ctx->frpc_service.data;

return NGX_OK;

```

}

static ngx\_int\_t ngx\_http\_ap\_frpc\_init(ngx\_conf\_t \*cf){  
ngx\_http\_handler\_pt \*h;  
ngx\_http\_core\_main\_conf\_t \*cmcf;

```
cmcf =

```

reinterpret\_cast\<ngx\_http\_core\_main\_conf\_t\*\>(ngx\_http\_conf\_get\_module\_main\_conf(cf,  
ngx\_http\_core\_module));

```
h =

```

reinterpret\_cast\<ngx\_http\_handler\_pt\*\>(ngx\_array\_push(&cmcf-\>phases[NGX\_HTTP\_ACCESS\_PHASE].handlers));

```
if (h == NULL) {
    return NGX_ERROR;
}

*h = ngx_http_ap_frpc_access_handler;
return NGX_OK;

```

}

static ngx\_int\_t ngx\_http\_ap\_frpc\_access\_handler(ngx\_http\_request\_t \*r)  
{  
if (r-\>method != NGX\_HTTP\_POST) {  
return NGX\_DECLINED;  
}  
ngx\_int\_t rc = ngx\_http\_read\_client\_request\_body(r,  
ngx\_http\_ap\_frpc\_request);  
if (rc == NGX\_ERROR || rc \>= NGX\_HTTP\_SPECIAL\_RESPONSE) {  
return rc;  
}  
if (rc == NGX\_AGAIN) {  
return NGX\_DONE;  
}

```
return NGX_DECLINED;

```

}

static void ngx\_http\_ap\_frpc\_request(ngx\_http\_request\_t _r) {  
ngx\_http\_ap\_frpc\_ctx\_t ctx;  
ctx =  
reinterpret\_cast\<ngx\_http\_ap\_frpc\_ctx\_t\>(ngx\_http\_get\_module\_ctx(r,  
ngx\_http\_ap\_frpc\_handler\_module));  
if (ctx == NULL) {  
ctx =  
reinterpret\_cast\<ngx\_http\_ap\_frpc\_ctx\_t_\>(ngx\_pcalloc(r-\>pool,  
sizeof(ngx\_http\_ap\_frpc\_handler\_module)));  
ngx\_http\_set\_ctx(r, ctx, ngx\_http\_ap\_frpc\_handler\_module);  
}  
ngx\_http\_ap\_frpc(r, ctx);  
ngx\_http\_finalize\_request(r, NGX\_DONE);  
}

static void \*ngx\_http\_ap\_frpc\_handler\_create\_loc\_conf(ngx\_conf\_t \*cf)  
{  
ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t \*conf;

```
conf =

```

reinterpret\_cast\<ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t\*\>(ngx\_pcalloc(cf-\>pool,

```
            sizeof(ngx_http_ap_frpc_handler_loc_conf_t)));
if (conf == NULL) {
    return NGX_CONF_ERROR;
}
conf->enable = NGX_CONF_UNSET_UINT;
conf->gsidindex = NGX_CONF_UNSET;

return conf;

```

}

static char \*ngx\_http\_ap\_frpc\_handler\_merge\_loc\_conf(ngx\_conf\_t \*cf,  
void \*parent, void \*child)  
{  
ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t _prev =  
reinterpret\_cast\<ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t_\>(parent);  
ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t _conf =  
reinterpret\_cast\<ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t_\>(child);

```
ngx_conf_merge_value(conf->enable, prev->enable, 0);
ngx_conf_merge_str_value(conf->services, prev->services, "other");
ngx_conf_merge_str_value(conf->controlserver, prev->controlserver,

```

“[http://skcontrol:3337/RPC2](http://skcontrol:3337/RPC2)”);  
ngx\_conf\_merge\_str\_value(conf-\>servertype, prev-\>servertype,  
“targeting-advertproxy”);  
if (conf-\>gsidindex == NGX\_CONF\_UNSET) {  
conf-\>gsidindex = prev-\>gsidindex;  
}  
return NGX\_CONF\_OK;  
}

static char \*ngx\_http\_ap\_frpc\_gsid(ngx\_conf\_t \*cf, ngx\_command\_t \*cmd,  
void \*conf) {  
ngx\_str\_t \*value;

```
ngx_http_ap_frpc_handler_loc_conf_t *lcf =

```

reinterpret\_cast\<ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t\*\>(conf);  
value = reinterpret\_cast\<ngx\_str\_t\*\>(cf-\>args-\>elts);

```
if (value[1].data[0] == '$') {
    value[1].len--;
    value[1].data++;

    lcf->gsidindex = ngx_http_get_variable_index(cf, &value[1]);
    if (lcf->gsidindex == NGX_ERROR) {
        return reinterpret_cast<char*>(NGX_CONF_ERROR);
    }
}

return NGX_CONF_OK;

```

}

static ngx\_int\_t ngx\_http\_ap\_frpc\_parse\_header(ngx\_http\_request\_t \*r) {  
if (r-\>headers\_in.content\_type != NULL) {  
if (ngx\_strncmp(r-\>headers\_in.content\_type-\>value.data,  
“text/xml”, 8) == 0) {  
return 0;  
} else if (ngx\_strncmp(r-\>headers\_in.content\_type-\>value.data,  
“application/x-frpc”, 18) == 0) {  
return 1;  
}  
}  
return -1;  
}

static ngx\_int\_t ngx\_http\_ap\_frpc\_read\_body(ngx\_http\_request\_t \*r,  
ngx\_str\_t \*request\_body) {  
u\_char \*p;  
size\_t len;  
ngx\_buf\_t \*buf, \*next;  
ngx\_chain\_t \*cl;

```
if (r->request_body == NULL
    || r->request_body->bufs == NULL
    || r->request_body->temp_file)
{
    return 0;
}

cl = r->request_body->bufs;
buf = cl->buf;

if (cl->next == NULL) {
    len = buf->last - buf->pos;
    p = reinterpret_cast<u_char*>(ngx_pnalloc(r->pool, len));
    if (p == NULL) {
        return NGX_ERROR;
    }
    ngx_cpystrn(p, cl->buf->pos, len);

    ngx_str_set(request_body, p);
    request_body->len = len;
    return len;
}

next = cl->next->buf;
len = (buf->last - buf->pos) + (next->last - next->pos);

p = reinterpret_cast<u_char*>(ngx_pnalloc(r->pool, len));
if (p == NULL) {
    return NGX_ERROR;
}

request_body->data = p;

p = ngx_cpymem(p, buf->pos, buf->last - buf->pos);
ngx_memcpy(p, next->pos, next->last - next->pos);

request_body->len = len;
return len;

```

}

static ngx\_int\_t ngx\_http\_ap\_frpc\_response(ngx\_http\_request\_t \*r,  
std::string \*response\_body, bool binary) {  
ngx\_buf\_t \*b;  
ngx\_chain\_t out;

```
b = reinterpret_cast<ngx_buf_t*>(ngx_pcalloc(r->pool,

```

sizeof(ngx\_buf\_t)));  
if (b == NULL) {  
return NGX\_HTTP\_INTERNAL\_SERVER\_ERROR;  
}  
b-\>pos =  
reinterpret\_cast\<u\_char\*\>(const\_cast\<char\*\>(response\_body-\>c\_str()));  
b-\>last =  
reinterpret\_cast\<u\_char\*\>(const\_cast\<char\*\>(response\_body-\>c\_str())) +  
response\_body-\>size();

```
b->memory = 1;
b->last_buf = 1;
out.buf = b;
out.next = NULL;

ngx_str_t content_type;
if (!binary) {
    ngx_str_set(&content_type, "text/xml");
} else {
    ngx_str_set(&content_type, "application/x-frpc");
}
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = response_body->size();
r->headers_out.content_type.len = content_type.len;
r->headers_out.content_type.data = content_type.data;
r->keepalive = 0; // Connection: close

ngx_int_t rc = ngx_http_send_header(r);

if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
    return rc;
}

ngx_http_output_filter(r, &out);
ngx_pfree(r->pool, b);
return NGX_OK;

```

}

static ngx\_int\_t ngx\_http\_ap\_frpc(ngx\_http\_request\_t \*r,  
ngx\_http\_ap\_frpc\_ctx\_t \*ctx)  
{  
ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t _cglcf;  
cglcf =  
reinterpret\_cast\<ngx\_http\_ap\_frpc\_handler\_loc\_conf\_t_\>(ngx\_http\_get\_module\_loc\_conf(r,

```
            ngx_http_ap_frpc_handler_module));

if (cglcf->enable == 0) {
    ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,

```

“ap\_frpc disable”);  
return NGX\_OK;  
} else {  
ngx\_log\_debug(NGX\_LOG\_DEBUG\_HTTP, r-\>connection-\>log, 0,  
“ap\_frpc enable”);  
}  
// Priznak zda je spojeni xml-rpc (0) nebo fastrpc (1)  
bool binary = false;  
if (ngx\_http\_ap\_frpc\_parse\_header(r) == -1) {  
return NGX\_ERROR;  
}  
else {  
binary = (ngx\_http\_ap\_frpc\_parse\_header(r) == 1);  
}  
// precteni tela pozadavku  
ngx\_str\_t request\_body;  
ngx\_int\_t bodyLength = ngx\_http\_ap\_frpc\_read\_body(r,  
&request\_body);  
if (bodyLength \<= 0) {  
return NGX\_ERROR;  
}

… process the request and generate response  
}

Body of my ngx\_int\_t ngx\_http\_ap\_frpc function is not important because  
it is not carried out in /public/RPC2 location  
Debug log:  
2011/08/10 13:25:55 [debug] 22105#0: \*39 http cl:1886 max:1048576  
2011/08/10 13:25:55 [debug] 22105#0: \*39 rewrite phase: 2  
2011/08/10 13:25:55 [debug] 22105#0: \*39 ap\_filter enable  
2011/08/10 13:25:55 [debug] 22105#0: \*39 rewrite phase: 3  
2011/08/10 13:25:55 [debug] 22105#0: \*39 post rewrite phase: 4  
2011/08/10 13:25:55 [debug] 22105#0: \*39 access phase: 5  
2011/08/10 13:25:55 [debug] 22105#0: \*39 posix\_memalign:  
000000000E1143E0:4096 @16  
2011/08/10 13:25:55 [debug] 22105#0: \*39 http read client request body  
2011/08/10 13:25:55 [debug] 22105#0: \*39 recv: fd:43 1354 of 1886  
2011/08/10 13:25:55 [debug] 22105#0: \*39 http client request body recv  
1354  
2011/08/10 13:25:55 [debug] 22105#0: \*39 http client request body rest  
532  
2011/08/10 13:25:55 [debug] 22105#0: \*39 recv: fd:43 -1 of 532  
2011/08/10 13:25:55 [debug] 22105#0: \*39 recv() not ready (11: Resource  
temporarily unavailable)  
2011/08/10 13:25:55 [debug] 22105#0: \*39 http client request body recv  
-2  
2011/08/10 13:25:55 [debug] 22105#0: \*39 http client request body rest  
532  
2011/08/10 13:25:55 [debug] 22105#0: \*39 event timer add: 43:  
60000:1312975615320  
2011/08/10 13:25:55 [debug] 22105#0: \*39 post event 00002BA47A8CE148  
2011/08/10 13:25:55 [debug] 22105#0: \*39 delete posted event  
00002BA47A8CE148  
2011/08/10 13:25:55 [debug] 22105#0: \*39 http run request:  
“/public/RPC2?”  
2011/08/10 13:25:55 [debug] 22105#0: \*39 http read client request body  
2011/08/10 13:25:55 [debug] 22105#0: \*39 recv: fd:43 532 of 532  
2011/08/10 13:25:55 [debug] 22105#0: \*39 http client request body recv  
532  
2011/08/10 13:25:55 [debug] 22105#0: \*39 http client request body rest  
0  
2011/08/10 13:25:55 [debug] 22105#0: \*39 event timer del: 43:  
1312975615320  
2011/08/10 13:25:55 [debug] 22105#0: \*39 ap\_frpc disable  
2011/08/10 13:25:55 [debug] 22105#0: \*39 http finalize request: -4,  
“/public/RPC2?” a:1, c:2  
2011/08/10 13:25:55 [debug] 22105#0: \*39 http request count:2 blk:0

and a part of configuration:

```
    location /public/RPC2 {
        proxy_set_header X-Forwarded-For

```

$proxy\_add\_x\_forwarded\_for;  
proxy\_pass [http://localhost:3352](http://localhost:3352);  
}

```
    # RPC rozhrani AP pro hitovani
    location /RPC2HIT {
        ap_frpc on;
        ap_frpc_gsid $ap_filter_cookie_sid;
        ap_frpc_services "hp|email";
        ap_frpc_controlserver http://skcontrol:3337/RPC2;
    }

```

Posted at Nginx Forum:

> **[Re: Proxying request - HTTP/1.1 502 Bad Gateway](https://forum.nginx.org/read.php?2,213552,213628#msg-213628)**
>
> Hello, we have problem with proxying some RPC (POST) requests to a backend server. When a request is sent without proxy to the backend directly everrything is OK. So problem is probably caused by nginx proxy. When the request is sent from the same...
