# upstream sent invalid Content-Length: 0 with non-empty body while reading response header from upstream, client: 10.0.0.1, server: example.com

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

## Root Cause

Upstream sends a Content-Length of 0 but then includes a non-empty response body, causing nginx to detect a mismatch and abort.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| nginx 1.18.0 | active | — | — |
| nginx 1.20.1 | active | — | — |
| nginx 1.22.0 | active | — | — |
| nginx 1.24.0 | active | — | — |

## Workarounds

1. **Fix the upstream application to correctly set Content-Length to the actual body size. For example, in a Go HTTP handler, ensure Content-Length matches the body:
func handler(w http.ResponseWriter, r *http.Request) {
    body := []byte("Hello")
    w.Header().Set("Content-Length", strconv.Itoa(len(body)))
    w.Write(body)
}
Avoid hardcoding Content-Length to 0 when writing a body.** (85% success)
   ```
   Fix the upstream application to correctly set Content-Length to the actual body size. For example, in a Go HTTP handler, ensure Content-Length matches the body:
func handler(w http.ResponseWriter, r *http.Request) {
    body := []byte("Hello")
    w.Header().Set("Content-Length", strconv.Itoa(len(body)))
    w.Write(body)
}
Avoid hardcoding Content-Length to 0 when writing a body.
   ```
2. **Use nginx's proxy_hide_header Content-Length; to remove the upstream's Content-Length header, forcing nginx to use chunked encoding or calculate length from the actual body. Example:
location / {
    proxy_pass http://upstream;
    proxy_hide_header Content-Length;
}** (72% success)
   ```
   Use nginx's proxy_hide_header Content-Length; to remove the upstream's Content-Length header, forcing nginx to use chunked encoding or calculate length from the actual body. Example:
location / {
    proxy_pass http://upstream;
    proxy_hide_header Content-Length;
}
   ```

## Dead Ends

- **Set proxy_request_buffering off;** — Request buffering affects client request body, not response body handling from upstream. (95% fail)
- **Increase proxy_buffer_size** — Buffer size does not affect the Content-Length validation logic; the error is about header-body mismatch. (93% fail)
- **Disable chunked transfer encoding with proxy_set_header Transfer-Encoding '';** — This modifies request headers to upstream, not the response; the error is in the response. (88% fail)
