ngx_http_js_module 指令

Directives js_content js_import js_include js_path js_set

简单说,Nginx NJS 就是在 Nginx 的配置文件中 /etc/nginx/nginx.conf 增加几条指令

  • js_content,执行其中 JS 内容并输出
  • js_include,指定特定的文件内的 JS 代码处理请求
  • js_set,设置特定的 JS 变量

上面这是 ngx_http_js_module 的使用方法,然而 ngx_stream_js_module 也是基本一样的,就是多了几个指令而已

  • js_access,Nginx Access 阶段执行的 JS 内容
  • js_filter,Nginx 输出的数据过滤阶段执行的 JS 内容
  • js_include,指定特定的文件内的 JS 代码处理请求
  • js_preread,Nginx Preread 阶段执行的 JS 内容
  • js_set,设置特定的 JS 变量

js_content

| Syntax: | **js_content** *function* | *module.function*; | | :——- | ———————————————— | | Default: | — | | Context: | location, limit_except |

Sets an njs function as a location content handler. Since 0.4.0, a module function can be referenced.

作为location 内容处理器(content handler),执行一个njs函数。

| Syntax: | **js_import** *module.js* | *export_name from module.js*; | | :——- | ———————————————————– | | Default: | — | | Context: | http |

This directive appeared in version 0.4.0.

Imports a module that implements location and variable handlers in njs. The export_name is used as a namespace to access module functions. If the export_name is not specified, the module name will be used as a namespace.

1
js_import http.js;

Here, the module name http is used as a namespace while accessing exports. If the imported module contains foo(), http.foo is used to refer to it.

Several js_import directives can be specified.

Syntax: **js_include** *file*;
Default:
Context: http

Specifies a file that implements location and variable handlers in njs:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
nginx.conf:
js_include http.js;
location   /version {
    js_content version;
}

http.js:
function version(r) {
    r.return(200, njs.version);
}

The directive is deprecated since 0.4.0, the js_import directive should be used instead.

Syntax: **js_path** *path*;
Default:
Context: http

This directive appeared in version 0.3.0.

Sets an additional path for njs modules.

| Syntax: | **js_set** *$variable* *function* | *module.function*; | | :——- | ——————————————————– | | Default: | — | | Context: | http |

Sets an njs function for the specified variable. Since 0.4.0, a module function can be referenced.