Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

nginx still opens the default error log after pointing it to another config

Errornginx opens logs/error.log at startup although error_log is set in the config

Short answer

nginx opens its built-in default error log file before it has read your configuration, and the default is logs/error.log under the prefix. If you cannot write there, startup complains. Pass -e with a path you can write to, or set -p to a prefix you own, and put error_log in the main context.

  • Applies to nginx 1.26, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the configuration nginx actually loads and see where error_log is set, then check which prefix the binary uses.

    nginx -T 2>&1 | grep -n "error_log"
    nginx -V 2>&1 | tr ' ' '\n' | grep -E "prefix|error-log"
  2. 2Match what you see, then jump to its fix

    1. 1The alert appears when you run nginx -c ... as a normal user, or in a container with a read-only pathThe default error log path is not writable by your userFix
    2. 2Logs turn up under /usr/local/nginx or fail thereA relative path resolves against the wrong prefixFix
    3. 3Your file names a log, yet the default file is still used by the main processerror_log is set only in an inner contextFix

What does the error look like?

  • nginx opens logs/error.log at startup although error_log is set in the config
  • nginx fails to start as a normal user because the default log path is not writable

Cause 1 of 3

The default error log path is not writable by your user

1Check

The error_log default is logs/error.log, relative to the prefix, and nginx opens it at startup. The default prefix is /usr/local/nginx unless -p or a build option changes it.

ls -ld /usr/local/nginx/logs

2Fix

Tell nginx to write its error log elsewhere with -e (available from 1.19.5). The special value stderr works too, which suits containers.

nginx -c /home/me/nginx.conf -e /home/me/logs/error.log
nginx -c /home/me/nginx.conf -e stderr

3Verify

Start nginx again. The alert is gone and the file is created.

ls -l /home/me/logs/error.log

Confirmed by nginx.org

Did this fix it?

Cause 2 of 3

A relative path resolves against the wrong prefix

1Check

Relative paths in the configuration, including the default log location, are resolved against the prefix set by -p.

nginx -T 2>&1 | grep -nE "access_log|error_log"

2Fix

Run nginx with a prefix directory you own, or use absolute paths in the config.

mkdir -p /home/me/nginx/logs
nginx -p /home/me/nginx -c /home/me/nginx.conf

3Verify

Logs appear under your prefix's logs directory.

ls /home/me/nginx/logs

Confirmed by nginx.org

Did this fix it?

Cause 3 of 3

error_log is set only in an inner context

1Check

The docs allow error_log in main, http, server and location, and say the default file is used unless one is set at the main level.

nginx -T 2>&1 | grep -n -B2 "error_log"

2Fix

Add error_log in the main context, at the top level of nginx.conf, before the http block.

error_log /var/log/nginx/error.log warn;
events {}
http { }

3Verify

Test the config and reload.

sudo nginx -t && sudo nginx -s reload

Confirmed by nginx.org

Did this fix it?

How do I stop it coming back?

  • In containers, log to stderr so the platform collects it.
  • Use absolute paths for logs and pid files in every custom config.
  • Create and chown the log directory before starting nginx as a non-root user.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.