njs scripting languagenjs脚本语言
njs is a subset of the JavaScript language that allows extending nginx functionality. njs是允许扩展nginx功能的JavaScript语言的子集。njs is created in compliance with ECMAScript 5.1 (strict mode) with some ECMAScript 6 and later extensions. njs是根据ECMAScript 5.1(严格模式)和一些ECMAScript 6及更高版本的扩展创建的。The compliance is still evolving.合规性仍在发展中。
Download and install下载并安装Changes变化Reference参考文献Examples例子Compatibility兼容性Command-line interface命令行界面Tested OS and platforms经过测试的操作系统和平台
Writing njs code using TypeScript definition files使用TypeScript定义文件编写njs代码Using node modules with njs在njs中使用节点模块
Use cases用例
Complex access control and security checks in njs before a request reaches an upstream server在请求到达上游服务器之前,在njs中进行复杂的访问控制和安全检查Manipulating response headers操作响应头Writing flexible asynchronous content handlers and filters编写灵活的异步内容处理程序和筛选器
See examples and blog posts for more njs use cases.有关更多njs用例,请参阅示例和博客文章。
Basic HTTP Example基本HTTP示例
To use njs in nginx:要在nginx中使用njs:
-
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 thehttp.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.
Tested OS and platforms经过测试的操作系统和平台
- FreeBSD / amd64;
- Linux / x86, amd64, arm64, ppc64el;
- Solaris 11 / amd64;
- macOS / x86_64;
Presentation at nginx.conf 2018在nginx.conf 2018上的介绍