8

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?

2
  • 1
    Does the nginx user have access to the /var/www/example.com/logs/ directory? Commented Nov 12, 2015 at 4:55
  • @AaronMason The directory is owned by root (user and group), but it's the exact same permissions as other server blocks on the machine that are successfully able to write to logs. Commented Nov 12, 2015 at 13:50

1 Answer 1

17

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.

2
  • 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) Commented Dec 3, 2020 at 8:36
  • Another possibility is to simply avoid the nightmare of using nginx. Commented Dec 27, 2023 at 19:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.