百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

njs

> DevOps
开源

用于 nginx 的 JavaScript 语言的子集

1.6K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于 nginx 的 JavaScript 语言的子集

NGINX JavaScript

NGINX JavaScript, also known as NJS, is a dynamic module for NGINX that enables the extension of built-in functionality using familiar JavaScript syntax. NJS supports modern JavaScript through its recommended QuickJS engine (ES2023). See JavaScript engines and compatibility for more details.

Table of Contents

  • How it works
  • JavaScript engines
  • Downloading and installing
    • Provisioning the NGINX package repository
    • Installing the NGINX JavaScript modules
    • Installed files and locations
  • Getting started with NGINX JavaScript
    • Verify NGINX is running
    • Enabling the NGINX JavaScript modules
    • Basics of writing .js script files
    • Reference of custom objects, methods, and properties
    • Example: Hello World
    • The NJS command line interface (CLI)
  • Building from source
    • Installing dependencies
    • Cloning the NGINX JavaScript GitHub repository
    • Building standalone command line interface utility (optional)
    • Cloning the NGINX GitHub repository
    • Building NGINX JavaScript as a module of NGINX
  • NGINX JavaScript technical specifications
    • Supported distributions
    • Supported deployment environments
    • Supported NGINX versions
    • Sizing recommendations
  • Asking questions, reporting issues, and contributing
  • Change log
  • License

How it works

NGINX JavaScript is provided as two dynamic modules for NGINX (ngx_http_js_module and ngx_stream_js_module) and can be added to any supported NGINX Open Source or NGINX Plus installation without recompilation.

The NJS module allows NGINX administrators to:

  • Add complex access control and security checks before requests reach upstream servers
  • Manipulate response headers
  • Write flexible, asynchronous content handlers, filters, and more!

See examples and our various projects developed with NJS:

https://github.com/nginxinc/nginx-openid-connect

Extends NGINX Plus functionality to communicate directly with OIDC-compatible Identity Providers, authenticating users and authorizing content delivered by NGINX Plus.

https://github.com/nginxinc/nginx-saml

Reference implementation of NGINX Plus as a service provider for SAML authentication.

https://github.com/nginxinc/njs-prometheus-module

Exposes Prometheus metrics endpoint directly from NGINX Plus.

[!TIP] NJS can also be used with the NGINX Unit application server. Learn more about NGINX Unit's Control API and how to define function calls with NJS.

JavaScript engines

NJS provides two interchangeable JavaScript engines, selectable with the js_engine directive:

  • QuickJS (recommended) — a modern engine compliant with ES2023. Enable it with js_engine qjs;. Building from source requires the QuickJS library (see Building from source). Read more: QuickJS engine support for NJS.
  • Built-in njs engine (deprecated since 1.0.0) — the original engine, a JavaScript subset compliant with ES5.1 (Strict Variant) plus a curated set of ES6 and newer extensions. It is currently the default and will continue to be supported and have its bugs fixed for the foreseeable future. We recommend migrating to QuickJS; eventually QuickJS will become the default, while the njs engine remains available as an option for compatibility.

See engine selection and compatibility for details.

Downloading and installing

Follow these steps to download and install precompiled NGINX and NGINX JavaScript Linux binaries. You may also choose to build the module locally from source code.

Provisioning the NGINX package repository

Follow this guide to add the official NGINX package repository to your system and install NGINX Open Source. If you already have NGINX Open Source or NGINX Plus installed, skip the NGINX installation portion in the last step.

Installing the NGINX JavaScript modules

Once the repository has been provisioned, you may install NJS by issuing the following command:

Ubuntu or Debian based systems

sudo apt install nginx-module-njs

RHEL, RedHat and its derivatives

sudo yum install nginx-module-njs

Alpine or similar systems

sudo apk add nginx-module-njs@nginx

SuSE, SLES or similar systems

sudo zypper install nginx-module-njs

[!TIP] The package repository includes an alternate module that enables debug symbols. Although not recommended for production environments, this module may be helpful when developing NJS-based configurations. To download and install the debug version of the module, replace the module name in the previous command with nginx-module-njs-dbg.

Installed files and locations

The package installation scripts install two modules, supporting NGINX http and stream contexts.

  • ngx_http_js_module

    This NJS module enables manipulation of data transmitted over HTTP.

  • ngx_stream_js_module

    This NJS module enables manipulation of data transmitted via stream protocols such as TCP and UDP.

By default, both modules are installed into the /etc/nginx/modules directory.

Getting started with NGINX JavaScript

Usage of NJS involves enabling the module, adding JavaScript files with defined functions, and invoking exported functions in NGINX configuration files.

Verify NGINX is running

NGINX JavaScript is a module for NGINX Open Source or NGINX Plus. If you haven't done so already, follow these steps to install NGINX Open Source or NGINX Plus. Once installed, ensure the NGINX instance is running and able to respond to HTTP requests.

Starting NGINX

Issue the following command to start NGINX:

sudo nginx

Verify NGINX is responding to HTTP requests

curl -I 127.0.0.1

You should see the following response:

HTTP/1.1 200 OK
Server: nginx/1.25.5

Enabling the NGINX JavaScript modules

Once installed, either (or both) NJS module(s) must be included in the NGINX configuration file. On most systems, the NGINX configuration file is located at /etc/nginx/nginx.conf by default.

Edit the NGINX configuration file

sudo vi /etc/nginx/nginx.conf

Enable dynamic loading of NJS modules

Use the load_module directive in the top-level (“main”) context to enable either (or both) module(s).

load_module modules/ngx_http_js_module.so;
load_module modules/ngx_stream_js_module.so;

Basics of writing .js script files

NJS script files are typically named with a .js extension and placed in the /etc/nginx/njs/ directory. They are usually comprised of functions that are then exported, making them available in NGINX configuration files.

Reference of custom objects, methods, and properties

NJS provides a collection of objects with associated methods and properties that are not part of ECMAScript definitions. See the complete reference to these objects and how they can be used to further extend and customize NGINX.

Example: Hello World

Here's a basic "Hello World" example.

example.js

The hello function in this file returns an HTTP 200 OK status response code along with the string "Hello World!", followed by a line feed. The function is then exported for use in an NGINX configuration file.

Add this file to the /etc/nginx/njs directory:

function hello(r) {
  r.return(200, "Hello world!\n");
}

export default {hello}

nginx.conf

We modify our NGINX configuration (/etc/nginx/nginx.conf) to import the JavaScript file and execute the function under specific circumstances.

# Load the ngx_http_js_module module
load_module modules/ngx_http_js_module.so;

events {}

http {
  # Set the path to our njs JavaScript files
  js_path "/etc/nginx/njs/";

  # Import our JavaScript file into the variable "main"
  js_import main from example.js;

  server {
    listen 80;

    location / {
      # Execute the "hello" function defined in our JavaScript file on all HTTP requests
      # and respond with the contents of our function.
      js_content main.hello;
    }
  }
}

For a full list of njs directives, see the ngx_http_js_module and ngx_stream_js_module module documentation pages.

[!TIP] A more detailed version of this and other examples can be found in the official njs-examples repository.

The NJS command line interface (CLI)

NGINX JavaScript installs with a command line interface utility. The interface can be opened as an interactive shell or used to process JavaScript syntax from predefined files or standard input. Since the utility runs independently, NGINX-specific objects such as HTTP and Stream are not available within its runtime.

Example usage of the interactive CLI

$ njs
>> globalThis
global {
  njs: njs {
    version: '0.8.4'
  },
  global: [Circular],
  process: process {
    argv: ['/usr/bin/njs'],
    env: {
      PATH: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
      HOSTNAME: 'f777c149d4f8',
      TERM: 'xterm',
      NGINX_VERSION: '1.25.5',
      NJS_VERSION: '0.8.4',
      PKG_RELEASE: '1~buster',
      HOME: '/root'
    }
  },
  console: {
    log: [Function: native],
    dump: [Function: native],
    time: [Function:

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Cjavascriptnginxnginx-complex-access-controlnginx-custom-scripting

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类DevOps
定价开源

> 相关工具

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理