njs runtime and module loading
Where JavaScript fits in Nginx
njs is an optional extension that lets the proxy invoke JavaScript for selected events. It is not a Node.js server running beside Nginx and does not provide a browser DOM. A configured handler receives an Nginx-specific request or session object. Prefer a plain directive when it already expresses the requirement; use code for a small transformation that would otherwise make the configuration difficult to read. The diagram shows one content handler, not a separate network service.
Client request → Nginx location → JavaScript handler → Nginx responseChoose the module and engine
The HTTP and stream JavaScript modules are different extensions. Install a matching trusted package for the exact Nginx build in an approved lab. A package version for Nginx is not the version of njs. Inspect package metadata, module paths and ABI compatibility; nginx -V alone is insufficient to identify loaded dynamic modules. The current engine documentation recommends QuickJS for new configurations; the original njs engine is deprecated since module release 1.0.0. Engine selection was introduced in 0.8.6; check that QuickJS support is actually built. For this chapter the staged lab requires a compatible module at 1.0.0 or later with QuickJS enabled, not a claim that the local binary supports it.
# Main context; replace with verified package path.load_module /verified/path/ngx_http_js_module.so;# Inside http { ... } on a compatible build:# js_engine qjs;Import and export a handler
Save the following as a JavaScript module named greetings.js. The default export exposes the function name used by the configuration. The request object r is supplied by Nginx; r.return completes the response rather than returning an ordinary JavaScript value to the caller. A successful request to /greeting produces HTTP 200 and the line Welcome to the lab. No client input is executed.
function greeting(r) { r.headersOut["Content-Type"] = "text/plain"; r.return(200, "Welcome to the lab\n");}export default { greeting };Attach the handler to a location
The partial configuration belongs inside http, after the correct module is loaded. It links a module alias to the function above. js_import resolves the module source; js_content selects a response handler. A conventional return directive at the same endpoint can terminate processing earlier, so avoid mixing competing content handlers. The companion lab includes a complete template. Never load a learner-uploaded script into a shared proxy.
js_engine qjs;js_import greetings from greetings.js;server { listen 127.0.0.1:19439; location = /greeting { js_content greetings.greeting; }}Practice and next steps
Draw the module, engine, location and handler boundaries. Explain why a Node test does not prove that the configured Nginx handler runs. Next: nginx-njs-request-transformation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
njs runtime and module loading
Distinguish njs from browser or Node.js JavaScript
What supplies r to an HTTP content handler?
What does js_import connect?
Which engine is recommended for new configurations by the reviewed current engine documentation?
What does r.return(200, "ok") do in a content handler?
Why is nginx -V alone insufficient?