如何从 nginx 生成 JSON 日志?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25049667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:27:43  来源:igfitidea点击:

How to generate a JSON log from nginx?

jsonloggingnginx

提问by Jules Olléon

I'm trying to generate a JSON log from nginx.

我正在尝试从 nginx 生成 JSON 日志。

I'm aware of solutions like this onebut some of the fields I want to log include user generated input (like HTTP headers) which need to be escaped properly.

我知道像解决方案的这一个,但有些我想记录包括用户生成的输入(如HTTP头)需要被正确转义的字段。

I'm aware of the nginx changelog entries from Oct 2011 and May 2008 that say:

我知道 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.

but this still doesn't help since \xXXis invalid in a JSON string.

但因为这仍然没有帮助\xXX在一个JSON字符串无效

I've also looked at the HttpSetMiscModulemodule which has a set_quote_json_strdirective, but this just seems to add \x22around the strings which doesn't help.

我还查看具有set_quote_json_str指令的 HttpSetMiscModule模块,但这似乎只是在无用\x22的字符串周围添加。

Any idea for other solutions to log in JSON format from nginx?

知道从 nginx 登录 JSON 格式的其他解决方案吗?

回答by pva

Finally it looks like we have good way to do this with vanilla nginx without any modules. Just define:

最后,看起来我们有一个很好的方法来使用没有任何模块的 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"'
  '}';

Note that escape=jsonwas added in nginx 1.11.8. http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format

请注意,在 nginx 1.11.8 中添加了escape=jsonhttp://nginx.org/en/docs/http/ngx_http_log_module.html#log_format

回答by Seva Poliakov

You can try to use that one https://github.com/jiaz/nginx-http-json-log- addition module for Nginx.

您可以尝试使用那个https://github.com/jiaz/nginx-http-json-log- Nginx 的附加模块。

回答by Valeriy Solovyov

You can try to use:

您可以尝试使用:

PS: The if parameter (1.7.0) enables conditional logging. A request will not be logged if the condition evaluates to “0” or an empty string:

PS: if 参数 (1.7.0) 启用条件记录。如果条件评估为“0”或空字符串,则不会记录请求:

map $status $http_referer{
    ~\xXX  0;
    default 1;
}

access_log /path/to/access.log combined if=$http_referer;

It's a good idea to use a tool such as https://github.com/zaach/jsonlintto check your JSON data. You can test the output of your new logging format and make sure it's real-and-proper JSON.

使用https://github.com/zaach/jsonlint等工具检查 JSON 数据是个好主意。您可以测试新日志格式的输出,并确保它是真实正确的 JSON。