input {
  file {
    path => "/usr/local/openresty/nginx/logs/access.log"
    start_position => "beginning"
    sincedb_path => "/dev/null"
  }
}

filter {
  grok {
    match => {
      "message" => '%{IPORHOST:client_ip} \[%{HTTPDATE:request_timestamp}\] "%{WORD:http_method} %{URIPATHPARAM:request} %{DATA:http_version}" %{DATA:server_name} %{NUMBER:status} %{NUMBER:body_bytes_sent} "%{DATA:http_referer}" "%{DATA:http_user_agent}" %{IPORHOST:custom_host} %{DATA:upstream_addr} %{DATA:upstream_response_length} %{NUMBER:request_time} %{DATA:upstream_response_time} %{DATA:upstream_cache_status} %{DATA:upstream_status} %{DATA:scheme} %{DATA:$ssl_protocol} %{NUMBER:bytes_sent}'
    }
  }

  if [upstream_response_time] == "-" {
    mutate {
      replace => { "upstream_response_time" => "0" }
    }
  }

  if [upstream_response_length] == "-" {
    mutate {
      replace => { "upstream_response_length" => "0" }
    }
  }

  if [upstream_status] == "-" {
     mutate {
       replace => { "upstream_status" => "0" }
     }
   }

  mutate {
    convert => {
      "status" => "integer"
      "body_bytes_sent" => "integer"
      "upstream_response_length" => "integer"
      "request_time" => "float"
      "upstream_response_time" => "float"
      "upstream_status" => "integer"
      "bytes_sent" => "integer"
    }
  }

  date {
    match => ["request_timestamp", "dd/MMM/yyyy:HH:mm:ss Z"]
    target => "request_timestamp"
  }
}

output {
  stdout { codec => rubydebug }
  elasticsearch {
    hosts => ["103.8.215.107:34005"]
    index => "nginx_access_logs_%{+YYYYMMdd}"
    document_type => "_doc"
    user => "elastic"
    password => "613q7Rv13yoZ65MNVmIq3tl1"
    template_name => "nginx_access_logs"
    template_overwrite => true
    manage_template => true
  }
}
