Nginx cannot write into access.log
Asked Answered
A

4

30

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

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?

Alcaeus answered 4/3, 2012 at 6:40 Comment(0)
C
22

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

To see users and groups for nginx processes:

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:

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.

See also nginx http_log_module docs.

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.

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

See also log rotation guide for ubuntu.

Captivity answered 20/3, 2014 at 1:58 Comment(4)
logrotate is not a daemon, its a cronjobPietro
Good point @Sebastian. In this case the definition of daemon is derived from the ancient greek: 'a benevolent or benign nature spirit'. ;-)Captivity
logrotate's true nature :) @CaptivityPietro
Wonderful information, thanks; it was logrotate giving me issues!Fernyak
B
9

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

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

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

Brush answered 25/5, 2016 at 6:19 Comment(1)
Just doing sudo service nginx restart on Ubuntu made it start writing the Nginx acces.log again for me. (after I corrected the permissions of the right user)Boyles
G
3

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

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

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

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

Grainy answered 4/3, 2012 at 7:25 Comment(3)
it's www-data:www-data in ubuntuQuits
If nginx is unable to write to the logs it created in the first place, chmod'ing the files is not tackling the root cause of the problem. You probably need to look at the nginx config.Labanna
@Quits by default, yes.Benzine
W
0

One situation I have encountered is that the disk is full

Willywilly answered 28/7, 2020 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.