一、目标
通过kibana方便查询nginx中的响应时间
二、搭建过程
本文的架构是:Filebeat+kafka+Logstash+ES+kibana,具体搭建请参照其他文章。版本是elasticsearch 7.4。
新增配置如下:
1、nginx日志格式修改
为了能让es识别nginx响应时间数据,成为独立的字段,需要把nginx日志改为json输出。
log_format main '{"@timestamp":"$time_iso8601",'
'"trace":"$upstream_http_ctx_transaction_id",'
'"log":"log",'
'"remote_addr":"$remote_addr",'
'"request":"$request",'
'"http_user_agent":"$http_user_agent",'
'"bytes_sent":$bytes_sent,'
'"request_time":$request_time,'
'"upstream_response_time":"$upstream_response_time",'
'"upstream_addr":"$upstream_addr",'
'"scheme":"$scheme",'
'"server_addr":"$server_addr",'
'"http_host":"$http_host",'
'"host":"$host",'
'"uri":"$uri",'
'"http_x_forwarded_for":"$http_x_forwarded_for",'
'"http_referer":"$http_referer",'
'"status":"$status"}';
重启nginx, 是日志配置生效。
2、filebeat
修改filebeat.yml配置文件,在需要显示为json的输入类型中添加一下配置:
json.message_key: event
json.keys_under_root: true
json.add_error_key: true
重启filebeat:
service filebeat restart
3、kibana配置
因为有新的字段加入,所以要刷新nginx的索引模式
三、效果
查看
搜索条件,大于1秒的请求: