nginx still opens the default error log after pointing it to another config
nginx opens logs/error.log at startup although error_log is set in the configShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
The 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
Logs turn up under /usr/local/nginx or fail thereA relative path resolves against the wrong prefixFix - 3
Your file names a log, yet the default file is still used by the main processerror_log is set only in an inner contextFix
- 1
What does the error look like?
nginx opens logs/error.log at startup although error_log is set in the confignginx 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/logs2Fix
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 stderr3Verify
Start nginx again. The alert is gone and the file is created.
ls -l /home/me/logs/error.logConfirmed by nginx.org
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.conf3Verify
Logs appear under your prefix's logs directory.
ls /home/me/nginx/logsConfirmed by nginx.org
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 reloadConfirmed by nginx.org
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
Servers
nginx fails on OpenShift with mkdir() client_temp failed (13: Permission denied)
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (13: Permission denied)
OpenShift starts the container under a random user ID that belongs to the root group, whatever USER your image sets.
3 causes, each with a fixServers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fixServers
curl cannot connect to port 80 or 443 on a server running nginx
CURLE_COULDNT_CONNECT (7): Failed to connect() to host or proxy
Exit code 7 means the TCP connection itself failed, before any HTTP happened.
3 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
Your website on a Google Compute Engine VM times out on port 80
The site works with curl on the VM but not from outside
A timeout, as opposed to a refusal, usually means a firewall dropped the packets.
3 causes, each with a fixServers
Gunicorn rejects a request with "Error parsing headers: limit request headers fields size"
Error parsing headers: 'limit request headers fields'
Gunicorn caps the size of one header field at 8190 bytes and the number of headers at 100 by default.
2 causes, each with a fix