Welcome! Log In Create A New Profile


Have problems adding header 'Set-Cookie' to headers_out in my nginx sub request module

July 24, 2018 04:29AM
**nginx version: `1.10.3`**

Here's my code to add 'Set-Cookie' to headers:

void add_headers_out(ngx_http_request_t *r, char* cookies)
ngx_table_elt_t *h;
ngx_str_t k = ngx_string("Set-Cookie");
ngx_str_t v = ngx_string(cookies);
h = ngx_list_push(&r->headers_out.headers);
if(h == NULL)
return ;
h->hash = ngx_hash_key_lc(k.data, k.len);
h->key.len = k.len;
h->key.data = k.data;
h->value.len = v.len;
h->value.data = v.data;
When I call `add_headers_out` in my parent request handler:

static void multipost_post_handler(ngx_http_request_t *r)
///////// fill up headers and body
//// body
int bodylen = body.len;
ngx_buf_t *b = ngx_create_temp_buf(r->pool, bodylen);
b->pos = body.data;
b->last = b->pos + bodylen;
b->last_buf = 1;
ngx_chain_t out;
out.buf = b;
out.next = NULL;

//// headers
r->headers_out.content_type = myctx->content_type;
r->headers_out.content_length_n = bodylen;
r->headers_out.status = myctx->status_code;

// myctx->cookie1: "PHPSESSID=1f74a78647e192496597c240de765d45;"
add_headers_out(r, myctx->cookie1);

// Test: checking additional headers by iterating headers_out.headers
// returns: "Set-Cookie : PHPSESSID=1f74a78647e192496597c240de765d45;"

// Send response to client
r->connection->buffered |= NGX_HTTP_WRITE_BUFFERED;
ngx_int_t ret = ngx_http_send_header(r);
ret = ngx_http_output_filter(r, &out);
ngx_http_finalize_request(r, ret);
return ;
It seems no problem in my code, but when I use my nginx module as a reverse proxy module to some sites, I find `Set-Cookie` is different. For example, I can only see some small part of original `Set-Cookie: PHPSES(then go with nothing)` through chrome. I do not know what cause that problem. Thanks for helping!
Subject Author Posted

Have problems adding header 'Set-Cookie' to headers_out in my nginx sub request module

hcnhcn012 July 24, 2018 04:29AM

Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 109
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready