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|------------------------------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版本后才提供此參數。鏈接

如果版本低,要么升級,要么在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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM