nginx/nginx · warning

NGX_LOG_ALERT

NGX_LOG_ALERT

Error message

inflateInit2() failed: %d

What it means

The gunzip filter failed to initialize zlib for decoding a gzip-encoded response: inflateInit2(MAX_WBITS + 16) returned something other than Z_OK. zlib only fails init with Z_MEM_ERROR (or Z_STREAM_ERROR for invalid args, which cannot occur here since windowBits is fixed at 47 for gzip), so this is effectively an out-of-memory condition logged at ALERT.

Source

Thrown at src/http/modules/ngx_http_gunzip_filter_module.c:318

static ngx_int_t
ngx_http_gunzip_filter_inflate_start(ngx_http_request_t *r,
    ngx_http_gunzip_ctx_t *ctx)
{
    int  rc;

    ctx->zstream.next_in = NULL;
    ctx->zstream.avail_in = 0;

    ctx->zstream.zalloc = ngx_http_gunzip_filter_alloc;
    ctx->zstream.zfree = ngx_http_gunzip_filter_free;
    ctx->zstream.opaque = ctx;

    /* windowBits +16 to decode gzip, zlib 1.2.0.4+ */
    rc = inflateInit2(&ctx->zstream, MAX_WBITS + 16);

    if (rc != Z_OK) {
        ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
                      "inflateInit2() failed: %d", rc);
        return NGX_ERROR;
    }

    ctx->started = 1;

    ctx->last_out = &ctx->out;
    ctx->flush = Z_NO_FLUSH;

    return NGX_OK;
}


static ngx_int_t
ngx_http_gunzip_filter_add_data(ngx_http_request_t *r,
    ngx_http_gunzip_ctx_t *ctx)
{
    ngx_chain_t  *cl;

View on GitHub (pinned to 3f6f7824d4)

Solutions

  1. Check nginx memory usage and limits at the time of the alert (dmesg for OOM kills, container/cgroup limits)
  2. Reduce concurrent gunzip work: lower worker_connections or gzip_buffers pressure, or stop the upstream from gzipping payloads nginx must decompress for non-gzip clients
  3. If recurrent without memory pressure, suspect a broken zlib build and rebuild against stock zlib
Defensive patterns

Strategy: retry

Validate before calling

# no config-level pre-check exists; watch the leading indicators instead:
# worker RSS and cgroup memory pressure
for p in $(pgrep -f 'nginx: worker'); do awk '/VmRSS/ {print $2}' /proc/$p/status; done

Try / catch

Let nginx retry at the request level (clients see one failed response); fix the memory condition rather than catching — the next request retries inflateInit2 naturally.

Prevention

When it happens

Trigger: First gzip buffer of a response arrives (Content-Encoding: gzip, client without gzip support, gunzip on) and zlib's inflateInit2 cannot allocate its internal window/state due to process memory exhaustion.

Common situations: nginx under memory pressure (cgroup limits, rlimits, or memory leaks elsewhere), very high concurrency inflating many large streams at once; typically transient.

Related errors


AI-assisted analysis of nginx/nginx@3f6f7824d4 (2026-08-22). Data as JSON: /api/errors/7b6f15dd3bc42a7c. Report an issue: GitHub.