Kloudless Blog

Kloudless Unified APIs enable you to code once and integrate many


This blog post shows how to use Nginx, Lua, Logstash and Elasticsearch to log, store, and analyze HTTP request and response metadata. The metadata can either be generated by Nginx or by any upstream service that Nginx proxies to. For example, the metadata could be the request time, response size, or any information the upstream returns to log for purposes such as analytics.

First a small example web service behind an Nginx server generates some data that we’d like to analyze via Elasticsearch. Nginx, with the help of Lua, parses and logs statistics returned by the example upstream service as it receives the response to send back to the client. This leads us to Logstash, which parses these logged statistics and forwards them to Elasticsearch for storage and analysis.

This post doesn’t cover how to install each individual tool. Please refer to their respective documentation for more information on that process.

A small example upstream service

The example upstream service below uses Python 2.7 and flask. It exposes a single endpoint that adds an HTTP header to the response which contains additional metadata to log for later analysis.

Now that the service includes metadata in its HTTP response, Nginx can be configured to log it.

Logging with Nginx and Lua

Logstash works out of the box with structured JSON objects, so it’s ideal for Nginx to parse the upstream response’s HTTP headers and log the data in a JSON format. This avoids having to configure a Logstash pattern that matches a custom log line format.

Below is the first half of an Nginx configuration file, which sets up the format to log data in:

The configuration file above first specifies lua_package_path to ensure Nginx can load and cache the third-party JSON module in the init_by_lua directive.

It then defines a log format for Nginx’s access log that happens to be a JSON string template as well, with Nginx variable names that will later be interpolated with corresponding values.

Nginx 1.11.8+ also supports the option escape=json, although this shouldn’t be included if the data being logged is itself in a JSON format, such as an array or nested object. If that’s the case, it’s better to encode the JSON data via Lua to avoid it being escaped twice. For example, log the JSON array ['dp1', 'dp2', 'dp3'] as shown below:

Following the log format, the Nginx configuration specifies a server block to handle the incoming request and outgoing response. It sets default values for the metadata to log as well, to avoid errors if the metadata in the response is entirely absent. It’s also possible to define different log formats for individual servers or event locations, if the response data varies by location.

The location block includes the logic to retrieve the actual metadata and set the Nginx variables. Within it, the header_filter_by_lua block’s context has access to the upstream response HTTP response.

The Lua code above parses the raw JSON string received in the X-Service-Metadata header and assigns it to a local variable, metadata, as a Lua table. Individual fields can now easily be assigned to their corresponding Nginx variables.

Now that the metadata is being logged to the access log, Logstash can read and parse it to store in Elasticsearch.

Parsing and Forwarding with Logstash

Below is the Logstash configuration to handle the Nginx log entries:

Logstash’s json filter plugin easily parses the JSON log entries in the file. The original message field with the raw JSON string is no longer necessary after the parsed data is available in the event.

A date filter sets @timestamp to be the original message’s timestamp rather than when Logstash received the event. The configuration then removes the original timestamp field similar to the original message field.

The Logstash output sends filtered events to Elasticsearch using Logstash’s default index template and index-naming pattern. Please refer to the Elasticsearch plugin documentation for more information on configuring index-related settings.

Storage and Analysis with Elasticsearch

The default Elasticsearch configuration is sufficient for our example. With everything set up, the Elasticsearch Query API available on port 9200 enables us to query the Logstash indices. For example, to get a list of available indices:


An example query on the Logstash index:

How we use this setup

Kloudless provides a single API abstraction layer to access several different third-party APIs like Google Drive, Dropbox, and others with a single implementation. We log API requests to Elasticsearch for our own analytics as well as providing metrics to developers on our platform regarding their apps’ API usage. It’s also been an invaluable tool when troubleshooting API requests, or monitoring new releases to the platform.


Share this article:

Let’s get started. Start building for free today.