I'm using nginx as a reverse proxy and I have been trying to write an nginx module that would process incoming requests, and if it likes certain HTTP headers present in the request, nginx would allow the request to reach the protected server (behind the nginx proxy). Now, I've successfully implemented the header processing, but I am stuck at figuring out how to forward the request to the server.
So far, I have looked into sub-requests but none of the code I try (or copied from existing modules such as ngx_http_addition_filter_module
!) seems to work. Either I get stuck in a loop where 100+ sub-requests get fired or nothing happens at all. The code I have been trying to use:
static ngx_int_t ngx_http_my_own_handler(ngx_http_request_t *r)
{
// some request processing here
// ...
// now issue the sub-request
ngx_http_request_t *sr;
ngx_http_post_subrequest_t *ps;
ps = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
if (ps == NULL) {
return NGX_ERROR;
}
ps->handler = ngx_http_foo_subrequest_done;
ps->data = "foo";
// re-use the request URI to try to forward it
return ngx_http_subrequest(r, &r->uri, &r->args, &sr, ps, NGX_HTTP_SUBREQUEST_CLONE);
}
And the ngx_http_foo_subrequest_done
handler looks like this:
ngx_int_t ngx_http_foo_subrequest_done(ngx_http_request_t *r, void *data, ngx_int_t rc)
{
char *msg = (char *) data;
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, "done subrequest r:%p msg:%s rc:%i", r, msg, rc);
return rc;
}
Please advise what I am doing wrong!