Storing docker nginx access logs inside a docker volume
Solution 1
By default the Nginx container forwards access logs to STDOUT and error logs to STDERR. You can see these lines in nginx Dockerfile:
# 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 \
So you can see nginx logs in container logs:
docker logs -f ngx
But if you want to store nginx logs in a docker volume, first create your customized nginx docker image:
FROM nginx
RUN rm /var/logs/nginx/*
And then your nginx service in docker-compose.yml would be like:
nginx:
build: ./nginx/
container_name: ngx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx:/etc/nginx/conf.d
- ./app/static:/static
- ./log/nginx:/var/log/nginx
depends_on:
- app
Solution 2
Apart from the comments above to add the volume, you have to adjust main Nginx configuration (most probably /etc/nginx/nginx.conf
) and/or configuration files for each vhost.
In most cases add these to your main configuration:
http {
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
}
Similar entries could be palces inside server
or location
Related videos on Youtube
wasabi_gardener
Updated on September 15, 2022Comments
-
wasabi_gardener over 1 year
Currently my docker container is printing the nginx access logs to /dev/stdout. How do I create a volume inside my docker container to store the access logs?
My Dockerfile:
FROM python:3.7 ENV APP_ROOT /src ENV CONFIG_ROOT /config RUN apt-get update RUN apt-get install -y apt-utils RUN apt-get -y install unixodbc-dev RUN apt-get -y install default-libmysqlclient-dev RUN mkdir ${CONFIG_ROOT} COPY /app/requirements.txt ${CONFIG_ROOT}/requirements.txt RUN pip install -r ${CONFIG_ROOT}/requirements.txt RUN mkdir ${APP_ROOT} WORKDIR ${APP_ROOT} ADD /app/ ${APP_ROOT}
My docker-compose.yml:
version: "3" services: app: build: . container_name: django-gunicorn restart: always env_file: - django.env ports: - "8000:8000" command: "gunicorn --workers=2 --bind=0.0.0.0:8000 mysite.wsgi:application" nginx: image: nginx:1.14 container_name: ngx ports: - "80:80" - "443:443" volumes: - ./nginx:/etc/nginx/conf.d - ./app/static:/static depends_on: - app
My nginx/default.conf:
limit_req_zone "$binary_remote_addr$request_uri" zone=one:10m rate=60r/m; server { listen 80; server_name example.org; server_tokens off; location /static/ { autoindex on; alias /static/; } location / { proxy_pass http://app:8000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; limit_req zone=one nodelay burst=30; } }
I am trying to add fail2ban and fluentd logging to this application but first I need to store the physical file (not /dev/stout) which can be used for other logging purposes.
Thanks you!
-
wasabi_gardener about 5 yearsSamuel, I've updated my original post. Thanks!
-
Igor Nikolaev about 5 yearsYou can try mounting some local directory for storing logs in a similar way you do with static assets and nginx configs:
./logs:/var/log/nginx
. I haven't tried that myself, but that could work.You might need to figure out the right path inside Docker container though.
-
-
Gupta about 2 yearsI has this config, but then again it is not saving log in access.log file. Any hint. what I'm missing ?
-
Szczad about 2 yearsHave you removed the symlinks, patched the configuration file, mounted /var/log from the host side? Please print the configuration
nginx -T