nginx not logging errors to log files

26,600

The Nginx syntax is correct here, so the issue has to do with the server environment. Here are some things you can do to debug it.

  1. Simulate what the web server would be doing. Attempt to write to the file using the same user the web server would use. If the web server user is nginx, then try this as root: su -m nginx -c 'echo "testing">>/var/www/example.com/logs/example.com_error.log'

  2. Watch what the web server is doing. As a temporary debugging measure, at the top level of your Nginx config add daemon off. Then stop Nginx and start with it strace nginx | tee nginx-output.txt. It will then run in the foreground and spew all the system calls it makes to STDOUT, which will also be captured in nginx-output.txt. Do Something to trigger the error, and then you can stop Nginx, remove the "daemon" line and start it normally while your review the debugging log. Search in it for mentions of the file name in question. Perhaps you'll find it there, with an error returned from the system call trying to access the file.

Another possibility is that you disagree with Nginx about what should be appearing in the error log. If nginx -V | grep debug gets a result, then you can change the last argument to error_log from error to debug. Reload Nginx and hit a URL that doesn't exist at the domain, this should generate lots of logging. Turn off debug logging when done.

Share:
26,600

Related videos on Youtube

J.W.F.
Author by

J.W.F.

Updated on September 18, 2022

Comments

  • J.W.F.
    J.W.F. almost 2 years

    I recently moved my CentOS 7 machine from Apache to nginx, and I am still working out the differences. One issue I have noticed for only one of my server blocks is that access and error log files are not actually being logged to, making it difficult to troubleshoot potential issues.

    The server block for my site looks like the following:

    server {
        listen       80;
        server_name  example.com;
        root         /var/www/example.com/public_html;
        index        index.php;
        access_log   /var/www/example.com/logs/example.com_access.log;
        error_log    /var/www/example.com/logs/example.com_error.log error;
    
        location / {
            index index.php index.html;
        }
    
        location /reports {
            autoindex on;
        }
    
        location ~ \.php$ {
            try_files          $uri =404;
            fastcgi_pass       unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_param      SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include            fastcgi_params;
        }
    
        error_page 404 /var/www/html/404.html;
        error_page 500 502 503 504 /var/www/html/50x.html;
    
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
        }
    
        location /robots.txt {
            #access_log off;
            #log_not_found off;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }
    

    I am unsure why the logs would not be kept. I compared file permissions across my server blocks, and they all share the same permissions.

    -rw-r--r--. 1 nginx root    0 Nov  7 02:54 example.com_access.log
    -rw-r--r--. 1 nginx root    0 Nov  7 02:54 example.com_error.log
    

    What could be the problem with why logs are not being stored?

    • Aaron Mason
      Aaron Mason over 8 years
      Does the nginx user have access to the /var/www/example.com/logs/ directory?
  • Scott Anderson
    Scott Anderson over 3 years
    If I put 'daemon off` at the top of my nginx conf (Magento's nginx.conf.sample) and run your strace command it logs '"daemon" directive is not allowed here' (Ubuntu 20)