大网数据广告活动
服务器运维问答

nginx not logging errors to log files

· 52CE 我爱测

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,…

问题原文

原帖链接 · J.W.F. · CC BY-SA 3.0

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?

已采纳解答

原帖链接 · Mark Stosberg · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-11-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。