保留最近 30 天的 nginx 日志

keep the nginx logs of the last 30 days

我想将 lsat 的 nginx 日志保留 30 天。 默认配置为 15 天,如图所示。

我想保留最近 30 天。

这里是nginx的looging设置:

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

但它并没有说明 "how oft" 应该采取什么措施。

我根本不是 nginx 专家,所以我不知道 how/where 我可以更改那个配置。

也许那里有人需要做同样的事情并想帮助我。

要更改此行为,您必须更改 nginx 的 logrotate 文件。 该文件可能位于 /etc/logrotate.d。 为了实现你想要做的事情,将指令 weeklyrotate 30 放在与 nginx 对应的文件中。 之后,使用以下命令确保更改生效:

  • logrotate /etc/logrotate.d/nginx-config-file

您可以为 nginx 设置 logrotate,这样您就可以根据您的要求维护日志 30 天或更长时间!

/etc/logrotate.d/nginx


/var/log/nginx/access_log {
rotate 7
size 5k
dateext
dateformat -%Y-%m-%d
missingok
compress
sharedscripts
postrotate
    test -r /var/run/nginx.pid && kill -USR1 `cat /var/run/nginx.pid`
endscript
}

相应地更改#rotate 的值! 30,40 等等......

Logrotate 是一个非特定于 nginx 的实用程序,默认安装在 Ubuntu/Debian 上,旨在简化日志文件的管理。您可以在联机帮助页中找到配置信息和背景:

man logrotate

在我的 Debian 服务器上,我的配置存储在 /etc/logrotate.d/nginx 中。联机帮助页提供了具有直观名称的配置文件示例。对于你的情况,你应该有行

daily
rotate 30

保存结果
sudo logrotate /etc/logrotate.d/nginx

更深入的教程:https://www.digitalocean.com/community/tutorials/how-to-manage-logfiles-with-logrotate-on-ubuntu-16-04