带logrotate的容器化nginx日志旋转 [英] containerized nginx log rotation with logrotate

查看:96
本文介绍了带logrotate的容器化nginx日志旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Nginx没有本地日志轮换,因此需要外部工具,例如logrotate.Nginx提出了一个挑战,即必须在旋转后重新打开日志.如果pid在/var/run中可用,则可以向它发送USR1信号.

Nginx doesn't have native log rotation, so an external tool, such as logrotate, is required. Nginx presents a challenge in that the logs have to be reopened post rotation. You can send a USR1 signal to it if the pid is available in /var/run.

但是在docker容器中运行时,/var/run中缺少pid文件(并且pid实际上属于主机,因为从技术上讲,它是一个主机进程).

But when running in a docker container, the pid file is missing in /var/run (and the pid actually belongs to the host, since it is technically a host process).

如果您不重新打开日志,nginx根本不会记录任何内容,尽管它可以继续用作Web服务器,反向代理等.

If you don't reopen the logs, nginx doesn't log anything at all, though it continues to function otherwise as web server, reverse proxy, etc.

推荐答案

您可以使用docker inspect从Pid属性获取进程ID,并使用kill -USR1 {pid}让Nginx重新打开日志.

You can get the process id from the Pid attribute using docker inspect and use kill -USR1 {pid} to have nginx reopen the logs.

这是我创建的/etc/logrotate.d/nginx文件:

Here's the /etc/logrotate.d/nginx file I created:

/var/log/nginx/access.log
{
    size 2M
    rotate 10
    missingok
    notifempty
    compress
    delaycompress
    postrotate
        docker inspect -f '{{ .State.Pid }}' nginx | xargs kill -USR1
    endscript
}

这篇关于带logrotate的容器化nginx日志旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆