gpt4 book ai didi

logging - Nginx 记录到 access.log.1 而不是 access.log

转载 作者:行者123 更新时间:2023-12-05 00:54:50 26 4
gpt4 key购买 nike

我知道以前有人问过这个问题,但我相信这是一个不同的问题。

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

Logrotate 创建具有正确权限的日志文件:
/var/log/nginx/*.log {
( ... )
create 0640 www-data adm

Nginx 登录到 access.log当它重新启动但移动到 access.log.1 logrotate 第一次运行后。之后,总是登录到 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;

( ... )

最佳答案

解决了。

我的问题几乎就像 this但不完全是。在那篇文章中,作者最终解决了这个问题,并说问题是“nginx 在收到来自 kill 的 -USR1 信号后没有将文件句柄释放到日志文件。长话短说,它没有重新加载日志文件的原因是因为/var/log/nginx 文件夹与 nginx 工作进程(由 www-data 拥有,在 web 下运行)不属于同一用户。”正如我们所见,这不是我的问题,因为我的权限是正确的。但是,我去比较了我的 logrotate 日志和那个问题上的日志,发现了一些东西。在这个问题上,kill信号成功终止,但由于权限原因,nginx 未释放文件句柄。就我而言,invoke-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):
( last two lines ... )
running postrotate script
error: error running shared postrotate script for '/var/log/nginx/*.log '

当我使用您在 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.

这是一个 bug在 Nginx 中。所以我所做的是用另一个线程上的人正在使用的命令替换该命令。所以现在我在配置文件上的 logrotate/nginx postrotate 脚本是
postrotate
kill -USR1 `cat /run/nginx.pid`
endscript

这解决了这个问题。

关于logging - Nginx 记录到 access.log.1 而不是 access.log,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39194819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com