json - 如何从 nginx 生成 JSON 日志?

我正在尝试从 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

https://stackoverflow.com/questions/25049667/

相关文章:

json - 将导入的 json 数据导入数据框

php - 带有双引号的json解析错误

python - python中的 pretty-print json(pythonic方式)

python - 在 Python 中将 JSON 转换为字符串

java - 如何将 POJO 转换为 JSON,反之亦然?

java - 在 Java 中将 JSON 转换为 XML

java - Retrofit2 安卓 : Expected BEGIN_ARRAY but was

python - 不是 JSON 可序列化的

json - postman - 如何将全局变量传递到 JSON 正文

javascript - SyntaxError : JSON. 解析:JSON 数据的第 1 行第