# upstream sent invalid content-length: 0 while reading response header from upstream

- **ID:** `nginx/upstream-sent-invalid-content-length-zero`
- **Domain:** nginx
- **Category:** protocol_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

The upstream server sent a Content-Length header with value 0 but included a response body, violating HTTP/1.1 specifications and causing nginx to reject the response.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| nginx 1.24.0 | active | — | — |
| nginx 1.22.1 | active | — | — |
| nginx 1.20.2 | active | — | — |
| nginx 1.18.0 | active | — | — |

## Workarounds

1. **Add 'proxy_ignore_headers Content-Length;' in the location block to make nginx ignore the Content-Length header from upstream, allowing the response to be processed based on chunked encoding or connection close.** (85% success)
   ```
   Add 'proxy_ignore_headers Content-Length;' in the location block to make nginx ignore the Content-Length header from upstream, allowing the response to be processed based on chunked encoding or connection close.
   ```
2. **Fix the upstream application to not send Content-Length: 0 when there is a body; use chunked transfer encoding instead by setting 'Transfer-Encoding: chunked' and omitting Content-Length.** (90% success)
   ```
   Fix the upstream application to not send Content-Length: 0 when there is a body; use chunked transfer encoding instead by setting 'Transfer-Encoding: chunked' and omitting Content-Length.
   ```
3. **If the upstream is a legacy server that cannot be changed, use a proxy module like 'ngx_http_lua_module' to strip the Content-Length header: 'header_filter_by_lua_block { if ngx.header["Content-Length"] == 0 then ngx.header["Content-Length"] = nil end }'** (75% success)
   ```
   If the upstream is a legacy server that cannot be changed, use a proxy module like 'ngx_http_lua_module' to strip the Content-Length header: 'header_filter_by_lua_block { if ngx.header["Content-Length"] == 0 then ngx.header["Content-Length"] = nil end }'
   ```

## Dead Ends

- **** — The error is about header semantic validity, not buffer size. (85% fail)
- **** — proxy_set_header modifies request headers, not response headers. (90% fail)
- **** — The error occurs during header parsing, which is independent of response buffering. (95% fail)
