nginx JavaScript module (original) (raw)
njs is an nginx module that extends the server's functionality through JavaScript scripting, enabling the creation of custom server-side logic and more.
- Download and install
- Changes
- Reference
- JavaScript Engine
- Examples
- Security
- Compatibility
- Command-line interface
- Understanding preloaded objects
- Tested OS and platforms
- ngx_http_js_module
- ngx_stream_js_module
- Writing njs code using TypeScript definition files
- Using node modules with njs
Use cases
- Complex access control and security checks in njs before a request reaches an upstream server
- Manipulating response headers
- Writing flexible asynchronous content handlers and filters
See examplesfor more njs use cases.
Basic HTTP Example
To use njs in nginx:
- install njs scripting language
- create an njs script file, for example,
http.js
. See Referencefor the list of njs properties and methods.function hello(r) {
r.return(200, "Hello world!");
}export default {hello};
- in the
nginx.conf
file, enablengx_http_js_module module and specify thejs_importdirective with thehttp.js
script file:load_module modules/ngx_http_js_module.so;
events {}
http {
js_import http.js;server {
listen 8000;
location / { js_content http.hello; }
}
}
There is also a standalone command line utility that can be used independently of nginx for njs development and debugging.
Tested OS and platforms
- FreeBSD / amd64;
- Linux / x86, amd64, arm64, ppc64el;
- Solaris 11 / amd64;
- macOS / x86_64;