Nginx 没有本机日志轮转,因此需要外部工具,例如 logrotate。 Nginx 提出了一个挑战,因为日志必须在轮换后重新打开。如果 pid 在 /var/run 中可用,您可以向其发送 USR1 信号。
但是当在 docker 容器中运行时,/var/run 中缺少 pid 文件(并且 pid 实际上属于主机,因为它在技术上是主机进程)。
如果您不重新打开日志,nginx 根本不会记录任何内容,但它会继续充当 Web 服务器、反向代理等。
您可以使用 docker Inspect 从 Pid 属性获取进程 ID,并使用 Kill -USR1 {pid} 让 nginx 重新打开日志。
这是我创建的 /etc/logrotate.d/nginx 文件:
/var/log/nginx/access.log
{
size 2M
rotate 10
missingok
notifempty
compress
delaycompress
postrotate
docker inspect -f '{{ .State.Pid }}' nginx | xargs kill -USR1
endscript
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)