How to configure Nginx logs running inside a Docker that the logs on the host machine can rotated properly?

  • Docker is installed and running on the host machine.
  • Nginx is installed inside a Docker container, not on the host machine.
  • The host machine is configured to receive logs from Nginx, specifically the logs located in the /data/logs/nginx/*.log directory.

I have an expectation that the Nginx logs on the host machine should undergo log rotation. However, currently, only the access.log and error.log files are rotated inside the Docker container.

Maybe doing the logrotate at the host machine is one solution, but we have multiple machines, so it is not efficient to do it at every machine.

Question: How can I configure log rotation for Nginx logs running inside a Docker container so that the logs on the host machine, located in the /data/logs/nginx/*.log directory, are rotated properly?

DockerFile:

FROM node:10-alpine

RUN apk update && apk add supervisor nginxpython2 logrotate

Run ......

COPY docker/conf/nginx/nginx.conf /etc/nginx/nginx.conf
COPY docker/conf/nginx/nginx-logrotate.conf /etc/logrotate.d/nginx-logrotate.conf
COPY docker/conf/supervisor/supervisord.conf /etc/supervisor/supervisord.conf
COPY docker/conf/supervisor/conf.d /etc/supervisor/conf.d

docker/conf/supervisor/conf.d/nginx.conf:

[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
autostart=true
autorestart=true
priority=10
stdout_events_enabled=true
stderr_events_enabled=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

docker/conf/nginx/nginx.conf:

worker_processes auto;
error_log /data/logs/nginx/error.log;
pid /var/run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    multi_accept on;
    worker_connections 87381;
}

http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '$http_host $upstream_response_time $request_time '
    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log /data/logs/nginx/access.log main;
    .....
}

docker/conf/nginx/nginx-logrotate.conf:

/data/logs/nginx/*.log {
    rotate 10
    daily
    compress
    delaycompress
    missingok
    notifempty
    create 0644 nginx nginx
    sharedscripts
    postrotate
        /usr/sbin/nginx -s reopen
    endscript
}

Hi :slight_smile:

I belive that running x supervisors + x logrotates is more inefficient, i would rather make nginx log to stdout and let docker logging driver handle the rotation.

but maybe log to a shared logging platform?