【问题标题】:Nginx log to stderrNginx 日志到标准错误
【发布时间】:2015-04-08 06:23:56
【问题描述】:

我想将 nginx 访问日志重定向到 stdout 以便能够通过 journalctl (systemd) 对其进行分析。

有相同的问题与批准的答案。 Have nginx access_log and error_log log to STDOUT and STDERR of master process 但这对我不起作用。使用/dev/stderr,我得到open() "/dev/stderr" failed (6: No such device or address)。使用/dev/stdout,我在journalctl -u nginx 中没有访问日志。

nginx.conf

daemon off;

http {
    access_log /dev/stdout;
    error_log /dev/stdout;
    ...
}
...

网站名称.conf

server {
    server_name sitename.com;
    root /home/username/sitename.com;

    location / {
        proxy_pass http://localhost:3000/;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        access_log on;
    }
}

nginx.service

[Service]
Type=forking
PIDFile=/run/nginx.pid
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nginx
ExecStartPre=/usr/sbin/nginx -t -q -g 'master_process on;'
ExecStart=/usr/sbin/nginx -g 'master_process on;'
ExecReload=/usr/sbin/nginx -g 'master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5

我已尽我所能通过更改上述代码中的每个可能参数并使用不同的 nginx 版本(1.2、1.6)来解决此问题,但没有任何成功。

我真的非常非常感兴趣如何完成这项工作,所以我在另一个线程上再次提出这个问题,因为我认为之前的答案是错误的、推测性的或特定于环境的。

$ journalctl -u nginx

只包含像

这样的行
 Feb 08 13:05:23 Username systemd[1]: Started A high performance web server and a reverse proxy server.

并且没有访问日志的迹象:(

【问题讨论】:

    标签: logging nginx stderr systemd


    【解决方案1】:
    server {
        error_log syslog:server=unix:/dev/log;
        access_log syslog:server=unix:/dev/log;
        ...
    }
    

    默认的日志配置(我运行的是 Ubuntu 15.04)从 syslog 读取,所以这个配置就是让日志可以通过 journalctl -u nginx 查看的全部内容

    【讨论】:

    • stderr 对我不起作用,但这个配置可以。谢谢!
    【解决方案2】:

    根据nginx documentationerror_log 指令支持stderr 作为其参数。因此,以下配置应将错误消息记录到 stderr:

    http {
        error_log stderr;
        ...
    }
    

    不幸的是,access_log 不支持 stdout 作为其参数。但是,应该可以将其设置为 syslog (documentation) 并让 systemd 将 syslog 调用包含到其日志中。

    【讨论】:

      猜你喜欢
      • 2016-01-16
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      相关资源
      最近更新 更多