Editor_ – The blog post titled “Introduction to the NGINX JavaScript Module” redirects here. The post has been updated to use the NGINX JavaScript Module directives and features supported as of April 2021._

The NGINX JavaScript module (njs) became generally available as a stable module in NGINX Open Source 1.11.10 and  NGINX Plus R12. [The module was originally called nginScript, and that name appears in some older posts.] We have been working steadily on NGINX JavaScript since its launch in  September 2015, adding the features and language support included in the stable module.

NGINX JavaScript is a unique JavaScript implementation for NGINX and NGINX Plus, designed specifically for server‑side use cases and per‑request processing. It extends NGINX configuration syntax with JavaScript code in order to implement sophisticated configuration solutions.

The use cases are extensive, especially as the NGINX JavaScript module is available for both HTTP and TCP/UDP protocols. Sample use cases for NGINX JavaScript include:

  • Generating custom log formats with values not available from regular NGINX variables (for example,  diagnostic logging)
  • Modifying responses from proxied servers
  • Implementing custom authentication schemes (for example,  OAuth 2.0 token introspection)
  • Parsing TCP/UDP protocols for application‑level sticky sessions (for example, [MQTT load balancing])

Before discussing NGINX JavaScript in more detail, let’s first address two common misconceptions.

NGINX JavaScript Is Not Lua

The NGINX community has created several programmatic extensions over the years. At the time of writing, Lua is the most popular of these; it’s available as a  module for NGINX and a  supported, prebuilt third‑party dynamic module for NGINX Plus. The Lua module and add‑on libraries provide deep integration with the NGINX core and a rich set of functionality, including a driver for Redis.

Lua is a powerful scripting language. It, however, remains fairly niche in terms of adoption and is not typically found in the “skillset toolbox” of the frontend developer or DevOps engineer.

NGINX JavaScript does not seek to replace Lua and it will be some time before NGINX JavaScript has a comparable level of functionality. The goal of NGINX JavaScript is to provide programmatic configuration solutions to the widest possible community by using a popular programming language.

#blog #tech #javascript #nginx javascript module

Harnessing Power & Convenience of JavaScript for Each Request with NGINX JavaScript Module
1.30 GEEK