Nginx 无法登录 access.log

Nginx 无法登录 access.log

我一直在为 Nginx 日志记录而苦恼。我首先发现的一个问题是 logrotate 失败。因此,我将用户更改为 nginx 工作进程(www-data)的同一所有者,并强制 logrotate,然后它突然停止记录。

这是许可

$ ls -al
total 3876
drwxrwxrwx  2 www-data adm       4096 Apr 11 01:09 .
drwxrwxr-x 10 root     syslog    4096 Apr 11 00:00 ..
-rw-r--rwx  1 www-data adm          0 Apr 11 01:09 access.log

$ ps -eo "%U %G %a" | grep nginx
root     root     nginx: master process nginx
www-data www-data nginx: worker process
www-data www-data nginx: worker process
root     root     nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data www-data nginx: worker process
www-data www-data nginx: worker process
ubuntu   ubuntu   grep --color=auto nginx

这是 logrotate 配置:

/var/log/nginx/*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    prerotate
        if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
            run-parts /etc/logrotate.d/httpd-prerotate; \
        fi \
    endscript
    postrotate
        invoke-rc.d nginx rotate >/dev/null 2>&1;
        DIR=$(dirname $1);
                USER=$(stat -c "%U" $DIR);
                chmod 647 $DIR/access.log;
    endscript
}

答案1

这是相当过度设计的。

/var/log/nginx/*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    create 0640 www-data adm
    su www-data adm
    sharedscripts
    postrotate
        killall -USR1 nginx
    endscript
}

基本上是这样的su www-data adm是你遗漏的。另请注意,在创建新的 vhost 配置并执行时nginx -s reload- nginx 将从 root 创建新的访问/错误日志(如果已定义),这将有效地阻止它们将来的轮换。

相关内容