You may need to customize what Kong Gateway logs. For instance, you may want to:
- Protect private information
- Comply with GDPR or other data protection regulations
- Remove instances of a specific piece of data from your logs, such as an email address
These changes can be made to Kong Gateway’s Nginx template and only affect the output of the Nginx access logs. This doesn’t have any effect on Kong Gateway’s logging plugins.
Let’s look at an example where you want to remove any instances of an email address from your Kong Gateway logs. The email addresses may come through in different formats, for example /servicename/v2/verify/alice@example.com
or /v3/verify?alice@example.com
. To keep all of these formats from being added to the logs, you need to use a custom Nginx template.
Make a copy of Kong Gateway’s Nginx template, then edit it to add or remove the data you need. The following template shows an example configuration for removing email addresses from logs:
# ---------------------
# custom_nginx.template
# ---------------------
worker_processes $; # can be set by kong.conf
daemon $; # can be set by kong.conf
pid pids/nginx.pid; # this setting is mandatory
error_log stderr $; # can be set by kong.conf
events {
use epoll; # custom setting
multi_accept on;
}
http {
map $request_uri $keeplog {
~.+\@.+\..+ 0;
~/v1/invitation/ 0;
~/reset/v1/customer/password/token 0;
~/v2/verify 0;
default 1;
}
log_format show_everything '$remote_addr - $remote_user [$time_local] '
'$request_uri $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
include 'nginx-kong.conf';
}
For this example, we’re using the following:
-
map $request_uri $keeplog
: Maps a new variable called keeplog
, which is dependent on values appearing in the $request_uri
. Each line in the example starts with a ~
because this is what tells Nginx to use a regex when evaluating the line. This example looks for the following:
- The first line uses a regex to look for any email address in the
x@y.z
format
- The second line looks for any part of the URI that contains
/servicename/v2/verify
-
The third line looks at any part of the URI that contains /v3/verify
Because all of these patterns have a value of something other than 0
, if a request has any of those elements, it will not be added to the log.
-
log_format
: Sets the log format for what Kong Gateway keeps in the logs. The contents of the log can be customized for your needs. For the purpose of this example, you can assign the new logs with the name show_everything
and set everything to the Kong Gateway default standards. To see the full list of options, refer to the Nginx core module variables reference.
Once you’ve adjusted the Nginx template for your environment, you need to tell Kong Gateway to use the newly created log, show_everything
.
To do this, alter the Kong Gateway variable proxy_access_log
by either editing etc/kong/kong.conf
or using the environmental variable KONG_PROXY_ACCESS_LOG
adjust the default location:
proxy_access_log=logs/access.log show_everything if=$keeplog
Restart Kong Gateway to apply changes with the kong restart
command.
Now, any request made with an email address in it will no longer be logged.