Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :(

like image 306
Ivan Kleshnin Avatar asked Feb 08 '15 12:02

Ivan Kleshnin


People also ask

How to log Nginx error messages to stderr?

According to the nginx documentation, the error_logdirective supports stderras its argument. The following configuration should therefore log error messages to stderr: http { error_log stderr; ...

How do I configure logging in Nginx?

Configuring Logging 1 Setting Up the Error Log. NGINX writes information about encountered issues of different severity levels to the error log. ... 2 Setting Up the Access Log. ... 3 Enabling Conditional Logging. ... 4 Usecase: Sampling TLS Parameters. ... 5 Logging to Syslog. ... 6 Live Activity Monitoring. ...

How to send Nginx access and error log to Docker?

By default, the NGINX image is configured to send NGINX access log and error log to the Docker log collector. This is done by linking them to stdout and stderr 1.

What to do if Nginx is unable to start or stop running?

Therefore if NGINX is unable to start or abruptly stopped running then you should check the error logs to find more details. You may also find few warnings in the error log but it does not indicate that a problem has occurred but the event may pose a serious issue in the near future.


1 Answers

server {
    error_log syslog:server=unix:/dev/log;
    access_log syslog:server=unix:/dev/log;
    ...
}

Default journald configuration (I'm running Ubuntu 15.04) reads from syslog, so this config is all it takes to have logs be viewable with journalctl -u nginx

like image 112
m.kocikowski Avatar answered Sep 17 '22 12:09

m.kocikowski