Nginx access log to standard output (or journald)

submited by
Style Pass
2024-10-27 02:00:05

It is not as easy as using /dev/stdout. TLDR: Use access_log syslog:server=unix:/dev/log;. Even if you use /dev/stdout, Nginx is always going to open a new FD from /dev/stdout as a regular file / PTY. This is not supported in systemd.

So, one day you want to migrate from /var/log/nginx/access_log.* to a more standarized way of logging, say, systemd-journald. Then, you remember that systemd by default redirects standard output of services to the journal. And you tries to figure out how to get Nginx to log to stdout and stderr instead of the default log files.

The error log is fairly easy. Nginx docs on ngx_core_module#error_log says you can just put error_log stderr;, and it just works.

However, the access log is way more complicated. First, you don’t find anything on ngx_http_log_module#access_log mentioning stdout. Then, if you Google “nginx access_log stdout”, you get plenty of blogs and Stack Overflow answers saying: just put access_log /dev/stdout; and it works!

The fact is, most people willing to get Nginx to log to stdout are using Docker, which has a volatile /var/log/, and it makes sense to log to stdout. Moreover, inside Docker containers, /dev/stdout are just plain files or ptys that can be open(2)‘ed whenever we like.

Leave a Comment