Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx cannot write into access.log

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?

like image 877
Seth Avatar asked Mar 04 '12 06:03

Seth


People also ask

How do I access nginx logs?

Configure NGINX access log By default, the access log is located at /var/log/nginx/access. log , and the information is written to the log in the predefined combined format. You can override the default settings and change the format of logged messages by editing the NGINX configuration file ( /etc/nginx/nginx.

How do I disable nginx access logs?

If you wish to turn off the Nginx error logs completely, you need to change the line to : error_log /dev/null crit; This will completely turn off the Nginx error logs on your server.

How do I change the log format in nginx?

The syntax for configuring a log format is: log_format format_name 'set_of_variables_to_define_format'; and the syntax for configuring access log is: access_log /path/to/log_file format_name; #simplest form OR access_log /path/to/log_file [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];


1 Answers

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.

like image 158
Mark Avatar answered Sep 25 '22 10:09

Mark