https://github.com/nginx/njs Skip to content Navigation Menu Toggle navigation Sign in * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + GitHub Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions By size + Enterprise + Teams + Startups By industry + Healthcare + Financial services + Manufacturing By use case + CI/CD & Automation + DevOps + DevSecOps * Resources Topics + AI + DevOps + Security + Software Development + View all Explore + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Enterprise + Enterprise platform AI-powered developer platform Available add-ons + Advanced Security Enterprise-grade security features + GitHub Copilot Enterprise-grade AI features + Premium Support Enterprise-grade 24/7 support * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up Reseting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} nginx / njs Public * Notifications You must be signed in to change notification settings * Fork 148 * Star 1.1k A subset of JavaScript language to use in nginx nginx.org/en/docs/njs/ License BSD-2-Clause license 1.1k stars 148 forks Branches Tags Activity Star Notifications You must be signed in to change notification settings * Code * Issues 48 * Pull requests 2 * Actions * Security * Insights Additional navigation options * Code * Issues * Pull requests * Actions * Security * Insights nginx/njs This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. master BranchesTags Go to file Code Folders and files Name Name Last commit Last commit message date Latest commit History 2,410 Commits .github .github auto auto external external nginx nginx src src test test ts ts utils utils .gitignore .gitignore .hgignore .hgignore .hgtags .hgtags CHANGES CHANGES CODE_OF_CONDUCT.md CODE_OF_CONDUCT.md CONTRIBUTING.md CONTRIBUTING.md LICENSE LICENSE NGINX-js-1660x332.png NGINX-js-1660x332.png README.md README.md SECURITY.md SECURITY.md SUPPORT.md SUPPORT.md configure configure View all files Repository files navigation * README * Code of conduct * BSD-2-Clause license * Security Project Status: Active - The project has reached a stable, usable state and is being actively developed. Community Support NGINX JavaScript Banner 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. The NJS language is a subset of JavaScript, compliant with ES5 (ECMAScript 5.1 Strict Variant) with some ES6 (ECMAScript 6) and newer extensions. See compatibility for more details. Table of Contents * How it works * 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. 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 http/hello.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: native], timeEnd: [Function: native] }, print: [Function: native] } >> Example usage of the non-interactive CLI $ echo "2**3" | njs -q 8 Building from source The following steps can be used to build NGINX JavaScript as a dynamic module to be integrated into NGINX or a standalone binary for use as a command line interface utility. Important To build the module for use with NGINX, you will also need to clone, configure and build NGINX by following the steps outlined in this document. Installing dependencies Most Linux distributions will require several dependencies to be installed in order to build NGINX and NGINX JavaScript. The following instructions are specific to the apt package manager, widely available on most Ubuntu/Debian distributions and their derivatives. Installing compiler and make utility sudo apt install gcc make Installing dependency libraries sudo apt install libpcre3-dev zlib1g-dev libssl-dev libxml2-dev libxslt-dev Warning This is the minimal set of dependency libraries needed to build NGINX and NJS. Other dependencies may be required if you choose to build NGINX with additional modules. Monitor the output of the configure command discussed in the following sections for information on which modules may be missing. Cloning the NGINX JavaScript GitHub repository Using your preferred method, clone the NGINX JavaScript repository into your development directory. See Cloning a GitHub Repository for additional help. https://github.com/nginx/njs.git Building the standalone command line interface utility (Optional) The following steps are optional and only needed if you choose to build NJS as a standalone utility. Install dependencies To use the NJS interactive shell, you will need to install the libedit-dev library sudo apt install libedit-dev Configure and build Run the following commands from the root directory of your cloned repository: ./configure Build NGINX JavaScript: make The utility should now be available at /build/njs. See The NJS Command Line Interface (CLI) for information on usage. Cloning the NGINX GitHub repository Clone the NGINX source code repository in a directory outside of the previously cloned NJS source repository. https://github.com/nginx/nginx.git Building NGINX JavaScript as a module of NGINX To build NGINX JavaScript as a dynamic module, execute the following commands from the NGINX source code repository's root directory: auto/configure --add-dynamic-module=/nginx Warning By default, this method will only build the ngx_http_js_module module. To use NJS with the NGINX Stream module, you'll need to enable it during the configure step so it builds with the NGINX binary. Doing so will automatically compile the ngx_stream_js_module module when NJS is added to the build. One way of accomplishing this is to alter the configure step to: auto/configure --with-stream --add-dynamic-module=/nginx Compile the module make Tip To build NGINX with NGINX JavaScript embedded into a single binary, alter the configure step to the following: auto/configure --add-module=/nginx Install module If built as a dynamic module(s), the NGINX JavaScript module(s) will be available in the /objs/ directory. The module (s) can then be copied to an existing NGINX installation and enabled. See Enabling the NGINX JavaScript Modules for details. Install compiled NGINX and NGINX JavaScript binaries Alternatively, you may choose to install the built NGINX and NGINX JavaScript binaries by issuing the following command: Important If built into the NGINX binary as a standard (not dynamic) module, this will be the easiest method of installation make install By default, the NGINX binary will be installed into /usr/local/nginx/ sbin/nginx. The NGINX JavaScript module(s) will be copied to /usr/ local/nginx/modules/. NGINX JavaScript technical specifications Technical specifications for NJS are identical to those of NGINX. Supported distributions See Tested Operating Systems and Platforms for a complete list of supported distributions. Supported deployment environments * Container * Public cloud (AWS, Google Cloud Platform, Microsoft Azure) * Virtual machine Supported NGINX versions NGINX JavaScript is supported by all NGINX Open Source versions starting with nginx-1.14 and all NGINX Plus versions starting with NGINX Plus R15. Asking questions, reporting issues, and contributing We encourage you to engage with us. Please see the Contributing guide for information on how to ask questions, report issues and contribute code. Change log See our release page to keep track of updates. License 2-clause BSD-like license --------------------------------------------------------------------- Additional documentation available at: https://nginx.org/en/docs/njs/ (c)2024 F5, Inc. All rights reserved. https://www.f5.com/products/nginx About A subset of JavaScript language to use in nginx nginx.org/en/docs/njs/ Resources Readme License BSD-2-Clause license Code of conduct Code of conduct Security policy Security policy Activity Custom properties Stars 1.1k stars Watchers 52 watching Forks 148 forks Report repository Releases 66 tags Contributors 26 * @xeioex * @igorsysoev * @lexborisov * @hongzhidao * @VBart * @drsm * @VadimZhestikov * @jirutka * @andrey-zelenkov * @thresheek * @arut * @TPXP * @devnexen * @osokin + 12 contributors Languages * C 87.0% * Perl 6.7% * JavaScript 5.9% * TypeScript 0.2% * Shell 0.1% * Python 0.1% Footer (c) 2024 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * Manage cookies * Do not share my personal information You can't perform that action at this time.