user1950164
user1950164

Reputation:

Nginx logging to access.log.1 instead of access.log

I know this has been asked before, but I believe this is a different issue.

Nginx runs under www-data:

$ ps -eo "%U %G %a" | grep nginx
root     root     nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data www-data nginx: worker process

/var/log/nginx/* have the right permissions:

$ ls -lah /var/log/nginx/
total 291M
drwxr-x---  2 www-data adm     4.0K Jul 25 06:25 .
drwxrwxr-x 14 root     syslog  4.0K Aug 28 06:25 ..
-rw-r-----  1 www-data adm      12K Aug 28 19:03 access.log
-rw-r-----  1 www-data adm     250M Aug 28 18:50 access.log.1

Logrotate creates the log files with the right permissions:

/var/log/nginx/*.log {
        ( ... )
        create 0640 www-data adm

Nginx logs to access.log when it is restarted but moves to access.log.1 after logrotate runs the first time. After that, always logs to access.log.1, and log files are not rotated after that.

EDIT: Has pointed out in the comments, the reason why you see access.log having been accessed at a later time than access.log.1 was because I had restarted nginx just before I did that ls, just to make sure for myself before posting on here, that indeed restarting nginx was fixing the problem (until the next logrotate). But before that ls nginx had been logging to access.log.1 for about 3 weeks...

EDIT2: Here's /etc/nginx/nginx.conf, the head and the bit that mentions logging

user www-data;
worker_processes auto;
pid /run/nginx.pid;

( ... )

http {

        ( ... )
        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ( ... )

Upvotes: 4

Views: 4353

Answers (2)

nvvetal
nvvetal

Reputation: 1793

Some more good solution from here

https://unix.stackexchange.com/questions/186807/what-does-nginx-s-reopen-do

So:

postrotate
  invoke-rc.d nginx -s reopen >/dev/null 2>&1
endscript

Upvotes: 0

user1950164
user1950164

Reputation:

Solved.

My problem was almost like this but not quite. In that one, the author eventually solved the problem and said the issue was that "nginx was not releasing the file handle to the log file upon receiving the -USR1 signal from kill. Long story short, the reason it was not reloading the log files was because the /var/log/nginx folder was not owned by the same user as the nginx worker processes (owned by www-data, running under web)." As we've seen, that's not my problem, because my permissions are correct. However, I went and compared my logrotate log with the one on that question, and found something. On that question, the kill signal terminates successfully, but the file handle isn't released by nginx because of permissions. In my case, the invoke-rc.d command does not terminate successfully. The logrotate config for nginx is the following:

/var/log/nginx/*.log {
        weekly
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 0640 www-data adm
        sharedscripts
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi \
        endscript
        postrotate
                invoke-rc.d nginx rotate >/dev/null 2>&1
        endscript
}

Note the postrotate script, that's the command that tells nginx to do its thing, which for the author on the other thread was the kill signal. In my logrotate log I get the following error (btw you can force logrotate by doing sudo logrotate -f -v /etc/logrotate.d/nginx):

( last two lines ... )
running postrotate script
error: error running shared postrotate script for '/var/log/nginx/*.log '

When I take the postrotate script that you see in the logrotate/nginx config and execute it by hand, it errors:

$ invoke-rc.d nginx rotate
initctl: invalid command: rotate
Try `initctl --help' for more information.
invoke-rc.d: initscript nginx, action "rotate" failed.

This is a bug in nginx. So what I did was replace that command with the one that the guy on the other thread is using. So now my logrotate/nginx postrotate script on the config file is

postrotate
        kill -USR1 `cat /run/nginx.pid`
endscript

This solves the issue.

Upvotes: 6

Related Questions