nginx 没有将错误记录到日志文件中

JWF*_*JWF 7 nginx log-files logging centos

我最近将我的 CentOS 7 机器从 Apache 移到了 nginx,我仍在解决这些差异。我只注意到我的一个服务器块的一个问题是访问和错误日​​志文件实际上没有被记录到,这使得很难解决潜在的问题。

我网站的服务器块如下所示:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不确定为什么不保留日志。我比较了服务器块中的文件权限,它们都共享相同的权限。

-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
Run Code Online (Sandbox Code Playgroud)

为什么不存储日志可能是什么问题?

Mar*_*erg 13

Nginx 语法在这里是正确的,所以问题与服务器环境有关。以下是您可以执行的一些操作来调试它。

  1. 模拟 Web 服务器将要做什么。尝试使用 Web 服务器将使用的同一用户写入文件。如果 Web 服务器用户是 nginx,请以 root 身份尝试: su -m nginx -c 'echo "testing">>/var/www/example.com/logs/example.com_error.log'

  2. 观察 Web 服务器在做什么。作为临时调试措施,在 Nginx 配置的顶层添加daemon off. 然后停止 Nginx 并开始使用它strace nginx | tee nginx-output.txt。然后它将在前台运行并将它对 STDOUT 进行的所有系统调用都发出来,这些调用也将在 nginx-output.txt 中捕获。做一些事情来触发错误,然后你可以停止 Nginx,删除“守护进程”行并在查看调试日志时正常启动它。在其中搜索有关文件名的提及。也许您会在那里找到它,尝试访问该文件的系统调用会返回一个错误。

另一种可能性是您不同意 Nginx 关于应该出现在错误日志中的内容。如果nginx -V | grep debug得到结果,那么您可以将最后一个参数更改为error_logfrom errorto debug。重新加载 Nginx 并访问域中不存在的 URL,这应该会生成大量日志记录。debug完成后关闭日志记录。