Nginx 提供静态响应消息体?

dan*_*bst 2 nginx

我想让 nginx 像特殊查询的快速响应器一样工作。

location /app {
  # server static response message body (10 bytes)
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?POST 请求需要它

Ale*_*Ten 5

您可以向return指令添加文本。

return 200 Text;
Run Code Online (Sandbox Code Playgroud)

二进制数据可以按字面意思放置。例如在 vim 中,我可以输入ctrl-v 001

return 200 "[.]";
#            ^--- \001 symbol here.
Run Code Online (Sandbox Code Playgroud)

结果:

$ curl -s http://localhost:2002/ | xxd
0000000: 5b01 5d                                  [.]
#          ^^ here it is
Run Code Online (Sandbox Code Playgroud)

但我认为最好提供静态二进制文件或使用嵌入式 lua 或 perl。例如使用 lua:

content_by_lua 'ngx.print("[\001]")';
Run Code Online (Sandbox Code Playgroud)

将给出与上面相同的结果,但 config 没有一些奇怪的不可打印字符。