njs is an nginx module that extends the server's functionality through JavaScript scripting, enabling the creation of custom server-side logic and more.
See examples for more njs use cases.
To use njs in nginx:
install njs scripting language
create an njs script file, for example, http.js
.
See Reference
for the list of njs properties and methods.
function hello(r) { r.return(200, "Hello world!"); } export default {hello};
in the nginx.conf
file, enable
ngx_http_js_module module
and specify the
js_import
directive
with the http.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.