我已经试过这个教程来旋转日志文件没有外部软件,但它似乎不工作,我的configuration在server {块:
if ($time_iso8601 ~ "^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})") {} access_log /var/log/access-$year-$month-$day.log; error_log /var/log/error-$year-$month-$day.log;
和创build的文件名为:
-rw-r--r-- 1 root root 0 May 28 17:46 error-$year-$month-$day.log
我的NginX版本:
nginx版本:nginx / 1.8.0
使用OpenSSL 1.0.2a 2015年3月19日构build
已启用TLS SNI支持
configuration参数:–prefix = / etc / nginx –conf -path = /etc/nginx/nginx.conf –sbin -path = / usr / bin / nginx –pid-path = / run / nginx.pid – -lock-path = / run / lock / nginx.lock –user = http –group = http –http-log-path = / var / log / nginx / access.log –error-log-path = stderr –http-client-body-temp-path = / var / lib / nginx / client-body –http-proxy -temp -path = / var / lib / nginx / proxy –http -fastcgi -temp path = / var / lib / nginx / fastcgi –http-scgi-temp-path = / var / lib / nginx / scgi –http-uwsgi-temp-path = / var / lib / nginx / uwsgi –with-imap – -with-imap_ssl_module –with-ipv6 –with-pcre-jit –with-file-aio –with-http_dav_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_realip_module –with-http_spdy_module – with-http_ssl_module –with-http_stub_status_module –with-http_addition_module –with-http_degradation_module –with-http_flv_module –with-http_mp4_module –with-http_secure_link_module –with-http_sub_module
见上面我的评论和cpburnz的推理,但如果你真的想继续:
您可以编写一个脚本,只将日志命令写入包含文件,当前date是硬编码的。 你可以在你的nginxconfiguration文件中include d,你的脚本会在将log命令写入包含文件之后重新启动或重新加载nginx。
就像是:
#!/bin/bash date=`date -Id` cat > /etc/nginx/includes/log_by_date.inc <<EOF access_log /var/log/access-${date}.log; error_log /var/log/error-${date}.log; EOF /etc/init.d/nginx restart
你可以从cron运行,很可能接近午夜。
加上当然你需要include /etc/nginx/includes/log_by_date.inc ,你有你当前的日志logging命令。
根据那篇博文,
请注意,无法在error_log指令中embeddedvariables ,因为如果无法写入文件,将无法logging任何可能的错误。
考虑到这一点,使用该Nginx方法error-$year-$month-$day.log使用命名格式error-$year-$month-$day.log来自动旋转错误日志。
但是,为错误日志设置日志轮转不是很困难。 在/usr/local/bin/rotate_nginx_error_log.sh创build一个简单的shell(bash)脚本:
#!/bin/sh # /usr/local/bin/rotate_nginx_error_log.sh # Get yesterday's date as YYYY-MM-DD YESTERDAY=$(date -d 'yesterday' '+%Y-%m-%d') PID_FILE=/run/nginx.pid LOG_FILE=/var/log/error.log OLD_FILE=/var/log/error-$YESTERDAY.log # Rotate yesterday's log. mv $LOG_FILE $OLD_FILE # Tell nginx to reopen the log file. kill -USR1 $(cat $PID_FILE)
确保它具有可执行权限:
chmod +x /usr/local/bin/rotate_nginx_error_log.sh
然后在您的crontab中添加一个cronjob,在午夜的时候每隔一段时间轮转一次log:
0 0 * * * /usr/local/bin/rotate_nginx_error_log.sh
这是我在我的服务器上:
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") { set $year $1; set $month $2; set $day $3; } access_log /path/to/logs/nginx-access-$year-$month.log;
现在,有时$time_iso8601没有预期的格式,然后将日志写入一个名为nginx-access--.log (实际上, $year和$month没有设置)。
所以我会考虑改变这样的行:
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") { set $year $1; set $month $2; set $day $3; access_log /path/to/logs/nginx-access-$year-$month.log; } else { access_log /path/to/logs/nginx-access-notime.log; }
但是,正如@cpburnz所说的那样,您将无法为错误日志执行此操作。