在nginx中使用Lua将请求传递到FastCGI

使用编译了Lua支持的nginx,我们该如何制作一种类似于nginx的fastcgi_pass指令的子请求到FastCGI处理程序?

我想做的是像这样的事情:

location = / {
    access_by_lua '
        res = ngx_fastcgi.pass("backend")
    ';
}

(显然,这行不通。)

我正在研究 HttpLuaModule ,在那里我看到ngx_fastcgingx.location.capture的提到,这显然会做出

针对使用磁盘文件目录配置的其他位置的非阻塞内部请求,或任何其他nginx C模块,如 ... ngx_fastcgi,...

但是随后在ngx_fastcgi的链接之后,带我到了HttpFastcgiModule,它只解释了nginx指令,而不是可用于Lua脚本的命令。ngx.location.capture是要使用的正确函数吗?(顺便说一句,这些请求将是对localhost进行的,只是在不同的端口上,如9000或9001。)

我如何在nginx中使用Lua将请求转发或进行子请求到FastCGI端点?

点赞
用户151741
用户151741

使用 ngx.location.capture() 方法执行预定义位置块的子请求。然后,在此位置块中执行外部的 FastCGI 请求。因为子请求本身并不是网络操作,而是纯在 nginx 的 C 环境中执行的,所以开销非常小。而且,因为 FastCGI 请求和其他类似 "proxy_pass" 的请求都是基于事件的,nginx 可以作为一个高效的中介运行。

例如,你可以这样写:

location / {
  access_by_lua '
    response = ngx.location.capture("/my-subrequest-handler")
    if response.status == 404 then
       return ngx.exit(401) -- can't find/authenticate user, refuse request
    end

    ngx.say(response.status)
  ';

  # other nginx config stuff here as necessary--perhaps another fastcgi_pass
  # depending upon the status code of the response above...
}

location = /my-subrequest-handler {
  internal; # this location block can only be seen by nginx subrequests
  fastcgi_pass localhost:9000; # or some named "upstream"
  fastcgi_pass_request_body       off; # send client request body upstream?
  fastcgi_pass_request_headers    off; # send client request headers upstream?
  fastcgi_connect_timeout         100ms; # optional; control backend timeouts
  fastcgi_send_timeout            100ms; # same
  fastcgi_read_timeout            100ms; # same
  fastcgi_keep_conn               on; # keep request alive
  include                         fastcgi_params;
}

在上面的例子中,即使我执行了 "/my-subrequest-handler" 的子请求,实际传递给 FastCGI 进程的 URL 仍然是最初调用 nginx 的 HTTP 客户端请求的 URL。

请注意,ngx.location.capture 是一个同步但非阻塞操作,这意味着你的代码执行会在收到响应之前停止,但 nginx 工作者在此期间可以执行其他操作。

使用 Lua 可以在 nginx 管道的任何阶段修改请求和响应,这些功能非常酷。例如,你可以通过添加标头、删除标头甚至转换正文来“更改”原始请求。也许调用者想要使用 XML,但上游应用程序只理解 JSON,我们可以在调用上游应用程序时进行转换。

Lua 不是默认内置在 nginx 中的。它是一个第三方模块,必须编译安装。有一种叫做 OpenResty 的 nginx 可以内置 Lua+LuaJIT 和其他一些可能需要的模块。

2013-08-22 18:18:32