Search code examples
loggingnginxstderrsystemd

Nginx log to stderr


I want to redirect nginx access logs to stdout to be able to analyze them through journalctl (systemd).

There is the same question with approved answer. Have nginx access_log and error_log log to STDOUT and STDERR of master process But it does not work for me. With /dev/stderr I get open() "/dev/stderr" failed (6: No such device or address). With /dev/stdout I get no access logs in journalctl -u nginx.

nginx.conf

daemon off;

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

sitename.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

I've tried my best to workaround that by changing every possible parameter in the code above and with different nginx versions (1.2, 1.6) but without any success.

I'm really, really interested how to make this work so I raise this question again on a different thread as I consider previous answer is wrong, speculative or environment specific.

$ journalctl -u nginx

contains only lines like

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

and no sign of access logs :(


Solution

  • According to the nginx documentation, the error_log directive supports stderr as its argument. The following configuration should therefore log error messages to stderr:

    http {
        error_log stderr;
        ...
    }
    

    Unfortunately, access_log doesn't support stdout as its argument. However, it should be possible to set it to syslog (documentation) and get systemd to include syslog calls into its journal.