将 NGINX 日志发送到 Graylog

将 NGINX 日志发送到 Graylog

我正在尝试使用 graylog 收集 nginx 错误和访问日志,我认为一切都配置正确,但 Graylog 没有从 NGINX 收到任何信息(Graylog 和 NGINX 位于 docker 容器中,并且都在同一个网络中)

我使用 nginx/1.13.5 和 Graylog 2.4.0,并且我使用此内容包在graylog上

这是我的 nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

    log_format graylog2_json escape=json '{ "timestamp": "$time_iso8601", '
                 '"remote_addr": "$remote_addr", '
                 '"remote_user": "$remote_user", '
                 '"body_bytes_sent": $body_bytes_sent, '
                 '"request_time": $request_time, '
                 '"status": $status, '
                 '"request": "$request", '
                 '"request_method": "$request_method", '
                 '"host": "$host",'
                 '"upstream_cache_status": "$upstream_cache_status",'
                 '"upstream_addr": "$upstream_addr",'
                 '"http_x_forwarded_for": "$http_x_forwarded_for",'
                 '"http_referrer": "$http_referer", '
                 '"http_user_agent": "$http_user_agent" }';

    access_log syslog:server=graylog:12301,facility=local0,tag=nginx,severity=info graylog2_json;
    error_log  syslog:server=graylog:12302,facility=local0,tag=nginx,severity=error warn;
    #error_log stderr;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
daemon off;

如果我尝试使用 nc,我的 graylog 会收到消息

echo -n "test message" | nc -u -w1 graylog 12301

谢谢!

答案1

我建议你收集一些调试信息:正如你所说你的网络工作


  1. Graylog 主机上的 TCP 转储数据包:

如果 Nginx 与 Graylog 位于同一主机上:sudo tcpdump udp -n -vv port 12301 -i lo -X

如果 Nginx 与 Graylog 位于不同的主机上:sudo tcpdump udp -n -vv port 12301 -X


tcp转储

  1. 如果网络层工作正常,并且您可以看到如图所示的数据包,请转到 Graylog 输入并检查您拥有哪种输入

    [a] 必须原始/纯文本 UDP(如果你有 GELF UDPGraylog 将过滤您的消息作为Nginx 以 Syslog 格式而不是 Json 格式发送日志

    [b] 你将拥有网络 IO 不为 0

    [C]港口(您的情况是 12301)和知识产权需要与 Nginx 配置相同


graylog 输入

  1. 如果你拥有所有这些,你将在 Graylog 中找到来自 Nginx 的 RAW 消息:

<190>Jul.26.16:12:07.graylog.nginx:.{."timestamp":."2018-07-26T16:12:07+03:00",,"remote_addr":."xx.xxxxx",,"body_bytes_sent":.4277,,"request_time":.0.005,,"response_status":.200,,"request":."POST./api/cluster/metrics/multiple.HTTP/1.1",,"request_method":."POST",,"host":."xx.xxxxx",,"upstream_cache_status":."-",,"upstream_addr":."xx.xxxxx",,"http_x_forwarded_for":."xx.xxxxx",,"http_referrer":."https://xx.xxxxx/system/inputs",."http_user_agent":."xx.xxxxxxx.xxxxxxx.xxxxxxx.xxxxx",."http_version":."HTTP/1.1",."nginx_access":.true.}


  1. 使用 Graylog 输入提取器从伪 Syslog RAW 消息中提取 JSON:

提取器示例:

{
  "extractors": [
    {
      "title": "Extract from Pseudo-Syslog a JSON",
      "extractor_type": "regex_replace",
      "converters": [],
      "order": 0,
      "cursor_strategy": "cut",
      "source_field": "message",
      "target_field": "message",
      "extractor_config": {
        "replacement": "$1",
        "regex": "^.*?(\\{.*?\\})$"
      },
      "condition_type": "none",
      "condition_value": ""
    }
  ],
  "version": "2.4.6"
}

当您使用“内容包”时,如果您进行导入导出,则需要在来自“内容包”(顺序:0)的所有其他规则之前添加该规则

添加规则后,您将获得来自 Nginx 的清晰 JSON 日志,所有其他工作将执行“内容包”


  1. 检查你的 Nginx 配置

ngnix.conf示例:

log_format graylog_json '{ "timestamp": "$time_iso8601", "remote_addr": "$remote_addr", "body_bytes_sent": $body_bytes_sent, "request_time": $request_time, "response_status": $status, "request": "$request", "request_method    ": "$request_method", "host": "$host", "upstream_cache_status": "$upstream_cache_status", "upstream_addr": "$upstream_addr", "http_x_forwarded_for": "$http_x_forwarded_for", "http_referrer": "$http_referer", "http_user_agent": "$h    ttp_user_agent", "http_version": "$server_protocol", "nginx_access": true }';
access_log syslog:server=graylog:5555 graylog_json;

希望您能从这些步骤中受益

相关内容