Logging Nginx将日志记录到access.log.1而不是access.log

Logging Nginx将日志记录到access.log.1而不是access.log,logging,nginx,logrotate,Logging,Nginx,Logrotate,我知道以前有人问过这个问题,但我相信这是另一个问题 Nginx在www-data下运行: $ ps -eo "%U %G %a" | grep nginx root root nginx: master process /usr/sbin/nginx -g daemon on; master_process on; www-data www-data nginx: worker process /var/log/nginx/*拥有正确的权限: $ ls -lah /var/lo

我知道以前有人问过这个问题,但我相信这是另一个问题

Nginx在
www-data
下运行:

$ ps -eo "%U %G %a" | grep nginx
root     root     nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data www-data nginx: worker process
/var/log/nginx/*
拥有正确的权限:

$ ls -lah /var/log/nginx/
total 291M
drwxr-x---  2 www-data adm     4.0K Jul 25 06:25 .
drwxrwxr-x 14 root     syslog  4.0K Aug 28 06:25 ..
-rw-r-----  1 www-data adm      12K Aug 28 19:03 access.log
-rw-r-----  1 www-data adm     250M Aug 28 18:50 access.log.1
/var/log/nginx/*.log {
        ( ... )
        create 0640 www-data adm
Logrotate创建具有正确权限的日志文件:

$ ls -lah /var/log/nginx/
total 291M
drwxr-x---  2 www-data adm     4.0K Jul 25 06:25 .
drwxrwxr-x 14 root     syslog  4.0K Aug 28 06:25 ..
-rw-r-----  1 www-data adm      12K Aug 28 19:03 access.log
-rw-r-----  1 www-data adm     250M Aug 28 18:50 access.log.1
/var/log/nginx/*.log {
        ( ... )
        create 0640 www-data adm
Nginx在重新启动时会记录到
access.log
,但在logrotate第一次运行后会移动到
access.log.1
。在此之后,始终将日志记录到
access.log.1
,并且在此之后不会旋转日志文件

编辑:在评论中指出,您看到的
access.log
access.log.1
晚被访问的原因是,在我重新启动nginx之前,我刚刚重新启动了
ls
,只是为了在这里发布之前确保自己,重新启动nginx确实解决了问题(直到下一个logrotate)。但在此之前,
ls
nginx已经登录到
access.log.1
大约3周了

EDIT2:这里是
/etc/nginx/nginx.conf
,头部和提到日志的位

user www-data;
worker_processes auto;
pid /run/nginx.pid;

( ... )

http {

        ( ... )
        ##
        # Logging Settings
        ##

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

        ( ... )
解决了

我的问题差不多,但不完全一样。在那篇文章中,作者最终解决了这个问题,并说问题就在于此“nginx在收到kill发出的-USR1信号时没有释放日志文件的文件句柄。长话短说,它没有重新加载日志文件的原因是/var/log/nginx文件夹与nginx工作进程不属于同一用户(属于www data,在web下运行)。"正如我们所看到的,这不是我的问题,因为我的权限是正确的。但是,我去比较了我的logrotate日志和那个问题上的日志,并发现了一些东西。在那个问题上,
kill
信号成功终止,但nginx没有因为权限而释放文件句柄。在我的例子中,
调用rc.d
命令未成功终止。nginx的logrotate配置如下:

/var/log/nginx/*.log {
        weekly
        missingok
        rotate 52
        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
        endscript
}
请注意postrotate脚本,这是一个命令,它告诉nginx执行它的操作,对于另一个线程上的作者来说,这是
kill
信号。在我的logrotate日志中,我得到了以下错误(顺便说一句,您可以通过执行
sudo logrotate-f-v/etc/logrotate.d/nginx
)来强制logrotate:

当我获取您在logrotate/nginx配置中看到的postrotate脚本并手动执行时,它会出错:

$ invoke-rc.d nginx rotate
initctl: invalid command: rotate
Try `initctl --help' for more information.
invoke-rc.d: initscript nginx, action "rotate" failed.
这是nginx中的一个命令。因此,我所做的是用另一个线程上的家伙正在使用的命令替换该命令。因此,现在我在配置文件上的logrotate/nginx postrotate脚本是

postrotate
        kill -USR1 `cat /run/nginx.pid`
endscript

这就解决了这个问题。

这里有一些更好的解决方案

因此:


那么,为什么我们看到access.log稍后会更新,而acces.log.1会更新?从您向我们展示的情况来看,它似乎工作正常。因为我刚刚重新启动了nginx,就在我重新启动nginx之前,
ls
您可以发布nginx配置文件的一部分吗关于日志记录有什么规定吗?@Neal完成了,请检查。很高兴你找到了这个!我正在(非常模糊地)思考与logrotate相关的事情,但很高兴它解决了。kill-USR1
cat/run/nginx.pid
--它不会重新启动nginx?它只是重新打开日志文件,对吗?