Nginx cannot write into access.log

Seth picture Seth · Mar 4, 2012 · Viewed 31.9k times · Source

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?

Answer

Mark picture Mark · Mar 20, 2014

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.