Nginx 仍然尝试打开默认错误日志文件,即使我在重新加载时设置了 nginx 配置文件

Nginx still try to open default error log file even though I set nginx config file while reloading

下面是我的nginx配置文件位于/etc/nginx/nginx.conf

user Foo;
worker_processes 1;

error_log /home/Foo/log/nginx/error.log;
pid /home/Foo/run/nginx.pid;

events {
    worker_connections 1024;
    use epoll;
}

http {
    access_log /home/Foo/log/nginx/access.log;

    server {
        listen 80;

        location = / {
            proxy_pass http://192.168.0.16:9999;
        }
    }
}

如您所见,我将日志、pid 文件位置更改为主目录。

当我重新启动时 Linux 它似乎工作了,Nginx 在我设置的文件和 pid 文件中也记录了错误日志。

但是,当它尝试 nginx -s reload 或其他时,它会尝试打开其他错误日志文件。

nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2015/12/14 11:23:54 [warn] 3356#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2015/12/14 11:23:54 [emerg] 3356#0: open() "/home/Foo/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /etc/nginx/nginx.conf test failed

我知道,我可以用 sudo 解决权限错误,但这里的主要问题是错误日志文件 (/var/log/nginx/error.log) Nginx 尝试打开。

为什么它会尝试访问另一个错误日志文件?

检查目录 /home/Foo/log/nginx/ 的权限。它必须是 nginx 可写的。像这样设置权限:

sudo chmod 766 /home/Foo/log/nginx

这是旧的...但我经历过同样的痛苦,这是我的解决方案。

如您所见,日志是警报,而不是阻塞错误:

nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)

这应该不是问题:) Nginx 只是喜欢在启动时检查该文件...

只需使用 -p 选项。像这样在本地启动 Nginx 对我有用:

nginx -c /etc/nginx/nginx.conf -g 'daemon off;' -p /home/Foo/log/nginx

是的,Nginx 只是喜欢在启动时检查该文件。我把nginx的安装目录复制到另一个地方,启动它,新的Nginx的pid还在老地方。所以我建议你删除旧目录。

或者使用 sudo 重新加载 nginx

sudo nginx -s reload

您可能需要用 sudo

触发它
sudo nginx -t

这个简单的答案是使用 sudo。

所以当我使用sudo nginx -t

一切都很好。

顺便说一句,当我在 Ubuntu 18.04 上增加 PHP.INI 中的文件上传限制时,我遇到了这个错误,我重新启动了我的 PHP 和我的 NGINX 等等当我测试时:

2020/10/19 20:27:43 [warn] 1317#1317: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
2020/10/19 20:27:43 [emerg] 1317#1317: BIO_new_file("/etc/letsencrypt/live/websitename.com/fullchain.pem") failed (SSL: error:0200100D:system library:fopen:Permission denied:fopen('/etc/letsencrypt/live/websitename.com/fullchain.pem','r') error:2006D002:BIO routines:BIO_new_file:system lib)
nginx: configuration file /etc/nginx/nginx.conf test failed

警报来自 nginx 初始化过程,当它检查是否可以写入已使用 --error-log-path 配置标志编译的错误日志路径时。这发生在 nginx 甚至查看你的配置文件之前,所以你在里面写什么并不重要。

最近(2020-11-19),一个-e选项被添加到nginx,允许您覆盖已经编译的错误日志路径。您可以使用该选项将nginx指向一个用户可写文件(或者可能 stderr)。

https://trac.nginx.org/nginx/changeset/f18db38a9826a9239feea43c95515bac4e343c59/nginx

您将收到此警报,因为您的用户没有修改日志文件的权限。我只是将权限分配给 Nginx 日志文件,它按预期工作。 只需使用此命令。

sudo chmod 766 /var/log/nginx/error.log