Configure NGINX Logging in a Docker Container

Or how to prevent your log aggregator from being flooded
author's avatar
Kees de Kooter
Jul 12 2019 11:14 • 1 min read

Default setup

By default, the NGINX image is configured to send NGINX access log and error log to the Docker log collector. This is done by linking them to stdout and stderr 1.

In the NGINX Dockerfile this is achieved by creating symbolic links to /dev/stdout and /dev/stderr:

# forward request and error logs to docker log collector
    && ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log

As a result the docker logs (and possibly configured log aggregators) are flooded with access log lines, obfuscating the actual problems.

Filtering what matters

We are usually only interested in errors, so it is fine if those are forwarded to the docker logs. But we can leave the access logs out.

We can achieve this by renaming the access log for our service. In nginx.conf add the following line:

server {
  ...
  access_log /var/log/nginx/<service-name>-access.log;
  ...
}

In addition we need to modify our Dockerfile a bit.

FROM nginx
...
VOLUME /var/log/nginx

On startup of this container we map in a host file system location with -v, or in case of docker-compose an entry in the volumes section of a service.

End result

  • error log lines are sent to docker log
  • access log lines are sent to a file that is stored on a mapped volume.

References

[1]NGINX Docs | Deploying NGINX and NGINX Plus on Docker