我知道以前有人问过这个问题,但我相信这是另一个问题。
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
Log转速使用正确的权限创建日志文件:
/var/log/nginx/*.log {
( ... )
create 0640 www-data adm
Nginx在重新启动时会将日志记录到access.log
,但在日志旋转第一次运行后移动到access.log.1
。在此之后,始终将日志记录到access.log.1
,然后不旋转日志文件。
编辑:已经在评论中指出,您看到access.log
被访问的时间比access.log.1
晚,原因是我在做ls
之前重新启动了nginx,只是为了确保自己在这里发布之前,重新启动nginx确实解决了问题(直到下一次日志旋转)。但在此之前,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;
( ... )
发布于 2016-08-28 21:53:41
解决了。
我的问题几乎像这,但不完全一样。在这篇文章中,作者最终解决了这个问题,并说问题是"nginx在接收到-USR1信号后并没有将文件句柄释放给日志文件。长话短说,它没有重新加载日志文件的原因是/var/ log /nginx文件夹不属于nginx工作进程(由www-data拥有,在web下运行)的同一个用户。“正如我们所看到的,这不是我的问题,因为我的权限是正确的。然而,我去比较了我的日志旋转日志和关于这个问题的日志,并发现了一些东西。在这个问题上,kill
信号成功终止,但是由于权限的原因,nginx没有释放文件句柄。在我的示例中,invoke-rc.d
命令不会成功终止。nginx的log旋转式配置如下:
/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
}
注意后置旋转脚本,它告诉nginx做它的事情,对于另一个线程上的作者来说,这是kill
信号。在日志中,我得到以下错误(顺便说一句,您可以通过执行sudo logrotate -f -v /etc/logrotate.d/nginx
强制日志旋转):
( last two lines ... )
running postrotate script
error: error running shared postrotate script for '/var/log/nginx/*.log '
当我获取您在log旋转式/nginx配置中看到的Post旋转式脚本并手动执行它时,它会出错:
$ invoke-rc.d nginx rotate
initctl: invalid command: rotate
Try `initctl --help' for more information.
invoke-rc.d: initscript nginx, action "rotate" failed.
这是nginx中的错误。所以我所做的就是用另一个线程上的人正在使用的命令替换这个命令。因此,现在我在配置文件上的log旋转式/nginx后置脚本是
postrotate
kill -USR1 `cat /run/nginx.pid`
endscript
这解决了这个问题。
发布于 2017-10-11 08:41:55
一些更好的解决方案
https://unix.stackexchange.com/questions/186807/what-does-nginx-s-reopen-do
所以:
postrotate
invoke-rc.d nginx -s reopen >/dev/null 2>&1
endscript
https://stackoverflow.com/questions/39194819
复制相似问题