我正在尝试从 nginx 生成 JSON 日志。
我知道像 this one 这样的解决方案但我想记录的一些字段包括需要正确转义的用户生成的输入(如 HTTP header )。
我知道 2011 年 10 月和 2008 年 5 月的 nginx 更改日志条目说:
*) Change: now the 0x7F-0x1F characters are escaped as \xXX in an
access_log.
*) Change: now the 0x00-0x1F, '"' and '\' characters are escaped as \xXX
in an access_log.
但这仍然没有帮助,因为 \xXX
是 invalid in a JSON string .
我也看过 the HttpSetMiscModule具有 set_quote_json_str
指令的模块,但这似乎只是在没有帮助的字符串周围添加 \x22
。
对于其他从 nginx 以 JSON 格式登录的解决方案有什么想法吗?
最佳答案
最后看起来我们有一个很好的方法来使用没有任何模块的 vanilla nginx 来做到这一点。只需定义:
log_format json_combined escape=json
'{'
'"time_local":"$time_local",'
'"remote_addr":"$remote_addr",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"status": "$status",'
'"body_bytes_sent":"$body_bytes_sent",'
'"request_time":"$request_time",'
'"http_referrer":"$http_referer",'
'"http_user_agent":"$http_user_agent"'
'}';
注意 escape=json 是在 nginx 1.11.8 中添加的。 http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
关于json - 如何从 nginx 生成 JSON 日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25049667/