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?

Seth
  • 558
  • 2
  • 7
  • 12

4 Answers4

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.

Mark
  • 6,731
  • 1
  • 40
  • 38
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

Animesh Jain
  • 125
  • 1
  • 6
  • 4
    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) – program247365 Jun 28 '17 at 00:36
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"!

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
0

One situation I have encountered is that the disk is full

Qs F
  • 1