nginx 日志 格式转换 问题(x22)

nginx 日志问题(x22)

# log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent $request_body "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

log_format main escape=json '[$time_iso8601]|$remote_addr|$request_method|$request|$status|$body_bytes_sent|$request_body|$request_time|$request_body|'
'"$http_referer"|"$http_user_agent"|$http_x_forwarded_for|'
'"$upstream_addr"|$upstream_response_time|'
'$upstream_cache_status|$scheme|$http_user_agent';


access_log logs/access.log main;

https://www.cnblogs.com/dance-walter/p/10635260.html

nginx 日志问题(x22)

问题:

1、request_body中含有中文时,nginx日志会转换为十六进制。
2、nginx记录问题

POST /xxxxx HTTP/1.1|200|4266|0.121|0.121|------------------------------4a74a6c5ef13x0Dx0AContent-Disposition: form-data; name=x22cityx22x0Dx0Ax0Dx0Ananjingx0Dx0A------------------------------4a74a6c5ef13x0Dx0AContent-Disposition: form-data; name=x22service_namex22x0Dx0Ax0Dx0AgdmmGoodsx0D------------------4a74a6c5ef13x0Dx0AContent-Disposition: form-data; name=x22canUseCouponx22x0Dx0Ax0Dx0A1x0Dx0A------------------------------4a74a6c5ef13--x0Dx0A|-|-|-|-|xxx|https

优化:

logstash为了能高效的处理各类日志,希望日志是一种特定结构存储的方式。


nginx默认日志格式:

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" $request_body';

问题日志

[25/Feb/2019:00:00:10 +0800]|192.168.10.19|POST /paas/callback HTTP/1.1|200|163|0.002|0.002|{x22rcx22:0,x22msgx22:x22successx22,x22transferratex22:x2245301x22}]}}

request_body中都是转换后十六进制,不易阅读.


直接提供可视化日志格式和解决十六进制的解决办法:

格式化数据:

###json格式:
    log_format log_json escape=json '{"timestamp": "$time_local",'
        '"remote_addr": "$remote_addr",'
        '"referer": "$http_referer",'
        '"request": "$request",'
        '"statu": "$status",'
        '"byte": "$body_bytes_sent",'
        '"agen": "$http_user_agent",'
        '"x_forwarde": "$http_x_forwarded_for",'
        '"up_addr": "$upstream_addr",'
        '"up_host": "$upstream_http_host",'
        '"up_resp_time": "$upstream_response_time",'
        '"request_time": "$request_time"}';


###自定义边界:
log_format main escape=json '[$time_iso8601]|$remote_addr|$request_method|$request|$status|$body_bytes_sent|$request_time|$request_body|'
                '"$http_referer"|"$http_user_agent"|$http_x_forwarded_for|'
                '"$upstream_addr"|$upstream_response_time|'
                '$upstream_cache_status|$scheme|$http_user_agent';
  • log_format :日志格式开头
  • main :日志名称
  • escape=json :nginx 1.11.8版本后才提供此参数。链接

如果版本低,要么升级,要么在logstash中使用ruby给做一次转化,可参考链接:https://github.com/logstash-plugins/logstash-codec-json/issues/2

问题2:

当nginx出现上述的报错,一定是数据发起端未正常编码(默认的发起请求的Content-Type为multipart/form-data)。
解决办法,在POST提交数据时encode数据(Content-Type:application/x-www-form-urlencoded)
链接:https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data/4073451#4073451

原文地址:https://www.cnblogs.com/lgj8/p/13724437.html