On this page
njs 脚本语言
njs 是 JavaScript 语言的子集,它允许扩展 nginx 功能。 njs 是根据ECMAScript 5.1(严格模式)和某些ECMAScript 6及更高版本的 extensions 创建的。依从性仍然是evolving。
Use cases
在请求到达上游服务器之前,在 njs 中进行复杂的访问控制和安全检查
处理响应头
编写灵活的异步内容处理程序和过滤器
有关更多 njs 用例,请参见examples和blog posts。
基本 HTTP 示例
要在 nginx 中使用 njs:
function hello(r) {
r.return(200, "Hello world!");
}
export default {hello};
- 在
nginx.conf
文件中,启用ngx_http_js_module模块,并通过http.js
脚本文件指定js_import指令:
load_module modules/ngx_http_js_module.so;
events {}
http {
js_import http.js;
server {
listen 8000;
location / {
js_content http.hello;
}
}
}
还有一个独立的command lineUtil,可以独立于 nginx 进行 njs 开发和调试。
经过测试的 os 和平台
FreeBSD/amd64;
Linux/x86,amd64,arm64,ppc64el;
Solaris 11/amd64;
macOS/x86_64;
在 nginx.conf 2018 上的演讲
https://www.youtube.com/embed/Jc_L6UffFOs?modestbranding=1&rel=0&showinfo=0&color=white