njs-0.5.3
Dmitry Volyntsev
xeioex at nginx.com
Tue Mar 30 19:31:47 UTC 2021
Hello,
I'm glad to announce a new release of NGINX JavaScript module (njs).
This release focuses on extending the modules functionality.
Notable new features:
- js_var directive. The directive creates an nginx variable writable
from njs. The variable is not overwritten after a redirect unlike
variables created with the set directive.
This is especially useful in situations where some directive value
depends on the result of a subrequest.
The following example illustrates the case where Authorization header
is processed by a HTTP endpoint which returns foo value as a result.
This result is passed as a header to the backend.
: nginx.conf:
: js_import main.js;
:
: js_var $foo;
: ..
:
: location /secure/ {
: auth_request /set_foo;
:
: proxy_set_header Foo $foo;
: proxy_pass http://backend;
: }
:
: location =/set_foo {
: internal;
: js_content main.set_foo;
: }
:
: main.js:
: function set_foo(r) {
: ngx.fetch('http://127.0.0.1:8080', {headers: {Authorization: r.headersIn.Authorization}})
: .then(reply => {
: r.variables.foo = reply.headers.get('foo');
: r.return(200);
: });
: }
:
: export default {set_foo};
You can learn more about njs:
- Overview and introduction: http://nginx.org/en/docs/njs/
- Presentation: https://youtu.be/Jc_L6UffFOs
- Using node modules with njs:
http://nginx.org/en/docs/njs/node_modules.html
- Writing njs code using TypeScript definition files:
http://nginx.org/en/docs/njs/typescript.html
Feel free to try it and give us feedback on:
- Github: https://github.com/nginx/njs/issues
- Mailing list: http://mailman.nginx.org/mailman/listinfo/nginx-devel
Changes with njs 0.5.3 30 Mar 2021
nginx modules:
*) Feature: added the "js_var" directive.
More information about the nginx
mailing list