m0_68170889 2023-04-13 23:53 采纳率: 30%
浏览 92
已结题

elk7版本中kibana上nginx日志索引模板配置

  • 写回答

9条回答 默认 最新

  • 2301_77534089 2023-04-14 00:15
    关注

    首先,在Logstash中使用grok模板来解析nginx日志,将解析后的字段存储到Elasticsearch。grok模板可以按照需要自定义,这里可以使用grok模板库中的nginx模板。例如:

    
    
    filter {
      grok {
        match => { "message" => "%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] \"%{WORD:verb} %{NOTSPACE:request} HTTP/%{NUMBER:httpversion}\" %{NUMBER:response} (?:%{NUMBER:bytes}|-) \"%{NOTSPACE:referrer}\" \"%{DATA:agent}\"" }
        overwrite => [ "message" ]
      }
    }
    
    在这个例子中,使用grok模板来解析nginx日志中的各个字段,并将解析后的字段存储在Elasticsearch中。
    
    其次,在Kibana中配置索引模板,让Kibana能够正确地解析存储在Elasticsearch中的数据。在Kibana中创建索引模式时,需要指定一个索引模板。Kibana中可以选择使用自定义索引模板,这里可以使用Elasticsearch官方提供的nginx索引模板。例如:
    
    {
      "index_patterns": ["nginx-*"],
      "mappings": {
        "properties": {
          "clientip": {
            "type": "ip"
          },
          "ident": {
            "type": "keyword"
          },
          "auth": {
            "type": "keyword"
          },
          "timestamp": {
            "type": "date",
            "format": "dd/MMM/yyyy:HH:mm:ss Z"
          },
          "verb": {
            "type": "keyword"
          },
          "request": {
            "type": "text"
          },
          "httpversion": {
            "type": "keyword"
          },
          "response": {
            "type": "integer"
          },
          "bytes": {
            "type": "long"
          },
          "referrer": {
            "type": "text"
          },
          "agent": {
            "type": "text"
          }
        }
      },
      "settings": {
        "index": {
          "number_of_shards": 1,
          "number_of_replicas": 0
        }
      }
    }
    
    在这个例子中,使用Elasticsearch官方提供的nginx索引模板,指定了nginx索引模板的映射关系。这样,Kibana就可以正确地解析存储在Elasticsearch中的nginx日志数据,并进行可视化处理。
    
    最后,在Logstash中使用output插件将解析后的数据写入到Elasticsearch中,并确保Logstash和Elasticsearch之间的通信正常。例如:
    
    output {
      elasticsearch {
        hosts => ["http://localhost:9200"]
        index => "nginx-%{+YYYY.MM.dd}"
      }
    }
    

    在这个例子中,使用output插件将解析后的数据写入到Elasticsearch中的nginx索引中,并指定了索引名称为nginx-加上当前日期。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月18日
  • 创建了问题 4月13日