Njs: header in subrequest

Created on 18 Feb 2019  路  4Comments  路  Source: nginx/njs

Is it possible to add "headers" key in subrequest options?
For example:
{
args: "arg1=txt",
body: "request body",
method: "POST",
headers: {content_type: "application/json", authorization: "token", ...}
}

question resolved

Most helpful comment

@hongzhidao

Sounds great, but does it still rely upon current request, or is individual in process?

It still will be linked to the current request, because njs context is linked to it. But, it will not depend heavily on nginx internal structures.

All 4 comments

Hi @an0ma1ia,

Unfortunately, it is hard to correctly replace incoming headers in nginx. The main and the sub requests share incoming headers structure.

If you want to control the outgoing headers in the subrequest you can use the following directives in the subrequest location
1) proxy_set_header to set or override headers.
2) proxy_pass_request_headers off to drop all the headers of the main request.

In the future, we plan to introduce 'native' http client in njs, which will not rely upon nginx subrequests.

I use GET parameter to pass header value to the subrequest location where I set header by proxy_set_header. Exactly what you recommend. Works well but looks a bit strange.

Thank you for the clarification.

@xeioex

In the future, we plan to introduce 'native' http client in njs, which will not rely upon nginx subrequests.

Sounds great, but does it still rely upon current request, or is individual in process?

@hongzhidao

Sounds great, but does it still rely upon current request, or is individual in process?

It still will be linked to the current request, because njs context is linked to it. But, it will not depend heavily on nginx internal structures.

Was this page helpful?
0 / 5 - 0 ratings