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

我最近将我的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;
    }
}

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

-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

为什么没有存储日志可能会出现什么问题?

最佳答案
这里的Nginx语法是正确的,因此问题与服务器环境有关.以下是一些可以调试它的方法.

>模拟Web服务器将要执行的操作.尝试使用Web服务器将使用的同一用户写入文件.如果Web服务器用户是nginx,那么以root身份尝试:
su -m nginx -c’echo“testing”>> /var/www/example.com/logs/example.com_error.log’
>观察Web服务器正在做什么.作为一个临时的调试措施,在你的Nginx配置的顶层添加daemon off.然后停止Nginx并从它开始strace nginx | tee nginx-output.txt.然后它将在前台运行并将它对STDOUT进行的所有系统调用喷出,这也将在nginx-output.txt中捕获. Do Something触发错误,然后您可以停止Nginx,删除“守护进程”行并在查看调试日志时正常启动它.在其中搜索有关文件名的提及.也许你会在那里找到它,系统调用返回的错误试图访问该文件.

另一种可能性是你不同意Nginx关于应该出现在错误日志中的内容.如果是nginx -V | grep debug获取结果,然后您可以将error_log的最后一个参数从error更改为debug.重新加载Nginx并点击域中不存在的URL,这应该会生成大量日志记录.完成后关闭调试日志记录.

相关文章

文章浏览阅读3.7k次,点赞2次,收藏5次。Nginx学习笔记一、N...
文章浏览阅读1.7w次,点赞14次,收藏61次。我们在使用容器的...
文章浏览阅读1.4k次。当用户在访问网站的过程中遇到404错误时...
文章浏览阅读2.7k次。docker 和 docker-compose 部署 nginx+...
文章浏览阅读1.3k次。5:再次启动nginx,可以正常启动,可以...
文章浏览阅读3.1w次,点赞105次,收藏182次。高性能:Nginx ...