Nginx – Send NGINX logs to Graylog

graylognginxsyslog

I'm trying to collect nginx error and access logs with graylog, I think everything is correctly configured, but Graylog receive nothing from NGINX
(Graylog & NGINX are in docker containers and both are in the same network)

I use nginx/1.13.5 & Graylog 2.4.0 and I use this content pack on graylog

Here is my 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;

If I try with nc, my graylog receive the message

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

Thanks!

Best Answer

I suggest you collect some debug info: As you said your network work


  1. TCP Dump packets on the Graylog host:

if Nginx on the same host as Graylog: sudo tcpdump udp -n -vv port 12301 -i lo -X

if Nginx on a different host as Graylog: sudo tcpdump udp -n -vv port 12301 -X


tcp dump

  1. If the network level works and you can see packets like on the picture go to Graylog Inputs and check which kind of input you have

    [a] It needs to be Raw/Plaintext UDP (if you have GELF UDP Graylog will filter your messages as Nginx sends logs in Syslog format and not in Json

    [b] You will have Network IO different from 0

    [c] Port (12301 in your case) and IP needs to be the same as in Nginx configuration


graylog input

  1. If you have all this you will find a RAW message from Nginx in your Graylog:

<190>Jul.26.16:12:07.graylog.nginx:.{."timestamp":."2018-07-26T16:12:07+03:00",."remote_addr":."xx.x.x.xxx",."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.x.x.xxx",."upstream_cache_status":."-",."upstream_addr":."xx.x.x.xxx",."http_x_forwarded_for":."xx.x.x.xxx",."http_referrer":."https://xx.x.x.xxx/system/inputs",."http_user_agent":."xx.x.x.xxxxx.x.x.xxxxx.x.x.xxxxx.x.x.xxx",."http_version":."HTTP/1.1",."nginx_access":.true.}


  1. Extract form pseudo-Syslog RAW message a JSON using Graylog Input Extractor:

Extractor Example:

{
  "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"
}

As you use "content pack" you need to add the rule before all others that cames from "content pack" (order: 0) if you do import export

After adding the rule, you will have clear JSON log from Nginx, all other work will do "content pack"


  1. Check your Nginx config

ngnix.conf example:

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;

Hope, following all this steps you will find useful

Related Topic