# Kong HTTP Log Plugin Errors

**URL:** <https://discuss.konghq.com/t/kong-http-log-plugin-errors/445>\
**Category:** Questions\
**Created:** [January 24, 2018, 11:16pm UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445 "2018-01-24T23:16:35Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ross\_Sbriscia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/ross_sbriscia/32/118_2.png) [@Ross\_Sbriscia](https://discuss.konghq.com/u/Ross_Sbriscia)\
**Post date:** [January 24, 2018, 11:16pm UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/1 "2018-01-24T23:16:35Z")

</div>

We are doing some testing of the Kong HTTP log plugin, found out a Kong read and connection timeouts does not invoke the HTTP log plugin in the process for any valuable information. Is there a reason for that or a way to make it so the Http log plugin could send out relevant info?

I’d like to be able to see what specific error occurred too if possible.

---

<div class="post-metadata">

**Author:** ![jeremyjpj0916](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jeremyjpj0916/32/1388_2.png) [@jeremyjpj0916](https://discuss.konghq.com/u/jeremyjpj0916)\
**Post date:** [January 25, 2018, 2:38am UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/2 "2018-01-25T02:38:40Z")

</div>

I am working alongside Ross, and just wanted to add to this a tad with some screenshots. Proxy definition to enforce a read timeout(“upstream\_read\_timeout”) we set this property to 1 millisecond to force a failure:

![image](https://canada1.discourse-cdn.com/flex036/uploads/konghq/original/1X/fd00768132aa4f45bee2c39fc7db94cf0638786f.png)

And the Kong response behavior to the Proxy Consumer:

![image](https://canada1.discourse-cdn.com/flex036/uploads/konghq/original/1X/974f2f93bc5a35a017e681301a7c452480f0dfe9.png)

And so far transactions using the http-log plugin show none of this ever occurring. Now the proxy error logs do currently print to std-out, but would you agree that tcp/udp/http-log and all the rest are within the scope of documenting these sorts of errors as well? And if not, then could you guide us as to how we might branch the http-log plugin so it can log these transactions as well? Note that we are indeed also running the Kong 0.12.1 version that has this sort of short circuiting fix in place for transactions that were rejected with a 401 unauthorized (and can confirm indeed the plugins do receive logs in those cases for that fix!).

Thanks for your time and consideration,  
-Jeremy

---

<div class="post-metadata">

**Author:** ![thibaultcha](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/thibaultcha/32/340_2.png) [@thibaultcha](https://discuss.konghq.com/u/thibaultcha)\
**Post date:** [January 26, 2018, 4:55am UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/3 "2018-01-26T04:55:19Z")

</div>

Hi,

Indeed, a timeout from the ngx\_http\_proxy\_module will make NGINX produce an HTTP 504 response, which the [error\_page directive in the Kong NGINX configuration](https://github.com/Kong/kong/blob/master/kong/templates/nginx_kong.lua#L66) will catch and redirect to our upstream error handling internal NGINX location. This location does not run anything in the NGX\_HTTP\_LOG\_PHASE.

A simple fix for this would be:

```auto
diff --git a/kong/templates/nginx_kong.lua b/kong/templates/nginx_kong.lua
index 5639f319..61b3d87f 100644
--- a/kong/templates/nginx_kong.lua
+++ b/kong/templates/nginx_kong.lua
@@ -147,6 +147,10 @@ server {
         content_by_lua_block {
             kong.handle_error()
         }
+
+ log_by_lua_block {
+ kong.log()
+ }
     }
 }

```

---

<div class="post-metadata">

**Author:** ![jeremyjpj0916](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jeremyjpj0916/32/1388_2.png) [@jeremyjpj0916](https://discuss.konghq.com/u/jeremyjpj0916)\
**Post date:** [January 26, 2018, 7:21am UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/4 "2018-01-26T07:21:02Z")

</div>

Thanks man! Is this something you think should be added into baseline Kong or do we need to deviate from the norm here and bite the bullet and make a custom template?

Just to explain from our perspective:

We get in a meeting where consumer is having an error, support reviews http-logs we fwd to splunk trying to find the transaction in question. Since Kong shortcuts on these cases a tech would find no trace of these transactions in splunk. Timeouts can be a pretty common occurrence when backend APIs rely on systems that get extremely sluggish at random times of stress 😅 .

-Jeremy

---

<div class="post-metadata">

**Author:** ![thibaultcha](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/thibaultcha/32/340_2.png) [@thibaultcha](https://discuss.konghq.com/u/thibaultcha)\
**Post date:** [January 26, 2018, 7:22am UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/5 "2018-01-26T07:22:53Z")

</div>

This should be a part of Kong. Contributions welcome!

---

<div class="post-metadata">

**Author:** ![harryparmar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/harryparmar/32/253_2.png) [@harryparmar](https://discuss.konghq.com/u/harryparmar)\
**Post date:** [January 26, 2018, 10:57am UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/6 "2018-01-26T10:57:40Z")

</div>

Hi can we expect this in 0.12.2? Thanks!

---

<div class="post-metadata">

**Author:** ![Ross\_Sbriscia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/ross_sbriscia/32/118_2.png) [@Ross\_Sbriscia](https://discuss.konghq.com/u/Ross_Sbriscia)\
**Post date:** [January 26, 2018, 6:15pm UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/7 "2018-01-26T18:15:21Z")

</div>

Hi All!

We gave this a shot, and unfortunately it didn’t seem to work. Here’s our current nginx-kong.conf file, do you see anything wrong?

```auto
charset UTF-8;

error_log /dev/stderr notice;

client_max_body_size 0;
proxy_ssl_server_name on;
underscores_in_headers on;

lua_package_path './?.lua;./?/init.lua;;;';
lua_package_cpath ';;';
lua_socket_pool_size 30;
lua_max_running_timers 4096;
lua_max_pending_timers 16384;
lua_shared_dict kong 5m;
lua_shared_dict kong_cache 256m;
lua_shared_dict kong_process_events 5m;
lua_shared_dict kong_cluster_events 5m;
lua_shared_dict kong_healthchecks 5m;
lua_shared_dict kong_cassandra 5m;
lua_socket_log_errors off;
lua_ssl_trusted_certificate '/usr/local/kong/ssl/kongcert.pem';
lua_ssl_verify_depth 2;

init_by_lua_block {
    kong = require 'kong'
    kong.init()
}

init_worker_by_lua_block {
    kong.init_worker()
}

proxy_next_upstream_tries 999;

upstream kong_upstream {
    server 0.0.0.1;
    balancer_by_lua_block {
        kong.balancer()
    }
    keepalive 60;
}

server {
    server_name kong;
    listen 0.0.0.0:8000;
    error_page 400 404 408 411 412 413 414 417 /kong_error_handler;
client_header_buffer_size 8k;
large_client_header_buffers 100 32k;
    error_page 500 502 503 504 /kong_error_handler;

    access_log off;
    error_log /dev/stderr notice;

    client_body_buffer_size 5m;

    listen 0.0.0.0:8443 ssl http2;
    ssl_certificate /usr/local/kong/ssl/kongcert.crt;
    ssl_certificate_key /usr/local/kong/ssl/kongprivatekey.key;
    ssl_protocols TLSv1.2;
    ssl_certificate_by_lua_block {
        kong.ssl_certificate()
    }

    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_prefer_server_ciphers on;
    ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256;

    real_ip_header X-Real-IP;
    real_ip_recursive off;

    location / {
        set $upstream_host '';
        set $upstream_upgrade '';
        set $upstream_connection '';
        set $upstream_scheme '';
        set $upstream_uri '';
        set $upstream_x_forwarded_for '';
        set $upstream_x_forwarded_proto '';
        set $upstream_x_forwarded_host '';
        set $upstream_x_forwarded_port '';

        rewrite_by_lua_block {
            kong.rewrite()
        }

        access_by_lua_block {
            kong.access()
        }

        proxy_http_version 1.1;
        proxy_set_header Host $upstream_host;
        proxy_set_header Upgrade $upstream_upgrade;
        proxy_set_header Connection $upstream_connection;
        proxy_set_header X-Forwarded-For $upstream_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $upstream_x_forwarded_proto;
        proxy_set_header X-Forwarded-Host $upstream_x_forwarded_host;
        proxy_set_header X-Forwarded-Port $upstream_x_forwarded_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass_header Server;
        proxy_pass_header Date;
        proxy_ssl_name $upstream_host;
        proxy_pass $upstream_scheme://kong_upstream$upstream_uri;

        header_filter_by_lua_block {
            kong.header_filter()
        }

        body_filter_by_lua_block {
            kong.body_filter()
        }

        log_by_lua_block {
            kong.log()
        }
    }

    location = /kong_error_handler {
        internal;
        content_by_lua_block {
            kong.handle_error()
	}

	log_by_lua_block {
		kong.log()
	}
    }
}

server {
    server_name kong_admin;
    listen 0.0.0.0:8001;

    access_log off;
    error_log /dev/stderr notice;

    client_max_body_size 10m;
    client_body_buffer_size 10m;

    listen 0.0.0.0:8444 ssl;
    ssl_certificate /usr/local/kong/ssl/admin-kong-default.crt;
    ssl_certificate_key /usr/local/kong/ssl/admin-kong-default.key;
    ssl_protocols TLSv1.2;

    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_prefer_server_ciphers on;
    ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256;

    location / {
        default_type application/json;
        content_by_lua_block {
            kong.serve_admin_api()
        }
    }

    location /nginx_status {
	allow 127.0.0.1;
	deny all;
        access_log off;
        stub_status;
    }

    location /robots.txt {
        return 200 'User-agent: *\nDisallow: /';
    }
}

```

---

<div class="post-metadata">

**Author:** ![jeremyjpj0916](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jeremyjpj0916/32/1388_2.png) [@jeremyjpj0916](https://discuss.konghq.com/u/jeremyjpj0916)\
**Post date:** [January 28, 2018, 9:54pm UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/8 "2018-01-28T21:54:01Z")

</div>

To add to Ross point above I thought it worthwhile to give some insight into our Dockerfile too as that is how we make the modifications to the lua template files kong includes rather than writing our own(for the time being):

```auto
FROM docker.company.com/company_et/alpine:3.6

ENV KONG_VERSION 0.12.1
ENV KONG_SHA256 9f699e20e7d3aa6906b14d6b52cae9996995d595d646f9b10ce09c61d91a4257

USER root
RUN apk add --no-cache --virtual .build-deps wget tar ca-certificates \
	&& apk add --no-cache libgcc openssl pcre perl unzip tzdata luarocks git \
	&& wget -O kong.tar.gz "https://bintray.com/kong/kong-community-edition-alpine-tar/download_file?file_path=kong-community-edition-$KONG_VERSION.apk.tar.gz" \
	&& echo "$KONG_SHA256 *kong.tar.gz" | sha256sum -c - \
	&& tar -xzf kong.tar.gz -C /tmp \
	&& rm -f kong.tar.gz \
	&& cp -R /tmp/usr / \
	&& rm -rf /tmp/usr \
	&& cp -R /tmp/etc / \
	&& rm -rf /tmp/etc \
	&& apk del .build-deps

RUN mkdir /usr/local/kong

COPY docker-entrypoint.sh /docker-entrypoint.sh

#Rewrite nginx kong conf to only enable TLS1.2
RUN sed -i '/ssl_protocols TLSv1.1 TLSv1.2;/c\ ssl_protocols TLSv1.2;' /usr/local/share/lua/5.1/kong/templates/nginx_kong.lua

#Temporary fix attempt to log timeouts
RUN sed -ie '/kong.handle_error()/{n;d}' /usr/local/share/lua/5.1/kong/templates/nginx_kong.lua; sed -i '/kong.handle_error()/ a \\t}\n\n\tlog_by_lua_block {\n\t\tkong.log()\n\t}' /usr/local/share/lua/5.1/kong/templates/nginx_kong.lua

#Increase the buffer header defaults
RUN sed -i '68i client_header_buffer_size 8k;' /usr/local/share/lua/5.1/kong/templates/nginx_kong.lua
RUN sed -i '69i large_client_header_buffers 100 32k;' /usr/local/share/lua/5.1/kong/templates/nginx_kong.lua

#Rewrite ngix conf so we can see our Kong ENV variable
RUN sed -i '8ienv KONG_SSL_CERT_KEY;' /usr/local/share/lua/5.1/kong/templates/nginx.lua

#Install custom plugins with luarocks
RUN luarocks install kong-plugin-stdout-log
RUN luarocks install kong-upstream-jwt

#Install HTTP plugin
RUN git clone https://github.optum.com/DataExternalization/optum-kong-http-log-plugin.git /usr/local/share/lua/5.1/kong/plugins/optum-kong-http-log-plugin

ENTRYPOINT ["/docker-entrypoint.sh"]

EXPOSE 8000 8443 8001 8444

STOPSIGNAL SIGTERM

USER 1001

CMD ["/usr/local/openresty/nginx/sbin/nginx", "-c", "/usr/local/kong/nginx.conf", "-p", "/usr/local/kong/"]

```

I don’t really have the knowledge outside of developing on individual plugins how to debug further, I wonder if kong.handle\_errors() short circuits method calls/blocks that come after it, nor do I know where to look to review the source code of kong.handle\_errors() and how content\_by\_lua\_block/log\_by\_lua\_block works with these methods(are they sort of like lua “phases” for when executions should happen?

---

<div class="post-metadata">

**Author:** ![thibaultcha](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/thibaultcha/32/340_2.png) [@thibaultcha](https://discuss.konghq.com/u/thibaultcha)\
**Post date:** [January 30, 2018, 1:34am UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/9 "2018-01-30T01:34:16Z")

</div>

I may have spoken too fast: I tested on my side that the NGINX log phase was executed, but the `kong.log()` call will not execute plugins indeed, for another reason: NGINX’s `error_page` does an internal redirect, which causes `ngx.ctx` (the value storing the API and plugins to execute for this request) to be reset. Thus, no plugins will be run (Kong even lost the information relating to which API was matched in that particular request at this point).

There seems to be no easy fix for this, we’ll have to add this to our backlog and think of a workaround. Sorry for the false hope!

---

<div class="post-metadata">

**Author:** ![jeremyjpj0916](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jeremyjpj0916/32/1388_2.png) [@jeremyjpj0916](https://discuss.konghq.com/u/jeremyjpj0916)\
**Post date:** [January 30, 2018, 6:18am UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/10 "2018-01-30T06:18:08Z")

</div>

Thanks for your continued effort looking into this, we too are going to try to find a hack/workaround to make Kong log these scenarios via plugin so we have a full view into api transaction errors from external logging applications. If anyone at Kong starts to think up an idea let us know and we will test it!

Setup easier tracking of this problem on git - [https://github.com/Kong/kong/issues/3193](https://github.com/Kong/kong/issues/3193)

-Jeremy

---

<div class="post-metadata">

**Author:** ![jeremyjpj0916](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jeremyjpj0916/32/1388_2.png) [@jeremyjpj0916](https://discuss.konghq.com/u/jeremyjpj0916)\
**Post date:** [May 10, 2018, 5:43am UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/11 "2018-05-10T05:43:45Z")

</div>

Any updates on this front? Would it be feasible to copy the entire ngx.ctx to a global var of sorts prior to the error\_page internal redirect and still execute kong.log() against that post redirect? Hoping Kong can reach full potential here on transaction logging.

---

<div class="post-metadata">

**Author:** ![thibaultcha](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/thibaultcha/32/340_2.png) [@thibaultcha](https://discuss.konghq.com/u/thibaultcha)\
**Post date:** [May 10, 2018, 9:18pm UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/12 "2018-05-10T21:18:51Z")

</div>

No updates on that front yet. Any experimentation from readers of this topic would be welcome.

---

<div class="post-metadata">

**Author:** ![jeremyjpj0916](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jeremyjpj0916/32/1388_2.png) [@jeremyjpj0916](https://discuss.konghq.com/u/jeremyjpj0916)\
**Post date:** [May 12, 2018, 12:44am UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/13 "2018-05-12T00:44:20Z")

</div>

What do you think about this possibly yo : [https://github.com/openresty/lua-nginx-module/issues/1057](https://github.com/openresty/lua-nginx-module/issues/1057) , you think this to be a valid way to do it in Kong and if we did where should this live? Could this be done in the template? Seems agentzh has known about this for awhile:  
“The problem is that the nginx core clears all modules’ ctx data upon internal redirects.”

EDIT - Ohhh 3scale api gateway who uses nginx similarly to Kong also wrote a module for it(you can see it tied to the issue I linked above). Initial thought is this would integrate into Kong core rather than a plugin/template(it is Apache 2.0 Licensed) -  
[https://github.com/3scale/apicast/blob/master/gateway/src/resty/ctx.lua](https://github.com/3scale/apicast/blob/master/gateway/src/resty/ctx.lua)  
And Testcase  
[https://github.com/3scale/apicast/blob/master/t/resty-ctx.t](https://github.com/3scale/apicast/blob/master/t/resty-ctx.t)

-Jeremy

---

<div class="post-metadata">

**Author:** ![jeremyjpj0916](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jeremyjpj0916/32/1388_2.png) [@jeremyjpj0916](https://discuss.konghq.com/u/jeremyjpj0916)\
**Post date:** [May 14, 2018, 4:55am UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/14 "2018-05-14T04:55:26Z")

</div>

Just going to update this post so we can consider the initial problem brought up as closed:

> <https://github.com/Kong/kong/issues/3193>

Awesome to see @thibaultcha cook something up that will give Kong much better visibility into API transaction error logging!

Will be interesting to see if there are any performance hits here but I am going to be hopeful it does not cost much! I would pay a few milliseconds regardless to ensure better logging.

---

<div class="post-metadata">

**Author:** ![bungle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/bungle/32/13_2.png) [@bungle](https://discuss.konghq.com/u/bungle)\
**Post date:** [June 4, 2018, 6:43pm UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/15 "2018-06-04T18:43:39Z")

</div>

@jeremyjpj0916 We came up with two solutions:

Make reference of `ctx` available for `error_page`:  
[https://github.com/Kong/kong/tree/wip/feat/log-plugins-on-error-page](https://github.com/Kong/kong/tree/wip/feat/log-plugins-on-error-page)

Remove `error_page`:  
[https://github.com/Kong/kong/tree/refactor/proxy-error-handling](https://github.com/Kong/kong/tree/refactor/proxy-error-handling)

Both are quite small changes to hot code paths. And there is a third option that is a little bit of both. All solutions have good and bad sides, we need to weight them a bit.

---

<div class="post-metadata">

**Author:** ![jeremyjpj0916](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jeremyjpj0916/32/1388_2.png) [@jeremyjpj0916](https://discuss.konghq.com/u/jeremyjpj0916)\
**Post date:** [June 4, 2018, 6:48pm UTC](https://discuss.konghq.com/t/kong-http-log-plugin-errors/445/16 "2018-06-04T18:48:58Z")

</div>

By removing error\_page, that rids yourself of the internal redirect in the first place right? But error\_page is also how you explicitly state error code to direct to the kong\_error\_handler

```auto
    error_page 400 404 408 411 412 413 414 417 494 /kong_error_handler;
    error_page 500 502 503 504 /kong_error_handler;

```

So I guess there was an alternative place that could be made for handling these statuses. Interesting stuff!
