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

本文探讨了nginx日志中request_body含有中文时转换为十六进制的问题,以及如何通过调整log_format实现日志结构化。提出了使用escape=json参数解决十六进制编码,并分享了解决POST请求Content-Type问题的链接。
摘要由CSDN通过智能技术生成

# 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;

nginx 日志问题(\x22)

问题:

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

2、nginx记录问题

POST /xxxxx HTTP/1.1|200|4266|0.121|0.121|------------------------------4a74a6c5ef13\x0D\x0AContent-Disposition: form-data; name=\x22city\x22\x0D\x0A\x0D\x0Ananjing\x0D\x0A------------------------------4a74a6c5ef13\x0D\x0AContent-Disposition: form-data; name=\x22service_name\x22\x0D\x0A\x0D\x0AgdmmGoods\x0D\------------------4a74a6c5ef13\x0D\x0AContent-Disposition: form-data; name=\x22canUseCoupon\x22\x0D\x0A\x0D\x0A1\x0D\x0A------------------------------4a74a6c5ef13--\x0D\x0A|-|-|-|-|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|{\x22rc\x22:0,\x22msg\x22:\x22success\x22,\x22transferrate\x22:\x2245301\x22}]}}

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版本后才提供此参数。链接

问题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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值