← All posts
Guides· 6 min read

How to Read and Make Sense of Nginx Error Logs

Decode nginx error logs fast: find log file locations, understand severity levels, run the right grep commands, and fix the most common errors.

By , Director of Softuvo Solutions (LinkedIn) · Published · Updated

Your site is behaving strangely — requests timing out, assets returning 502s, a form that worked yesterday throwing errors today. You SSH in and stare at /var/log/nginx/error.log. It scrolls past in a blur of timestamps and cryptic messages. If you've ever closed that terminal window and hoped the problem would go away, this guide is for you. By the end you will know where nginx writes its logs, how to read them in real time, how to turn up verbosity safely for debugging, and what the most common error messages actually mean.

Where nginx writes its logs

Before you can view nginx logs, you need to know where they live. The defaults vary slightly by distribution and how nginx was installed.

  • Debian/Ubuntu (apt install nginx): /var/log/nginx/access.log and /var/log/nginx/error.log
  • RHEL/CentOS/Amazon Linux (yum/dnf install nginx): /var/log/nginx/access.log and /var/log/nginx/error.log
  • Compiled from source with default prefix: /usr/local/nginx/logs/error.log
  • Docker official image: nginx writes to /dev/stderr (error) and /dev/stdout (access) so logs go to the container log driver

If you are not sure where your installation writes logs, check the compiled-in default and any override in the config:

# Show the compiled-in default paths
nginx -V 2>&1 | grep -- '--error-log-path'

# Show the active error_log directive across all loaded config files
nginx -T 2>/dev/null | grep error_log

nginx -T dumps the full merged configuration, including every included file, so the error_log lines it prints are exactly what nginx is using right now. If the directive appears more than once, the innermost context (a server or location block) overrides the outer one.

You can also confirm which files nginx has open with:

sudo ls -l /proc/$(pgrep -f 'nginx: master')/fd | grep log

That lists every file descriptor the master process holds open, which will include the error log even if the path is non-standard.

The anatomy of an nginx error log line

Every line in the nginx error log follows a consistent structure. Once you recognise it, you can extract meaning in seconds rather than minutes. Here is a typical entry:

2024/05/14 03:22:11 [error] 1234#1234: *89 connect() failed (111: Connection refused) while connecting to upstream, client: 203.0.113.42, server: example.com, request: "GET /api/data HTTP/1.1", upstream: "http://127.0.0.1:3000/api/data", host: "example.com"

Breaking that down: the timestamp comes first (yyyy/mm/dd hh:mm:ss), then the severity level in brackets, then the worker process ID and thread ID separated by a hash, then an asterisk followed by the connection counter. After that comes the actual message, followed by context fields: client IP, server block name, the original request line, and (when relevant) the upstream target URI. The context fields are often more useful than the message itself, because they tell you exactly which request triggered the error and where nginx was trying to send it.

Severity levels and which ones actually matter

Nginx uses eight severity levels, borrowed from syslog: debug, info, notice, warn, error, crit, alert, and emerg. Your log level is set with the error_log directive. The syntax is:

# In nginx.conf, http {}, server {}, or location {}
error_log /var/log/nginx/error.log warn;

# The level controls the minimum severity written.
# Setting 'warn' means warn, error, crit, alert and emerg are written;
# debug, info and notice are silently dropped.

Most production servers run at warn or error. Here is a practical guide to what you will actually see:

  • debug — every step nginx takes internally. Only useful for tracing a specific problem. Never run this in production for more than a few minutes; it fills disk fast.
  • info — informational events such as a client opening or closing a connection. Rarely actionable.
  • notice — significant events like nginx starting or reloading its config.
  • warn — worth a glance but rarely urgent. Often a client closing a connection before nginx finished sending a response.
  • error — the level you will spend most of your time in. Failed upstream connections, permission denied on files, timeouts: all land here.
  • crit — nginx could not do something important, like open a socket or read a config file after reload. Investigate immediately.
  • alert / emerg — nginx is either refusing to start or is in serious trouble. These are rare and require immediate action.

How to view nginx logs in real time and search them

Tailing the raw log file is fine for watching live traffic, but for diagnosing a specific problem you need filtering. These are the commands worth keeping in your muscle memory:

# Watch errors arrive in real time
tail -f /var/log/nginx/error.log

# Show the last 100 error-level entries, filtering out warn noise
grep '\[error\]' /var/log/nginx/error.log | tail -100

# Count upstream errors by host to spot a failing backend fast
grep 'upstream' /var/log/nginx/error.log \
  | awk '{print $NF}' | sort | uniq -c | sort -rn

# Narrow down to a specific date when you know roughly when something broke
grep '2024/05/14' /var/log/nginx/error.log

# Search across rotated and compressed logs when the incident happened days ago
zcat /var/log/nginx/error.log.*.gz | grep 'connect() failed'

# Show only crit and above for a quick severity triage
grep -E '\[(crit|alert|emerg)\]' /var/log/nginx/error.log

For access logs, pair them with error logs. If you see a spike of 502s in error.log, run this to see which URL paths are affected:

grep ' 502 ' /var/log/nginx/access.log \
  | awk '{print $7}' | sort | uniq -c | sort -rn

Cross-referencing the two logs together almost always points you to the root cause faster than either log alone. The access log tells you the volume and the paths; the error log tells you why nginx could not fulfil the request.

How to enable nginx verbose logging and debug logging safely

Sometimes error-level output is not enough. You need to see exactly what nginx negotiated with an upstream, or trace a rewrite rule, or confirm which SSL certificate is being selected. That is when you temporarily increase the log level to debug.

The safest way to do this in production is to scope debug logging to a single server block or to a single client IP, rather than enabling it globally. Enabling debug globally on a busy server can write hundreds of megabytes per minute.

# Option 1: enable debug logging for one server block only
# Edit the relevant server block in /etc/nginx/sites-enabled/yoursite.conf:
server {
    listen 443 ssl;
    server_name example.com;

    # Write debug output to a separate file for this vhost only
    error_log /var/log/nginx/example.com-debug.log debug;

    # ... rest of config
}

# Test and reload
nginx -t && systemctl reload nginx

# Reproduce the problem, then tail the debug log
tail -f /var/log/nginx/example.com-debug.log

# When done, remove the debug directive and reload again
# nginx -t && systemctl reload nginx
# Option 2: enable debug logging for one client IP only (nginx must be compiled
# with --with-debug, which the packaged version on Debian/Ubuntu/RHEL is)
# In the main events {} block in nginx.conf:
events {
    debug_connection 203.0.113.42;  # your IP only
}

# The global error_log must be set to debug for debug_connection to take effect:
error_log /var/log/nginx/error.log debug;

# Reload, reproduce from that IP, then revert both changes.

Check whether your nginx binary was compiled with debug support before relying on debug_connection:

nginx -V 2>&1 | grep -- '--with-debug'
# If the flag appears in the output, debug mode is available.

A few things to watch when running verbose logging. Disk usage can grow extremely fast, so tail the log and watch df -h / in another terminal. Never leave debug logging enabled overnight without log rotation in place. Once you have reproduced the problem and captured what you need, revert the config and reload nginx immediately.

The most common errors and what they mean

Rather than cataloguing every possible message, here are the ones that appear on most production servers, with what to check first for each.

  • connect() failed (111: Connection refused) while connecting to upstream — your upstream service (Node, Gunicorn, PHP-FPM, etc.) is not listening on the expected address or port. Check whether the process is running (systemctl status your-service or ss -ltnp | grep :PORT) and whether the upstream address in your nginx config matches reality.
  • recv() failed (104: Connection reset by peer) — the upstream accepted the connection but dropped it mid-response. Usually means the backend process crashed while handling the request. Check your application logs.
  • upstream timed out (110: Connection timed out) — the backend is alive but too slow. Your proxy_read_timeout or proxy_connect_timeout values may need adjustment, or the backend has a performance problem. See the guide on How to Reduce Nginx 502 and 504 Gateway Errors for the exact timeout directives and safe values.
  • open() failed (13: Permission denied) — nginx cannot read a file it needs to serve. Check file ownership and permissions. The nginx worker process typically runs as www-data or nginx; that user needs read access. Run ls -la on the file and compare with ps aux | grep nginx to confirm the worker user.
  • SSL_do_handshake() failed — TLS negotiation failed. Often a misconfigured certificate, a client using an unsupported protocol version, or a certificate that has expired. Check ssl_protocols and ssl_ciphers in your config, and verify the certificate chain with openssl s_client -connect example.com:443.
  • no live upstreams while connecting to upstream — all servers in an upstream block are marked down. This usually follows a wave of connection refused errors. Check each backend listed in your upstream {} block.
  • could not build server_names_hash — the server_names_hash_bucket_size is too small for your longest server_name. Increase it in nginx.conf (typical fix: server_names_hash_bucket_size 64;) and reload.
  • worker_connections are not enough — you are hitting the per-worker connection limit. See the guide on nginx worker_processes and worker_connections: how to tune them, with numbers for the exact settings and how to measure whether you are actually hitting the limit before changing anything.

Configuring log rotation so you have history when you need it

Logs you cannot search are useless. Most distributions configure logrotate for nginx automatically, but it is worth verifying and adjusting the retention.

# Check the active logrotate config for nginx
cat /etc/logrotate.d/nginx

# A reasonable production config
/var/log/nginx/*.log {
    daily
    missingok
    rotate 30          # keep 30 days
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        # Signal nginx to reopen log files after rotation
        [ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)
    endscript
}

# Test the config without rotating
logrotate --debug /etc/logrotate.d/nginx

The kill -USR1 signal tells nginx to close the old file descriptor and open a new one with the original filename. Without this step, nginx keeps writing to the rotated file (now renamed) and the fresh log file stays empty. If you use systemd's nginx.service instead, you can replace the postrotate block with systemctl kill -s USR1 nginx.

Thirty days of compressed daily logs is a practical minimum for most teams. If an incident is reported a week after it happened, you will want that history. The compressed files are small; storage cost is rarely a reason to keep fewer than 30 rotations.

Building a faster mental model and catching problems before users do

The pattern with nginx error logs is always the same: find the first occurrence of an error, identify the upstream or file involved, cross-reference with your application logs, then fix and verify. The hard part is not reading individual lines — it is noticing when a new error type has appeared, or when an error rate quietly doubles over two hours while you are focused on something else.

Even without additional tooling, a few habits make a real difference. Check error.log before users report problems, not after. Keep log rotation sane so you have at least 30 days of history. Make sure your log format includes the upstream field if it does not already. Set up a cron job that mails you a daily count of crit entries and above. These small habits turn nginx logs from an intimidating wall of text into a genuinely useful signal.

That said, manually grepping logs after every incident is slow. Opservo watches your nginx error log continuously, clusters repeated messages into patterns, and tells you in plain English when an error rate shifts — so you find out about a slow bleed before it becomes a full outage, without having to remember which grep flags to use at 2 am.

Read next in the application and service troubleshooting series: How to Reduce Nginx 502 and 504 Gateway Errors and nginx worker_processes and worker_connections: how to tune them, with numbers. The whole series is at Application and service troubleshooting.