Linux Nginx 无法写入 access.log

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9552930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:59:27  来源:igfitidea点击:

Nginx cannot write into access.log

linuxnginxamazon-ec2logfile

提问by Seth

When nginx start, it creates log file "access.log" with 0 size. But no log are written in it. error.log works fine.

当 nginx 启动时,它会创建大小为 0 的日志文件“access.log”。但是里面没有写日志。error.log 工作正常。

nginx.conf:

nginx.conf:

http {
    access_log /usr/local/webserver/nginx/logs/access.log combined;
    ....
}

The logs file is:

日志文件是:

-rw-r--r--  1 root root    0 Mar  4 00:54 access.log
-rw-r--r--  1 root root 3903 Mar  4 00:54 error.log

I am totally confused. @_@

我完全糊涂了。@_@

Is it a permission issue?

是权限问题吗?

However, in the later part of nginx.conf, in the server {} section, the access_log works! Why http {} section not working?

但是,在 nginx.conf 的后面部分,在 server {} 部分,access_log 起作用了!为什么 http {} 部分不起作用?

回答by daemonTutorials

You must bind the user and group nginx to your log-files.

您必须将用户和组 nginx 绑定到您的日志文件。

chown nginx:nginx access.log
chown nginx:nginx error.log

Can you post your complete nginx.conf? With pastebin for example?

你能发布你完整的 nginx.conf 吗?以pastebin为例?

EDIT: in every section you must define the keyword like "combined"!

编辑:在每个部分中,您必须定义关键字,如“组合”!

回答by Mark

Depending on your configuration, nginx master process and worker processes likely run as different users.

根据您的配置,nginx 主进程和工作进程可能以不同的用户身份运行。

To see users and groups for nginx processes:

要查看 nginx 进程的用户和组:

ps -eo "%U %G %a" | grep nginx

root     root     nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www-data www-data nginx: worker process

The worker process user needs write permission for the log file.

工作进程用户需要日志文件的写权限。

To see file permissions of access.log:

查看 access.log 的文件权限:

ls -l /var/log/nginx/access.log
-rw-r----- 1 www-data www-data 0 Apr 29  2012 /var/log/nginx/access.log

In this case, access log is owned by the nginx worker process and has write access.

在这种情况下,访问日志归 nginx 工作进程所有,并且具有写访问权限。

See also nginx http_log_moduledocs.

另请参阅 nginx http_log_module文档。

As a secondary issue, nginx logs may be rotated once they reach a certain size by the logrotate cronjob. When the new log file is created, it should be created with owner, group and permissions to allow the nginx worker process to write to it.

作为次要问题,nginx 日志可能会在 logrotate cronjob 达到特定大小后进行轮换。创建新的日志文件时,应该使用所有者、组和权限创建它,以允许 nginx 工作进程对其进行写入。

These log rotation settings for nginx are defined in /etc/logrotate.d/nginx

nginx 的这些日志轮换设置在 /etc/logrotate.d/nginx 中定义

See also log rotation guide for ubuntu.

另请参阅ubuntu 的日志轮换指南

回答by Animesh Jain

I had a similar problem where the access logs file was not getting written to, but the error log file was working fine. The permissions were also fine for me. I got it to fix itself by forcing the nginx process to reload log files using

我有一个类似的问题,访问日志文件没有被写入,但错误日志文件工作正常。权限对我来说也很好。我通过强制 nginx 进程使用重新加载日志文件来修复它自己

kill -USR1 `cat /var/run/nginx.pid`

where /var/run/nginx.pidis the path to your nginx PID file

/var/run/nginx.pidnginx PID 文件的路径在哪里