Nginx中文文档
DocsHome
  • 贡献导引
  • 介绍
    • 安装 nginx
    • 从源码构建 nginx
    • 初学者指南
    • 管理指南
    • 控制 nginx
    • 连接处理方式
    • 设置哈希
    • 调试日志
    • 记录日志到 syslog
    • 配置文件度量单位
    • 命令行参数
    • Windows 下的 nginx
    • QUIC 和 HTTP/3 支持
    • nginx 如何处理请求
    • 服务器名称
    • 使用 nginx 作为 HTTP 负载均衡器
    • 配置 HTTPS 服务器
    • nginx 如何处理 TCP/UDP 会话
    • 关于 nginScript
  • 其他
    • Linux 软件包
  • How-To
    • 在 Win32 平台上使用 Visual C 构建 nginx
    • 设置 Amazon EC2 的 Nginx Plus 环境
    • 使用 DTrace pid 提供程序调试 nginx
    • 转换重写规则
    • WebSocket 代理
  • 开发
    • 贡献指南
    • 开发指南
  • 模块参考
  • 核心功能
    • HTTP
      • ngx_http_core_module
      • ngx_http_access_module
      • ngx_http_addition_module
      • ngx_http_api_module
      • ngx_http_auth_basic_module
      • ngx_http_auth_jwt_module
      • ngx_http_auth_request_module
      • ngx_http_autoindex_module
      • ngx_http_browser_module
      • ngx_http_charset_module
      • ngx_http_dav_module
      • ngx_http_empty_gif_module
      • ngx_http_f4f_module
      • ngx_http_fastcgi_module
      • ngx_http_flv_module
      • ngx_http_geo_module
      • ngx_http_geoip_module
      • ngx_http_grpc_module
      • ngx_http_gunzip_module
      • ngx_http_gzip_module
      • ngx_http_gzip_static_module
      • ngx_http_headers_module
      • ngx_http_hls_module
      • ngx_http_image_filter_module
      • ngx_http_index_module
      • ngx_http_js_module
      • ngx_http_keyval_module
      • ngx_http_limit_conn_module
      • ngx_http_limit_req_module
      • ngx_http_log_module
      • ngx_http_map_module
      • ngx_http_memcached_module
      • ngx_http_mirror_module
      • ngx_http_mp4_module
      • ngx_http_perl_module
      • ngx_http_proxy_module
      • ngx_http_random_index_module
      • ngx_http_realip_module
      • ngx_http_referer_module
      • ngx_http_rewrite_module
      • ngx_http_scgi_module
      • ngx_http_secure_link_module
      • ngx_http_session_log_module
      • ngx_http_slice_module
      • ngx_http_spdy_module(过时)
      • ngx_http_split_clients_module
      • ngx_http_ssi_module
      • ngx_http_ssl_module
      • ngx_http_status_module(过时)
      • ngx_http_stub_status_module
      • ngx_http_sub_module
      • ngx_http_upstream_module
      • ngx_http_upstream_conf_module
      • ngx_http_upstream_hc_module
      • ngx_http_userid_module
      • ngx_http_uwsgi_module
      • ngx_http_v2_module
      • ngx_http_xslt_module
    • Mail
      • ngx_mail_core_module
      • ngx_mail_auth_http_module
      • ngx_mail_proxy_module
      • ngx_mail_ssl_module
      • ngx_mail_imap_module
      • ngx_mail_pop3_module
      • ngx_mail_smtp_module
    • Stream
      • ngx_stream_core_module
      • ngx_stream_access_module
      • ngx_stream_geo_module
      • ngx_stream_geoip_module
      • ngx_stream_js_module
      • ngx_stream_keyval_module
      • ngx_stream_limit_conn_module
      • ngx_stream_log_module
      • ngx_stream_map_module
      • ngx_stream_proxy_module
      • ngx_stream_realip_module
      • ngx_stream_return_module
      • ngx_stream_split_clients_module
      • ngx_stream_ssl_module
      • ngx_stream_ssl_preread_module
      • ngx_stream_upstream_module
      • ngx_stream_upstream_hc_module
      • ngx_stream_zone_sync_module
    • 其他
      • ngx_google_perftools_module
由 GitBook 提供支持
在本页
  • 示例配置
  • 指令
  • js_include
  • hls_buffers
  • js_set
  • 请求与响应参数
  • 原文档
  1. 核心功能
  2. HTTP

ngx_http_js_module

上一页ngx_http_index_module下一页ngx_http_keyval_module

最后更新于5年前

ngx_http_js_module 模块用于在 中实现 location 和变量处理器 — 它是 JavaScript 语言的一个子集。

此模块不是默构建,可以使用 --add-module 配置参数与 nginScript 模块一起编译:

./configure --add-module=path-to-njs/nginx

可以使用以下命令克隆 nginScript 模块(需要 客户端):

hg clone http://hg.nginx.org/njs

该模块也可以构建为:

./configure --add-dynamic-module=path-to-njs/nginx

示例配置

js_include http.js;

js_set $foo     foo;
js_set $summary summary;

server {
    listen 8000;

    location / {
        add_header X-Foo $foo;
        js_content baz;
    }

    location /summary {
        return 200 $summary;
    }
}

http.js 文件:

function foo(req, res) {
    req.log("hello from foo() handler");
    return "foo";
}

function summary(req, res) {
    var a, s, h;

    s = "JS summary\n\n";

    s += "Method: " + req.method + "\n";
    s += "HTTP version: " + req.httpVersion + "\n";
    s += "Host: " + req.headers.host + "\n";
    s += "Remote Address: " + req.remoteAddress + "\n";
    s += "URI: " + req.uri + "\n";

    s += "Headers:\n";
    for (h in req.headers) {
        s += "  header '" + h + "' is '" + req.headers[h] + "'\n";
    }

    s += "Args:\n";
    for (a in req.args) {
        s += "  arg '" + a + "' is '" + req.args[a] + "'\n";
    }

    return s;
}

function baz(req, res) {
    res.headers.foo = 1234;
    res.status = 200;
    res.contentType = "text/plain; charset=utf-8";
    res.contentLength = 15;
    res.sendHeader();
    res.send("nginx");
    res.send("java");
    res.send("script");

    res.finish();
}

指令

js_include

-

说明

语法

js_include file;

默认

——

上下文

http

指定一个在 nginScript 中实现 location 和变量处理器的文件。

hls_buffers

-

说明

语法

js_content function;

默认

——

上下文

location、limit_except

将 nginScript 函数设置为 location 内容处理器。

js_set

-

说明

语法

js_set $variable function;

默认

——

上下文

http

提示

该指令在 1.5.12 版本中出现

为指定变量设置 nginScript 函数。

请求与响应参数

每个 HTTP nginScript 处理器接收两个参数,请求和响应。

请求对象具有以下属性:

  • uri

    请求的当前 URI,只读

  • method

    请求方法,只读

  • httpVersion

    HTTP 版本,只读

  • remoteAddress

    客户端地址,只读

  • headers{}

    请求头对象,只读

    例如,可以使用语法 headers['Header-Name'] 或 headers.Header_name 来访问 Header-Name 头

  • args{}

    请求参数对象,只读

  • variables{}

    nginx 变量对象,只读

  • log(string)

    将 string 写入错误日志

响应对象具有以下属性:

  • status

    响应状态,可写

  • headers{}

    响应头对象

  • contentType

    响应的 Content-Type 头字段值,可写

  • contentLength

    响应的 Content-Length 头字段值,可写

响应对象具有以下方法:

  • sendHeader()

    将 HTTP 头发送到客户端

  • send(string)

    将部分响应体的发送给客户端

  • finish()

    完成向客户端发送响应

原文档

http://nginx.org/en/docs/http/ngx_http_js_module.html
nginScript
仓库
Mercurial
指令
js_include
js_content
js_set
请求与响应参数
动态形式