我发现在做Nginx的日志旋转的例子在这里
但是一个简单的测试:
set $date "2018-08-24";
access_log /home/tim/log/access-http-$date.log default;
Run Code Online (Sandbox Code Playgroud)
生成一个名为access-http-.log
. 我正在使用 nginx 1.13.6(带有 openresty)。
更新
经过大量的修改和调整后,我想出了以下 logrotate 脚本来旋转 nginx 生成的不同日志文件。我已经把它放到了 /etc/logrotate.conf 文件中。剩下的问题是日志不会每天轮换(我目前不确定为什么),但是 alogrotate -f <filename>
产生的结果正是我想要的。
一个有趣的注意事项是nginx -s reload
可以代替 USR1 使用。不幸的是,nginx 日志记录页面中没有引用它,但我找到了它(在手册页 IIRC 中)。我手动构建 openresty/nginx 以合并我需要的额外模块,因此我不会像 pid 保留那样获得打包版本中的各种额外功能。
/path/to/log/access-http.log /path/to/log/access-https.log /path/to/log/api.log /path/to/log/error.log {
daily
missingok
notifempty
create 664 nobody tim
dateext
dateformat -%Y-%m-%d
dateyesterday
rotate 10
su tim tim
postrotate
/usr/local/bin/openresty -s reload
endscript
}
Run Code Online (Sandbox Code Playgroud)
我认为这对于拥有大型 nginx 配置同时提供网页和 API 的任何人都非常有用。我将 http 分开,因为我不提供非 https 页面,并且它将脚本小子废话从我的页面日志中删除。