Nginx 记录两个文件以及 docker 日志收集器 [英] Nginx logs in both file as well as docker log collector
问题描述
我正在部署一个 Nginx
容器,并且在标准的 Dockerfile
中,日志正在使用这些命令重定向到容器日志
I am deploying a Nginx
container and in the standard Dockerfile
logs are being redirected to container logs with these commands
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log
如果我删除上述命令,那么日志将按预期写入 access.log
和 error.log
文件.如何将日志保存在文件中并将它们发送到容器日志?
If I remove above mentioned commands then logs are written to access.log
and error.log
files as expected.
How can I keep the logs in the files as well as send them to container logs ?
推荐答案
1- 下面的命令不会将 /var/log/nginx/access.log
的内容重定向到 /dev/stdout
相反,它创建了一个从 /dev/stdout
到 /var/log/nginx/access.log
的符号链接.换句话说, /var/log/nginx/access.log
只是一个符号链接.
1- The below command does not redirect the content of /var/log/nginx/access.log
to /dev/stdout
instead it created a symlink from /dev/stdout
to /var/log/nginx/access.log
. In other words, /var/log/nginx/access.log
is just a symlink.
ln -sf /dev/stdout /var/log/nginx/access.log
# ls -alh /var/log/nginx/access.log
lrwxrwxrwx 1 root root 11 Nov 23 01:12 /var/log/nginx/access.log -> /dev/stdout
2- 出于两个原因,根本不建议将文件存储在 docker 容器中.首先,一旦重新创建容器,文件就会丢失.其次,文件可能会增长到巨大的大小,尤其是在这些文件上没有日志轮换的情况下.您可以使用 FluentD 或配置 docker 本身将日志转发到外部服务器或主机节点上的文件,而不是在容器内保留副本.
2- Storing files in the docker containers is not recommended at all for two reasons. First, the files will be lost once the containers are recreated. Second, the files could grow to huge sizes especially if there is not log rotation over these files. Instead of keeping a copy inside the container you can use FluentD or configure docker itself to forward the logs to an external server or a file on the host node.
这篇关于Nginx 记录两个文件以及 docker 日志收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!