send access logs in json to Elasticsearch using rsyslog

Here is a simple example of how to send well-formatted JSON access logs directly to the Elasticsearch server.

It is as simple as Nginx (it could be any webserver) sends the access logs using UDP to the rsyslog server, which then sends well-formatted JSON data to the Elasticsearch server.

No other server program like logstash is used. The data is transformed in rsyslog and it is passed through a couple of modules to ensure the JSON is valid and Elasticsearch would not complain (and missing logs entry!).

  1. Nginx to send access logs using UDP to the rsyslog server.
  2. rsyslog server to accept UDP messages.
  3. rsyslog server transforms the web-server access logs from the Nginx server to JSON.
  4. rsyslog server sends the validated JSON to the Elasticsearch server.

The configuration and the commands are tested on CentOS 7, CentOS 8 and Ubuntu 18 LTS (just replace yum with apt).

STEP 1) Nginx to send access logs using UDP to the rsyslog server.

It is simple enough to send Nginx’ access logs to a UDP server (local or remote) there are two articles here: nginx remote logging to UDP rsyslog server (CentOS 7) and syslog – UDP local to rsyslog and send remote with TCP and compression. For simplicity, Nginx will send to the remote rsyslog server using UDP.
Instruct the Nginx to send access logs using UDP to the remote rsyslog server.
Define a new access log format in http serction:

        log_format mainJSON escape=json '@cee: {'

It is a valid JSON object, but sometimes in user agent or referer contain non-standard and not valid characters, so it breaks the JSON format, which may lead to problems in Elasticsearch (read ahead).

In a server section of Nginx configuration file /etc/nginx/nginx.conf:

server {
     access_log      /var/log/nginx/example.com_access.log main;
     access_log      syslog:server=,facility=local7,tag=nginx,severity=info mainJSON;

Multiple access_log directives are allowed.
Change the IP ( – the IP of the remote UDP rsyslog server) and the port (514) to the proper ones and reload the Nginx.

STEP 2) rsyslog server to accept UDP messages. And include the needed modules.

Nginx sends UDP messages, which rsyslog accepts. Here is the basic rsyslog configuration (/etc/rsyslog.conf), uncomment or add the following lines:

$ModLoad imudp
$UDPServerRun 514

module(load="mmjsonparse")          # for parsing CEE-enhanced syslog messages
module(load="omelasticsearch")      # for outputting to Elasticsearch

Or it could be used the new syntax (which is preferred):

input(type="imudp" port="514")

module(load="mmjsonparse")          # for parsing CEE-enhanced syslog messages
module(load="omelasticsearch")      # for outputting to Elasticsearch

The modules mmjsonparse, omelasticsearch and mmutf8fix will be used in the next sections.
They may not be installed by default like in CentOS 7/8. Here is how to install under CentOS 7:

yum install -y rsyslog-mmjsonparse rsyslog-elasticsearch rsyslog-mmutf8fix

Check out the server’s firewall and make a rule to accept UDP mesages on port 514 if there is a firewall (for more details check the links provided in STEP 1)):

firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="" port protocol="tcp" port="10514" accept"
firewall-cmd --reload is the IP of the client, i.e. the web-server IP.

STEP 3) rsyslog server transforms the web-server access logs from the Nginx server to JSON.

rsyslog parses the JSON and transforms it properly. Make a template (insert it in the RULE section of /etc/rsyslog.conf):

template(name="json-syslog" type="list" option.jsonf="on") {
        property(outname="@timestamp" name="timereported" dateFormat="rfc3339" format="jsonf")
        property(outname="host" name="hostname" format="jsonf")
        property(outname="severity" name="syslogseverity" caseConversion="upper" format="jsonf" datatype="number")
        property(outname="facility" name="syslogfacility" format="jsonf" datatype="number")
        property(outname="syslog-tag" name="syslogtag" format="jsonf")
        property(outname="source" name="app-name" format="jsonf" onEmpty="null")
        property(outname="vhost" name="$!vhost" format="jsonf" datatype="string" onEmpty="null")
        property(outname="remote_addr" name="$!remote_addr" format="jsonf" datatype="string" onEmpty="null")
        property(outname="time_iso8601" name="$!time_iso8601" format="jsonf" datatype="string" onEmpty="null")
        property(outname="request_uri" name="$!request_uri" format="jsonf" datatype="string" onEmpty="null")
        property(outname="request_length" name="$!request_length" format="jsonf" datatype="number" onEmpty="null")
        property(outname="request_method" name="$!request_method" format="jsonf" datatype="string" onEmpty="null")
        property(outname="request_time" name="$!request_time" format="jsonf" datatype="number" onEmpty="null")
        property(outname="server_port" name="$!server_port" format="jsonf" datatype="number" onEmpty="null")
        property(outname="server_protocol" name="$!server_protocol" format="jsonf" datatype="string" onEmpty="null")
        property(outname="ssl_protocol" name="$!ssl_protocol" format="jsonf" datatype="string" onEmpty="null")
        property(outname="status" name="$!status" format="jsonf" datatype="number" onEmpty="null")
        property(outname="bytes_sent" name="$!bytes_sent" format="jsonf" datatype="number" onEmpty="null")
        property(outname="http_referer" name="$!http_referer" format="jsonf" datatype="string" onEmpty="null")
        property(outname="http_user_agent" name="$!http_user_agent" format="jsonfr" datatype="string" onEmpty="null")
        property(outname="upstream_response_time" name="$!upstream_response_time" format="jsonf" datatype="string" onEmpty="null")
        property(outname="upstream_addr" name="$!upstream_addr" format="jsonf" datatype="string" onEmpty="null")
        property(outname="upstream_connect_time" name="$!upstream_connect_time" format="jsonf" datatype="string" onEmpty="null")
        property(outname="upstream_cache_status" name="$!upstream_cache_status" format="jsonf" datatype="string" onEmpty="null")
        property(outname="tcpinfo_rtt" name="$!tcpinfo_rtt" format="jsonf" datatype="number" onEmpty="null")
        property(outname="tcpinfo_rttvar" name="$!tcpinfo_rttvar" format="jsonf" datatype="number" onEmpty="null")

This template will be used to transform each UDP message from the Nginx access logs. The mmutf8fix will analyze the UDP access log message and it will fix problems with non-standard characters and it si mandatory to use this module because messages may not be accepted by the Elasticsearch server. Then the module mmjsonparse kicks in and parses each message so we can use the above template to build our valid JSON object (option.jsonf=”on” will create an JSON object, which the property directive adds a pair of name/value. The value is from the original and parsed already JSON object of the message by the module mmjsonparse). Add the actions in an IF selecting only the remote IPs (not local, because we do not want the local syslog mesages).

if ($fromhost-ip != "" ) then {

The IF will include one more action shown in the next section.

STEP 4)rsyslog server sends the validated JSON to the Elasticsearch server.

Use action with type “omelasticsearch” (to activate the Elasticsearch mode) and the above template to send the transformed JSON object to the Elasticsearch server. Here is the final and full IF rule located at the top of the RULE section in /etc/rsyslog.conf (right after the above template):

if ($fromhost-ip != "" ) then {
    action(type="omelasticsearch" server="" serverport="9200" template="json-syslog" searchIndex="rsyslog-index" dynSearchIndex="on" errorfile="/var/log/omelasticsearch.log")
& stop

Change the “server” with the IP of your Elasticsearch server (this is the IP of the Elasticsearch server – A error log file is inlcuded to track the errors. Each message is a valid UTF-8 JSON object and the errors are written in “/var/log/omelasticsearch.log“. It is a goog idea to check this files frequently.

The JSON object

@cee: {
"http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36","upstream_response_time":"",

Leave a Reply

Your email address will not be published. Required fields are marked *